Forum: Ruby on Rails demo :expires_in with :memory_store

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andi C. (Guest)
on 2009-03-09 11:03
 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 topic is locked and can not be replied to.