Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails memcache store default auto expiration time

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?

like image 650
user1954528 Avatar asked Mar 20 '13 05:03

user1954528


2 Answers

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!

like image 118
piersadrian Avatar answered Oct 18 '22 10:10

piersadrian


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.

like image 20
Stuart M Avatar answered Oct 18 '22 09:10

Stuart M