Topic: Cached files not being 'seen' after changing page_cache_directory

My question is: how can i get rails to use cached files when they are being created in the 'public/cache' directory rather than in 'public'?

Following the directions in fngtips article entitled 'lazy sweeping' i changed the directory into which rails writes its cache files by adding the following line to my environment file.

config.action_controller.page_cache_directory = RAILS_ROOT+"/public/cache/"

Running my app (locally) i can see that rails is creating the cache files properly in public/cache, but it's not 'seeing' them. The next time the same url is accessed the log files show that the cache is being ignored, and getting re-generated.

If i remove the custom cache_directory instruction (so that cached files are dumped into the root of 'public') then rails uses the cached files as expected. But this is inconvenient for mass-deletion of cache files.

Does anyone know how to ensure that cached files stored in public/cache are used properly?

Re: Cached files not being 'seen' after changing page_cache_directory

I have this same problem. Caching works in default mode but once I switch the directory, Rails kind of bugs out on me and doesnt even see the cache.

Any advice would be appreciated

Re: Cached files not being 'seen' after changing page_cache_directory

What kind of caching are you doing? Is it page caching? In that case Rails is never supposed to "see" the cache. It's supposed to be fetched directly by the web server (such as apache). So you'll need to configure your web server to look in that directory for the files.

Railscasts - Free Ruby on Rails Screencasts

Re: Cached files not being 'seen' after changing page_cache_directory

Hi ryan. Yes its page caching i'm using, thanks for the clarification.

On my production server Apache is used as the front end. Am i correct in thinking that the following code (in .htaccess) is sufficient for the custom cache directory setup to work properly in that environment?:

RewriteRule ^$ cache/index.html [QSA]
RewriteRule ^([^.]+)$ cache/$1.html [QSA]

If so then i guess its just my local mongrel configuration that needs changing then so that caching works when i run the site on my development machine too.

Re: Cached files not being 'seen' after changing page_cache_directory

Yeah, it looks like that will work. This tutorial shows how to do it in lighttpd as well. As far as mongrel though, I'm not sure how. Caching isn't normally enabled in development mode anyway, so is it an issue?

Railscasts - Free Ruby on Rails Screencasts

Re: Cached files not being 'seen' after changing page_cache_directory

I've specifically enabled caching for dev mode at the moment so i can see what's going on more clearly in the logs. Thanks for all the info, and for railscasts, that's turning out to be an excellent resource! If i figure out how to configure mongrel i'll post back here.

Re: Cached files not being 'seen' after changing page_cache_directory

I did a quick write up about this here http://www.bitbutter.com/page_cache_directory-gotcha/27

Re: Cached files not being 'seen' after changing page_cache_directory

I think I'm having this exact same issue, only with fragment caching. On edge:

<% cache :controller => "base", :action => "foo_bar" do %>
Do fancy stuff!
<% end %>

After a while - I haven't isolated exactly what causes it - rails starts ignoring the expired caches and just keeps some old version instead. I _think_ it's related to conflicts with a routing namespace I have. Not sure tho'.

I have never ever cached before, though, completely beginner on this. I might just oversee some basics.

This is with nginx proxying to mongrel, btw.

Last edited by leethal (2007-09-22 11:09:31)

Re: Cached files not being 'seen' after changing page_cache_directory

You need to add the leading "/" for Apache.  This took me a while to figure out!

RewriteRule ^$ /cache/index.html [QSA]
RewriteRule ^([^.]+)$ /cache/$1.html [QSA]

cbit wrote:

Hi ryan. Yes its page caching i'm using, thanks for the clarification.

On my production server Apache is used as the front end. Am i correct in thinking that the following code (in .htaccess) is sufficient for the custom cache directory setup to work properly in that environment?:

RewriteRule ^$ cache/index.html [QSA]
RewriteRule ^([^.]+)$ cache/$1.html [QSA]

If so then i guess its just my local mongrel configuration that needs changing then so that caching works when i run the site on my development machine too.

Re: Cached files not being 'seen' after changing page_cache_directory

Those rewrite rules need some modifications to work correctly.  Something like this in your htaccess file will work a bit better, but still needs improvement:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{REQUEST_URI} ^([^.]+)/?$
RewriteCond %{DOCUMENT_ROOT}/cache/%1.html -f
RewriteRule ^[^.]+/?$ /cache/%1.html [QSA,L]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule ^$ /cache/index.html [QSA,L]

This won't work if you have trailing slashes, but it works in other cases.

Re: Cached files not being 'seen' after changing page_cache_directory

Okay, after 2 days of work, I figured out how to create an htaccess file to complement changes to page_cache_directory.  This will allow Apache to find cached pages using Rails builtin page caching (not action caching or fragment caching.  Without a proper htaccess file either Apache cannot find the cached pages or the rewrite rules will interfere with Rails routes in the routes.rb file.

This solution has been tested with passenger on my website hosted at railsplayground.com.

Here is the final product:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^([^.]+)/$
RewriteRule ^[^.]+/$ /%1 [QSA,L]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{REQUEST_URI} ^([^.]+)$
RewriteCond %{DOCUMENT_ROOT}/cache/%1.html -f
RewriteRule ^[^.]+$ /cache/%1.html [QSA,L]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule ^$ /cache/index.html [QSA,L]

12

Re: Cached files not being 'seen' after changing page_cache_directory

Thanks jzwolak; I spent 2 hours playing around with mod_rewrite on my server yesterday before finally giving in and resigning myself to the fact I'd have to add all the cached files in /public to .gitignore instead. I just tried your ruleset and it worked perfectly first time smile