Hello, please sign in or register
You are here: Home

mod cache


I have the tiniest pages of javascript on my site, which is generated by server side code which grabs price information from a lot of different webservers. The strategy however is to store this information for up to a day, and hence not have to ask for the information from other webservers again until this period has elapsed . The problem is, how? I can write something at the application layer, i.e. store this data in a database. But better yet get Apache to do it hurrah, Which begs the question, how?

Read Apache's mod_cache documentation if you want further insight. The rest of this document outlines the steps to caching all the pages on my website which begin with



Check mod_cache is installed on your apache server. Open http.conf and you should find mod_cache written e.g...

[CODE=httpd.conf]... LoadModule cache_module modules/mod_cache.so ... # Additionally ... LoadModule disk_cache_module modules/mod_disk_cache.so LoadModule file_cache_module modules/mod_file_cache.so LoadModule mem_cache_module modules/mod_mem_cache.so ... [/CODE]

Set cache variables in httpd.conf

I am running various Virtual Hosts on my webserver. If you are familiar with these then this should not look to strange.

[CODE=httpd.conf]... ### Virtual Hosts ServerName www.mysite.co.uk DocumentRoot /var/www/mysite/www ... ... ... # Cache directives here (below), Must go at the end of the VirtualHost. CacheRoot "/var/lib/httpdcache" # Cache size in KBytes, 50mb CacheSize 50000 # Enable caching for all files of all child directories of dir/ CacheEnable disk dir/ CacheDirLevels 2 CacheDirLength 1 # store for one Day CacheDefaultExpire 86400 # override http headers cache control CacheIgnoreCacheControl On # dont send set-Cookie in headers CacheIgnoreHeaders Set-Cookie # max expiry set to one day CacheMaxExpire 86400 ... [/CODE]

Make the cache directory


[CODE=shell] mkdir /var/lib/httpdcache chmod 777 /var/lib/httpdcache chown nobody /var/lib/httpdcache [/CODE]

Restart httpd server

[CODE=shell] # Works from roots directory, 'su -' service httpd restart [/CODE]

Now define headers, which the server will read to determine whether to use the cached page or not. Open up and edit the individual file placing headers.

[CODE=pricerunner.php] /** * Date in the future */ header("Expires: Mon, 26 Jul 2006 05:00:00 GMT"); /** * Last Modified */ header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); /** * HTTP 1.1 */ header("Cache-Control: "); [/CODE]

Cleaning up the Cache

You might want to free up some space by removing old cached files, currently mod_cache cant achieve this although its in Apache's TODO.

Apache stores the files in the location you specified in settings above. To view the current size of the directory you will need something like... [CODE=shell] du -c --max-depth=0 /var/lib/httpdcache -h [/CODE] To remove expired files ... [CODE=shell] #find all files under /dir older than 7 days, and delete find /dir -type f -mtime +7 | xargs rm -f #find all files newer than FILE, and delete find /dir -type f -newer /path/to/FILE -exec rm \{\} \; #find all files older than 10 minutes, and print names(print is default) find /dir -type f -mmin +10 -print #find all files newer than 1 day, and tar them to file.tar find /dir -type f -mtime -1 | tar -c -T - -f file.tar [/CODE] The above script will not remove empty directories. Here is how to remove 'em... [CODE=shell] find /path/to/cache -depth -type d -empty -exec rmdir {} \; # Combine the above two scripts to # 1. remove the old cached files and # 2. remove the empty directories - created by the first operation. find /path/to/cache -type f -mtime +3 | xargs rm -f && find /path/to/cache -depth -type d -empty -exec rmdir {} \; [/CODE]



Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Email never shown*
Home Page


Andrew Dodson
Since:Feb 2007

Comment | flag


Bookmark and Share