Rails cache expire

本小妞迷上赌 提交于 2021-02-18 10:36:07

问题


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)

I can read it through Rails.cache.read('temp') and Rails.cache.fetch('temp').

The problem is it doesn't expire. It will still be alive after 60 seconds. Can any one tell me what is missing here.

FYI: I have declared in my development.rb as follows :

config.action_controller.perform_caching = true

config.cache_store = :memory_store

Is there anything I missed out? I want to expires my cache.


回答1:


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.



回答2:


The :expires_in option only works with compatible stores (eg memcached) - not memory.

From http://guides.rubyonrails.org/caching_with_rails.html:

Finally, if you are using memcached or Ehcache, you can also pass :expires_in. In fact, all parameters not used by caches_action are sent to the underlying cache store.




回答3:


You should use fetch method with do block instead of Rails.cache.write. Even though the name is "fetch", it writes to the cache if you put inside do block. You also do not need to clear cache entry manually, just use the "fetch" with expires_in and race_condition_ttl. Keep the ttl value under expiry time and keep it as small as possible.

output_json = Rails.cache.fetch('temp',expires_in: 1.minute,race_condition_ttl:3) do
  my_expansive_operation().to_json
end


来源:https://stackoverflow.com/questions/8846400/rails-cache-expire

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!