Rails cache expire

前端 未结 3 2218
遇见更好的自我
遇见更好的自我 2021-02-20 11:42

I have a rails application, in that I am using simple rails cache. My testing is as follows:

Rails.cache.write(\'temp\',Date.today,:expires_in => 60.seconds)
         


        
3条回答
  •  [愿得一人]
    2021-02-20 12:06

    After some search, I have found one possible reason why the cache is not cleaned after 60 seconds.

    1. You call Rails.cache.write which is documented here.
    2. It calls write_entry(namespaced_key(name, options), entry, options), where your option :expires_in is one part of the options argument.
    3. The implementation of write_entry has the following condition:

      if expires_in > 0 && !options[:raw]
          # Set the memcache expire a few minutes in the future to support race condition ttls on read
          expires_in += 5.minutes
      end
      

    So there are 5 minutes added to your 60 seconds. 2 possible solutions:

    • Just live with it :-)
    • Try to include the option :raw => true, perhaps this will skip the condition, so that your expiry works as suspected.

提交回复
热议问题