Demo :expires_in with :memory_store

require ‘rubygems’
require ‘active_support’
cache = ActiveSupport::Cache.lookup_store(:memory_store)
#cache=Rails.cache
def cache.fetch(key,options={})
second=options.delete(:expires_in).to_i
raise “cache_fetch method second can not blow zero” if second<=0
time_tag = ((Time.now.to_i/second)*second)

    expires_key = [time_tag,key]

p expires_key
instance_eval{@data.delete_if{|k,v|
Time.now.to_i>k.first+second}}
super(expires_key,options){yield}
end

puts cache.fetch(‘test’,:expires_in=>3.seconds){rand}
puts cache.fetch(‘test’,:expires_in=>3.seconds){rand}
sleep 4
puts cache.fetch(‘test’,:expires_in=>3.seconds){rand}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs