Matador
Stop cache expiration triggered thundering herd problems with Matador. Currently works with redis.
Installation
Add this line to your application's Gemfile:
gem 'matador'
And then execute:
$ bundle
Or install it yourself as:
$ gem install matador
Usage
Set your cache service object (currently only supports redis)
require "matador"
require "redis"
$redis = Redis.new(host: "127.0.0.1", port: "16379")
Matador.cache_store = $redis
Caching with an expiration is susceptible to a thundering herd in high traffic situations:
data = time_consuming_operation(...)
$redis.set("some_key", data)
$redis.expire("some_key", 120)
Wrap it in matador. When the cache expires after 2 minutes, the first request goes through to generate the new cache. While the new cache is being built (next 10 seconds), all other requests are served the stale cache.
Matador.fetch("some_key", :ttl => 120, :therd_ttl => 10) do
time_consuming_operation
end
TO DO
- Integrate with Memcache
Contributing
- Fork it ( http://github.com/
/matador/fork ) - Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request