New Drupal "cache" module started

18 Aug 2009
Posted by doq

Long time ago in early 2006 I have developed my Drupal's cache.inc shared memory based implementation. APC implementation was done after some time. But these contributions to Drupal 4.7 (or was it 4.6) were never published.

Years passed. Things happened. I realized open-source to be one of the greatest things happening to me. Now I can't imagine writing Drupal module on my free time and not to share it with community.

So today I would like to present my new Drupal 6.x contribution - Cache module.

Features, difference from other solutions

Cache module provides bridge between Drupal and alternative cache handler implementers, like APC or memcache. The module started as fork of Cache Router in July 2009. The aim was my own view of how caching should be implemented, provide more features and to make implementation more reliable.

Now (version 6.x-1.0-alpha6 when this article was written) it supports the following caching handlers:

Name Engine type Description
XCache xcache XCache PHP op-code and variable caching.
No caching null Disable caching for specified cache bin.
phpvar phpvar NOT SUPPORTED. DO NOT USE.
Database database Alternative database cache handler.
APC apc Alternative PHP cache.
Memcache memcache Memcached caching.
Eaccelerator eaccelerator Eaccelerator engine. NOT SUPPORTED. DO NOT USE.
Memcached memcached Memcached caching based on memcached PECL extension.
Redis redis Volatile key-value database wih support of lists and sets with atomic operations to push / pop elements.
File file Caching into files.

Eaccelerator and experimental phpvar caching engines are currently unmaintained. Cache module also provides implementation of page_fast_cache() function.

Additional features (as comparing to Cache Router module 6.x-1.0-beta8) added:

  • hook_requirements() implementation. You can see report status of used cache handlers, see what is required for them to work properly.
  • Alternative session handler. Session data can be stored in any of specified caching solution. This feature should be familiar from Memcache API and Integration module.
  • Set of core patches to improve performance on big Drupal web-sites. Check out patches directory.
  • In case all alternative cache handlers fail (e.g. no APC, memcache installed) it will use standard database caching. So web-site won't break. Administrator then may check "Status report" page and see why some cache handlers fail. Watchdog logs will provide useful diagnostics information.
  • Cache handlers chaining. It is possible to specify cache handler for table and also to specify one or several backup cache handlers. For example, assume we want to put critical cache_form data into memcached server but also want entries to be read from database in case memcached server will fail.
  • Alternative path.inc implementations. More information here.
  • Working simpletest cache.test implementation to check whether your cache configuration works.
  • Statistics (provided by memcache, memcached, file engines).

Installing module

Enable "Cache" (from "Caching" package) module on Administer -> Site building -> Modules page.

Add the following lines to your settings.php configuration file:

    $conf['cache_inc'] = './sites/all/modules/cache/cache.inc';
    $conf['cache_settings'] = array(
      'engines' => array(
        'database-engine' => array(
          'engine' => 'database',
        ),
      ),
      'schemas' => array(
        'database-schema' => array(
          'database-engine',
        )
      ),
      'bins' => array(
        'default' => 'database-schema',
      ),
    );

"default" is for the default caching schema. All valid cache bins (tables) can be added in addition, but you must have a "default" if you skip any bins.

You can add several more cache schemes and use them for bins. Each schema is array of cache engines. If several engines per schema is specified then cache chaining will be used: for example if cache entry is not found in 1st engine then we'll try to find it in 2nd, then in 3rd etc.

Cache engines are specified in 'engines' array. Every of them requires 'engine' key to be set to any of the following values: apc, db, file, null, memcache, memcached, redis, xcache, eaccelerator. For other configuration options please check README.txt file which is supplied with module package.

To enable alternative session handler you might want to add to web-site's settings.php:

    $conf['session_inc'] = './sites/all/modules/cache/session.inc';

New bins "cache_session" and "cache_session_user" will be used in this case. You may configure any caching schema for them like for any other table.

