Rails cache expire

前端 未结 3 2224
遇见更好的自我
遇见更好的自我 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:09

    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
    

提交回复
热议问题