I have been struggling for a while to find out if there is some default expiration time set by Rails, in case we don't provide any while storing a key-value pair to memcache? e.g. Rails.cache.write('some-key', 'some-value')
Would rails set some expiration time by default if we haven't specified?
If you're using the default, built-in MemCacheStore
class provided by Rails, then no. It won't assume an expiry time when you create new cache entries. You can read the applicable code to verify that. It checks to see if you've passed an expires_in
option to the #write
method like
Rails.cache.write("key", "content", expires_in: 2.hours)
and if you haven't, simply passes 0 to memcache indicating no expiry time. Hope this helps!
If you are using the newer (and I think better) Dalli memcached gem, you can configure it at the adapter-level using a line like the following:
config.cache_store = :dalli_store, 'cache-1.example.com', 'cache-2.example.com',
{ :namespace => NAME_OF_RAILS_APP, :expires_in => 1.day}
See the README for a detailed explanation of the :expires_in
option. Overall, I think Dalli is worth checking out for more than just this feature, its also faster and supports some newer authentication features, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With