Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing expired cache entries from disk cache on Ruby on Rails 4

I have a rails app which uses disk cache for the now default Russian-doll caching. I have no trouble with invalidating the cache and my cache strategy is working to my requirements, however I have to find a proper way to delete the expired entries from the disk. As documented the disk cache keeps on growing until it is either cleared or the disk is full.

I'm aware that I can do rake tmp:cache:clear but that deletes the entire cache, not just stale items! I'm looking for a better way which will preserve fresh entries and delete from the disk only stale cached entries. I'm already using a shell script to delete entries that have not been accessed in the last day but this does not guarantee I'm deleting only stale entries and preserving all fresh entries.

I am aware I can switch to memcached or redis, but I prefer not to, as disk cache is doing a fine job without the overhead of resources and supporting yet another server (server in terms of a server process, not actual hardware/virtual-machine).

How do you clear stale cache entries when using disk cache? Is there a better way then using the files' atime/mtime?

like image 606
Georgi Avatar asked Dec 16 '14 10:12

Georgi


People also ask

How do I flush Rails cache?

clear in the Rails console to clear the cache. Unfortunately there is no way (yet) to do it from the command line. We can add a rake task that will do this for us. Then we can execute ./bin/rake cache:clear and clear the Rails cache from the command line.

Where is Rails cache stored?

Memory store Since version 5.0, Rails automatically sets up the :memory_store in the development configuration when generating a new application. When using the memory store, cached data is kept in memory in the Ruby web server's process.

How do I access Rails cache?

You can play around with caching locally by running rails dev:cache , or by setting config. action_controller. perform_caching to true in config/environments/development. rb .

What is Russian doll caching?

Ruby on Rails Caching Russian Doll Caching You may want to nest cached fragments inside other cached fragments. This is called Russian doll caching . The advantage of Russian doll caching is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment.


1 Answers

According to documentation, you might use #cleanup http://api.rubyonrails.org/classes/ActiveSupport/Cache/FileStore.html#method-i-cleanup

You could, for example, schedule a cron job to run it periodically on your hosts ./script/rails runner -e production 'Rails.cache.cleanup'

like image 125
robinw777 Avatar answered Oct 29 '22 01:10

robinw777