Home page > Scripts > Filesystem cache for Drupal

Filesystem cache for Drupal

Tuesday 4 June 2013, by Fil

This is a drop-in replacement for Drupal 6’s cache system. We avoid a lot of problems by using a filesystem-cache in lieu of the classic database-cache.

Our site with Drupal 6.26 (published 2012-05-02) kept crashing because it filled its `cache_menu` table too quickly.

This is a drop-in replacement for includes/cache.inc that makes Drupal use a filesystem-based cache instead of the database.


  1. Download the file (http://trac.rezo.net/trac/rezo/brow...)
  2. Rename the old includes/cache.inc to includes/cache.classic.inc, in case you want to go back.
  3. Install this file as includes/cache.inc.
  4. By default, we use the sys_get_temp_dir() command to learn where to store the cache files. If you prefer to see what happens, create a tmp/ directory (with proper write permissions for www-data and a security .htaccess file); the code will use this directory if it exists.


This has not been tested yet on any other version than Drupal 6.26. If it works for you, please comment in the forum below.

How it works

When Drupal tries to cache some data it uses

cache_set($cid, $data, $table, $expire, $headers)

… our code computes a 4-hexdigits hash key from (cid, table), and stores the values (data, headers) in the file named tmp/a0/3c.cache.

When Drupal requests this value from the cache, we load the values from the same file, check that the cid and table match the request, that the cache has not expired, and then return the value.

There will be collisions in this small space (16^4= 65,536 files “only”). But that’s a deliberate feature, because it allows us to avoid using a costly garbage collector [1].


P.S. Something better probably exists somewhere. I just couldn’t find it; if this is useful for you, or if you have suggestions, please comment below.


[1] This approach was initially developed for SPIP, see Memoization.

Reply to this article