Really simple and efficient caching system using htaccess

I recently had to use a caching system in a web service we provide at Epsitec.

The service needed to provide some data, generated every day. This data was retrieved from another server then processed to get useful information, and kept the same for the whole day. We also wanted to store the data.

I decided to devise a really simple caching system, inspired by WP Super Cache.

The system works that way:

  • Hide the “processing” part from the user using URL rewriting
  • The user always thinks he’s accessing a file
  • If the file does not exist yet, process the data, create the file, serve the file
  • If the file does not exist but cannot be fetched (too early, too late), return an error

Here is the code of the htaccess file:

1
2
3
4
5
6
7
8
9
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
 
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^daily/([0-9]{4})-([0-9]{2})-([0-9]{2})\.xml$ /fetcher.php?year=$1&month=$2&day=$3 [L]
 
</IfModule>
ErrorDocument 404 /error.xml

You can see that the name of each file is daily/yyyy-mm-dd.xml.

We first check that the file does not exist yet with RewriteCond. The regexp passes it to the fetcher to generate the content using RewriteRule.

My fetcher.php file is the script that retrieves the data, processes it and saves it into a file.

It has many advantages:

  • Retrieve the data only once a day, efficient for both you, your data provider and the user who doesn’t have to wait
  • Efficient, since most of the accesses are accesses to a file, involving no processing, and no database access