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.
Rails 5.2 introduced built-in Redis cache store, which allows you to store cache entries in Redis.
Keep it in memory thread_mattr_accessor method which makes it easy to build a trivial thread-safe cache service. Rails' implementation ensures every thread has its own storage location for cached_value ; therefore this should be thread-safe (assuming #compute_value can safely run both concurrently and in parallel).
Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the web server (i.e. Apache or NGINX) without having to go through the entire Rails stack. While this is super fast it can't be applied to every situation (such as pages that need authentication).
View caching in Ruby on Rails is taking the HTML that a view generates and storing it for later.
After some search, I have found one possible reason why the cache is not cleaned after 60 seconds.
Rails.cache.write
which is documented here.write_entry(namespaced_key(name, options), entry, options)
, where your option :expires_in
is one part of the options
argument.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:
:raw => true
, perhaps this will skip the condition, so that your expiry works as suspected.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With