1
[25] pry(main)> Rails.cache.fetch("my_key", :expires_in => 1.year) do
[25] pry(main)*   (Time.now.to_date).to_s
[25] pry(main)* end
=> "2013-11-01"
[28] pry(main)> Rails.cache.fetch("my_key")
=> nil

I cant understand the above behavior. The cache does not have this key before this, or rather it is nil.

This works fine if I remove the expires_in option.

This is in the production version of my app which uses memcached

 # Use a different cache store in production
  config.cache_store = :mem_cache_store, <DNS NAME>

This also works on my local which I expect is the filesystem based caching.

Edit: ah, 1.year is too long perhaps.. It works with 1.day. Is this a bug or is this documented somewhere?

Edit: It appears that 1.month is the max in a duration form. But this still cant be accepted behavior Memcache maximum key expiration time

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87

1 Answers1

2

From a few sources listed below, it appears that this is the way memcached behaves, and nothing to do with rails itself. If, as @Beerlington says, Rails 4.0 doesnt have this, it could be sanity checking the values before passing it to memcached.

So, the answer,

The maximum expiry duration that memcached accepts is 1 month. This is hard coded in its code. If you give a duration longer than 1 month, it will look like it wrote to cache, but it is actually dropped. If you need a longer expiry for some reason, you can always choose to provide an exact time it should expire, i.e the 1.year.from_now style. This will accept longer expiry durations if given this way, and I did not come across any max (perhaps 2038 :P, or not).

Sources

  1. Memcache maximum key expiration time

  2. eliminating memcached's 30-day limit

  3. https://magento.stackexchange.com/questions/5925/memcached-why-data-is-not-stored/5961?noredirect=1#5961

  4. https://groups.google.com/forum/m/#!topic/memcached/TC30yVElo8U

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • 1
    Edit: It isnt dropped exactly.. it is considered as a unix timestamp, and since it is in the past, it isnt saved. – Karthik T Nov 21 '13 at 01:04