Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple caches in rails?

I have a rails application where I would like to use both memcached and the file store cache, for different purposes.

I want to use the file store cache to keep a large number of pages that don't change often (some not at all) - i.e. page caching - and use memcached for everything else (action and DB caching etc). The reason is that the pages stored on the file store cache are likely to require a large amount of storage, but individually most will be accessed infrequently.

Is this possible to do or will configuring memcached as the cache mean that it is also used for page caching?

As a secondary question, what is a safe way to remove pages from the file store cache in some form of cron job, as there does not seem to be an option to specify ttl for this cache. For example a UNIX find command would quickly find and remove all old pages or pages that haven't been accessed in a long time - is this safe to do given the app server might potentially try to serve one of those pages at the time (tho this is very unlikely)? If not then what is the best way to do this.

like image 658
frankodwyer Avatar asked Apr 08 '09 12:04

frankodwyer


2 Answers

If you want to use the filesystem only for page caching and memcached for action and fragment caching, you're fine. Page caching always uses the filesystem. Just remember that page caching bypasses your Rails application, so you can't use it for pages that include content that changes from user to user or for pages that are access controlled with filters.

Regarding the removal of pages, on Unix, a file can be deleted, but it is not actually removed from disk until all open file handles are closed. If the app server has opened the file to serve a request, and the find command deletes it a split-second later, the app server doesn't suddenly get an error when it tries to read.

You could also consider having find delete files based on their last access time, instead of creation or modification, and using a sweeper in your Rails app to delete the cached page when its content is out of date.

like image 82
Steve Madsen Avatar answered Oct 03 '22 19:10

Steve Madsen


A simpler approach may be to use a http cache upstream of your application as your page cache rather than two stores within rails. This way you can use http headers to control the cache behavior, including TTL's. These same limits will also apply to browser's local caches as a nice bonus.

Varnish is about as high performance as it gets, but would require setting up another moving piece in your hosting environment as a proxy. This may still be worthwhile depending on what you're doing.

A simpler approach might be Rack::Cache, which will be easy to set up provided you're using a rack enabled version of rails.

like image 35
Jason Watkins Avatar answered Oct 03 '22 20:10

Jason Watkins