SyntaxHighlighter

Tuesday, July 05, 2011

Set a default expires_in value for memcache

I bet there is an easier way to do this, but here's what I came up with. I didn't want to cache items forever (which is the default with memcache and rails). I wanted to cache items for 6 hours, max. To do this, I wrote a patch and if the :expires_in option is missing, it will set it to 21600. I would love to hear if there is a better way...


module ActiveSupport
module Cache
class Store

private

# Original code from ActiveSupport::Cache::Store
#
# def expires_in(options)
# expires_in = options && options[:expires_in]
# raise ":expires_in must be a number" if expires_in && !expires_in.is_a?(Numeric)
# expires_in || 0
# end

def expires_in(options)
expires_in = options && options[:expires_in]
raise ":expires_in must be a number" if expires_in && !expires_in.is_a?(Numeric)
expires_in || 6.hours
end

end
end
end