Cache in Drupal 7

Janez Urevc
[janez@|http://]janezurevc.name
@slashrsm

What is cache?

A, B, C => => result
The idea is to save the result and use it, when we have to calculate the same problem with the same input data again.

Use cases in web development?

  • Heavy database queries;
  • Output that is hard to render;
  • Complex calculations;
  • Things that need a lot of I/O;
  • Data, that was fetched from remote servers;
  • and many, many more...
Disk == slow
RAM == fast


General idea is to store as much data as possible into RAM.

Cache in Drupal 7

Cache item

A single cache entry. It has a key and a value.

Cache bin

A group of items that live together; "A bucket where we put our data".

Cache backend

Technology used to saved data (MySQL, Memcached, APC, Redis, ...). Each bin can be saved to a different back-end.

Drupal cache for developers

 
if ($cache = cache_get('my_cache_item', 'cache')) {
  $data = $cache->data;
}
else {
  // some heavy calculations...
  
  cache_set('my_cache_item', $data, 'cache');
}

// Use $data
        
More info: http://dgo.to/a/cache_set

Cache back-ends

Database (default)

  • Easy - configured by default
  • Does not add any complexity to the server stack
  • Can work totally fine for most of smaller sites
  • Query cache helps
  • Permanent
  • SLOW!
  • Relys on DB, which is already under pressure on high-traffic sites
  • Uses disk (except query cache for reads)

APC user cache

  • Easy to configure
  • Fast as hell!!!
  • No additional complexity on server stack (assuming you already use APC)
  • Uses RAM
  • Non-permanent
  • Non centralized - each server will have it's own cache
  • Syncronzation problems
  • You cannot clear it with drush
More info: drupal.org/project/apc.

Memcached

  • Fast!
  • Uses RAM
  • Easy to administer
  • Easy to set memory pools (distribution)
  • All web servers will use same cache - easy to synchronize
  • Non-permanent
  • Adds another level of complexity to your server stack
  • You have to compile/install PHP extension
More info: drupal.org/project/memcache.

Redis

  • Fast!!
  • Uses RAM/Disk combination
  • All web servers will use same cache
  • Permanent
  • You can use PHP library if you're not able to install PHP extension
  • Delayed writes to disk (configurable)
  • Adds another level of complexity to your server stack
  • A bit more complex to administer than Memcached
  • Sooner or later will use disk
More info: drupal.org/project/redis.

MongoDB

  • Fast!
  • All web servers will use same cache
  • Permanent
  • Can be used to store data (nodes, fields, ...)
  • Adds another level of complexity to your server stack
  • Probbably the most complext to administer
  • You have to compile/install PHP extension
More info: drupal.org/project/mongodb.

Configuration (settings.php)

 
# Memcache
include_once('./includes/cache.inc');
include_once('./sites/all/modules/memcache/memcache.inc');
$conf['memcache_key_prefix'] = 'drupal';

# Redis
$conf['cache_backends'][] = 'sites/all/modules/redis/redis.autoload.inc';
$conf['redis_client_interface'] = 'PhpRedis';  //Library or extension

# APC
$conf['cache_backends'][] = 'sites/all/modules/apc/drupal_apc_cache.inc';

# Mongo
$conf['cache_backends'][] = 'sites/all/modules/mongodb/mongodb_cache/mongodb_cache.inc';


$conf['cache_default_class'] = 'MemCacheDrupal';
//$conf['cache_default_class'] = 'Redis_Cache';
//$conf['cache_default_class'] = 'DrupalAPCCache';
//$conf['cache_default_class'] = 'DrupalMongoDBCache';

$conf['cache_cache_form'] = 'Redis_Cache';
$conf['cache_cache_menu'] = 'DrupalMongoDBCache';
// etc....
        

Performance overview

Page & block cache

Views and panels cache.

How to optimize/cache your page?

Enable query log

Enable profiler

Cache parts of page (blocks, panes)

Page cache (or Varnish)


Thank you!!
janez@janezurevc.name
@slashrsm