Simple hash with expiring values.
- Simple - Returns value or nil.
- Default - Returns value or default
- Block - Returns value, or refreshes the value by yielding to long running job.
Add this line to your application's Gemfile:
gem 'cache_hash'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cache_hash
Returns value or nil.
c = CacheHash.new(ttl: 3)
c[:a] = "its here"
puts c[:a] # => "its here" or nil if called after 3 seconds
Returns value or default.
x = 0
c[:foo] = "HAZ VALUE"
c.fetch(:foo, "NO VALUE") # => "HAZ VALUE"
# wait 3 seconds
c.fetch(:foo, "NO VALUE") # => "NO VALUE"
Returns value, or refreshes the value by yielding to long running job.
c = CacheHash.new(ttl: 5)
users = ['smith', 'jones']
# first run loads cache
users.each do |user|
row = c.fetch(user) {
# long running job
get_user(user)
}
end
# second run hits cache
users.each do |user|
row = c.fetch(user) {
# long running job
get_user(user)
}
end
# third run refreshes cache
sleep 6
users.each do |user|
row = c.fetch(user) {
# long running job
get_user(user)
}
end
c = CacheHash.new(ttl: 2, gc_interval: 1)
- Fork it ( https://github.com/[my-github-username]/cache_hash/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 a new Pull Request