I've got a Rails app that has stopped caching somewhere along the way, and I'm not sure which revision along the way might have stopped it from working.
I'm under the impression that page caching, when working properly, should never even hit Rails if it finds the cached file. However, when loading my page and monitoring production.log, it's hitting both Rails and the DB.
I have a sweeper set up that clears the cache on :create, :update, and :destroy. It works fine, as the /public/cache/index.html file is updated whenever one of those events occurs. I thought at first that it might be because I was using the OutputCompression plugin, but removing that had the same result, so I put it back in. The index.html is there, but .htaccess and Rails ignore it and rebuild the entire page, including rewriting the cached index.html.
Here are the relevant parts of the code (unless I'm missing something):
Controller:
class SecretsController < ApplicationController
caches_page :index
cache_sweeper :secret_sweeper, :only => [:create, :update, :destroy]
# snipped
end
.htaccess:
RewriteEngine On
# Rewrite index to check for cached
RewriteRule ^/$ /cache/index.html [QSA]
RewriteRule ^$ /cache/index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Firebug response headers
Date: Tue, 02 Jun 2009 18:50:36 GMT
Server: Apache/1.3.41 (Unix) mod_fastcgi/2.4.2 PHP/5.2.9 mod_log_bytes/1.2 mod_bwlimited/1.4 mod_auth_passthrough/1.8 FrontPage/5.0.2.2635 mod_ssl/2.8.31 OpenSSL/0.9.8b
Vary: Accept-Encoding
X-Runtime: 0.05637
Etag: "4f3497a74141d1e92ae7a1fe4d5dc1d2"
Cache-Control: private, max-age=0, must-revalidate
Content-Encoding: gzip
Content-Length: 22356
Connection: close
Content-Type: text/html; charset=utf-8
default-style: tms
I'd love to be able to use mod_gzip, but ASmallOrange doesn't support it, while DreamHost did (before they tripled my price).
Anyway, can anyone shed any light on why Rails is ignoring the cached index.html? I'm assuming it's something in .htaccess, since it should never touch Rails if it's working right.
EDIT: The caching problem turned out to be the first slash on the RewriteRules. It wasn't finding the cached file until I changed them both to "cache/index.html", and now caching works perfectly.
However, now I do have to remove the OutputCompression calls, because it's returning the gzipped version of the file with the Content-Type set to "text/html". Any idea how to get it to send the correct content type for just that file? It's the only one cached in the entire app.
EDIT AGAIN: Changing the .htaccess to this didn't help with the gzip problem:
RewriteRule ^/$ cache/index.html [QSA,T=application/x-gzip]
RewriteRule ^$ cache/index.html [QSA,T=application/x-gzip]
It still shows up as the text representation of a zip file (i.e. gibberish), unless compression is disabled. Caching works perfectly, though.