To enable alternative path storage handler you might want to add to web-site's settings.php:

    $conf['path_inc'] = './sites/all/modules/cache/path/path-max.inc';

or
    $conf['path_inc'] = './sites/all/modules/cache/path/path-cache.inc';

This feature also requires core patch path_inc-variable-support.patch to be applied. It can be applied manually or applied through web administration using patchdoq module if installed: Administer -> Site building -> Patch.

New bins "cache_path_alias" and "cache_path_source" will be used. You may configure any caching schema for them like for any other table. Aware that leaving it for default database engine might degrade performance.

Examples of working Cache module configuration:

Example #1:

    $conf['cache_inc'] = './sites/all/modules/cache/cache.inc';
    $conf['session_inc'] = './sites/all/modules/cache/session.inc';
    $conf['cache_settings'] = array(
      'engines' => array(
        'db' => array(
          'engine' => 'database',
          'server' => array(),
          'shared' => TRUE,
          'prefix' => '',
        ),
        'apc' => array(
          'engine' => 'apc',
          'server' => array(),
          'shared' => TRUE,
          'prefix' => '',
        ),
        'memcache' => array(
          'engine' => 'memcache',
          'server' => array(
            'localhost:11211',
            'localhost:11212',
          ),
          'shared' => TRUE,
          'prefix' => '',
        ),
      ),
 
      'schemas' => array(
        'db' => array(
          // Engines:
          'db',
        ),
        'apc' => array(
          // Engines:
          'apc',
        ),
        'memcache' => array(
          // Engines:
          'memcache',
        ),
      ),
 
      'bins' => array(
        // Bin name     => Schema name.
        'default'       => 'db',
        'cache'         => 'apc',
        'cache_form'    => 'memcache',
        'cache_page'    => 'memcache',
        'cache_filter'  => 'memcache',
      ),
    );

Example #2 (with cache chaining - very rare usage case but may be useful):

    $conf['cache_inc'] = './sites/all/modules/cache/cache.inc';
    $conf['session_inc'] = './sites/all/modules/cache/session.inc';
    $conf['cache_settings'] = array(
      'engines' => array(
        'apc-engine' => array(
          'engine' => 'apc',
          'server' => array(),
          'shared' => TRUE,
          'prefix' => '',
        ),
        'memcache-engine' => array(
          'engine' => 'memcache',
          'server' => array(
            'localhost:11211',
            'localhost:11212',
          ),
          'shared' => TRUE,
          'prefix' => '',
        ),
        'db-engine' => array(
          'engine' => 'database',
          'server' => array(),
          'shared' => TRUE,
          'prefix' => '',
        ),
      ),
      'schemas' => array(
        'apc-memcache-db-schema' => array(
          // Engines:
 
          // Primary cache.
          'apc-engine',
 
          // Secondary cache.
          'memcache-engine',
 
          // Another secondary cache, in case both APC and memcache server fail.
          'db-engine',
        ),
 
        'apc-schema' => array(
          // Engines:
          'apc-engine',
        ),
        'memcache-schema' => array(
          // Engines:
          'memcache-engine',
        ),
      ),
      'bins' => array(
        // Bin name     => Schema name.
        'default'       => 'memcache-schema',
        'cache'         => 'apc-schema',
        'cache_form'    => 'apc-memcache-db-schema',
        'cache_filter'  => 'my-memcache',
      ),
 
    );

Downloads and more information

Download Cache module from drupal.org. More information can be found in project issues.

Cache project is similar in functionality to the following projects (so you may find them also useful):

Comments

Do NOT Forget....

If you have several sites sharing the same memcached, APC or db put appropriate prefixes in appropriate spaces. It took me a couple of hours to figure that out (LOL).
Great module - 30% increase in hitrate on drupal6

Val (not verified) | Jan 8th, 2010 at 4:53 am

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

 
 
 
If you have found mistakes in the text then please select it and press Ctrl-Enter to send report to the site administrator.