Create a gist now

Instantly share code, notes, and snippets.

module ActiveSupport
module Cache
class MemoryStoreWithExpiry < MemoryStore
def initialize
super
@times = {}
end
def write(name, value, options = nil)
if options && options[:expires_in]
@times[name] = { :cached_at => Time.now, :expires_in => options[:expires_in] }
end
super
end
def read(name, options = nil)
return if expired?(name)
super
end
private
def expired?(name)
return false unless @times[name]
(Time.now - @times[name][:cached_at]) > @times[name][:expires_in]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment