Forum Moderators: phranque
To further enhance my server, I am trying to set up a cache for dynamically generated content. Considering the speed benefits of using memory over disk, (no I/O operations), I decided to use mod_mem_cache. Here's my config:
# Configure memory cache
<IfModule mod_mem_cache.c>
MCacheSize 64000
MCacheRemovalAlgorithm LRU
MCacheMinObjectSize 1024
MCacheMaxObjectSize 524288
</IfModule>
# Enable cache
<IfModule mod_cache.c>
CacheEnable mem /
CacheDefaultExpire 86400
CacheIgnoreHeaders Set-Cookie
</IfModule>
I have several virtual host containers; most of my sites serve dynamically generated 'static' content (pardon the poor choice of words here, what I mean to say is that the content is grabbed froma DB but changes only rarely). I figure some caching could save me a great deal of processing power, since the CMS needs to be invoked a lot less.
Using mod_rewrite, I have arranged my site's URL structure like http://www.example.com/section/page/. In my debug logs, I see mod_cache is serving everything (images etc. -> stuff which I will exempt from caching later) except the HTML content of /page/ itself! It does cache, but doesn't serve from the cache later on.
Why does mod_cache refuse to serve a perceived directory index? And how do i make sure it does?
[edited by: jdMorgan at 7:18 pm (utc) on Dec. 24, 2009]
[edit reason] example.com [/edit]
<IfModule mod_disk_cache.c>
CacheRoot /var/cache/httpd
CacheDirLength 2
</IfModule>
# Enable cache
<IfModule mod_cache.c>
CacheEnable disk /
CacheDisable /textpattern
CacheDisable /images
CacheDefaultExpire 86400
CacheIgnoreCacheControl On
CacheIgnoreHeaders Set-Cookie
</IfModule>
Here are some logs:
[Thu Dec 24 01:34:54 2009] [debug] mod_cache.c(131): Adding CACHE_SAVE filter for /
[Thu Dec 24 01:34:54 2009] [debug] mod_cache.c(138): Adding CACHE_REMOVE_URL filter for /
[Thu Dec 24 01:34:54 2009] [debug] mod_cache.c(131): Adding CACHE_SAVE filter for /index.php/
[Thu Dec 24 01:34:54 2009] [debug] mod_cache.c(138): Adding CACHE_REMOVE_URL filter for /index.php/
[Thu Dec 24 01:34:54 2009] [debug] mod_headers.c(743): headers: ap_headers_output_filter()
[Thu Dec 24 01:34:54 2009] [debug] mod_cache.c(663): cache: Caching url: /index.php/
[Thu Dec 24 01:34:54 2009] [debug] mod_cache.c(669): cache: Removing CACHE_REMOVE_URL filter.
[Thu Dec 24 01:34:54 2009] [debug] mod_disk_cache.c(977): disk_cache: Stored headers for URL http://www.example.com:80/index.php/?
[Thu Dec 24 01:34:54 2009] [debug] mod_disk_cache.c(1066): disk_cache: Body for URL http://www.example.com:80/index.php/? cached.
It appears Apache is using the rewritten URL to identify the cache entry. i.e. the cache module sees my page not as http://www.example.com/ but as /index.php, per the generic rewrite in my .htaccess file. Since all my pages rewrite internally to /index.php, the wrong content is bound to be served. To fix this, I have had to change my rewrite to /index.php to a rewrite to /index.php/$1. This makes sure all my pages have a unique identifier for mod_cache to work with.
However still, the directory index (which is my CMS-generated content) is cached, but it is re-cached on every load. You can see this in the first few lines of the log entry, where the module doesn't even check for the presence of a cached version, but instead immediately proceeds to apply a SAVE - filter.
I have tried an ugly workaround involving adding a constant to the rewrite (i.e. rewriting to /index.php/$1cache), but that also fails, because the module apparently applies its SAVE filter both to the requested and rewritten URL. Isn't there a clean way to solve this problem?
[edited by: jdMorgan at 7:19 pm (utc) on Dec. 24, 2009]
[edit reason] example.com [/edit]
So in your case, the URL is example.com/, and that is resolved to the filepath /index.php
I hope that clears up at least that one point.
The only thing that looks a little fishy is that you've apparently got caching enabled for your entire filespace, and the cache files are apparently also stored in that filespace. I would think you'd want them separated off into their own directory, which of course would be set so that mod_cache wouldn't try to re-cache anything in there. Just a thought -- I don't know anything about this module. :(
Jim
Thank you for your thoughts. In my case, URLs are purposely rewritten to index.php/$1 instead of index.php to prevent the cache from hitting the same entry on every request.
As for the cache, it is stored in /var/cache/http, as specified in the CacheRoot directive. The cacheenable directive specifies the urls for which cacheing is enabled, relative to your documentroot.
I've passed this one on to the apache user list. Keep you posted.