Getting Started With Cache Functions In Drupal 7

30th March 2015

When generating markup in Drupal you'll often want to store the output in a cache instead of regenerating it every time. This is especially important for potentially expensive rendering tasks that don't change between page requests. Drupal 7 comes with a cache system that can be taken advantage of with the cache_get() and cache_set() functions. There is also a third function called drupal_static() that also fills in gaps between these two functions.

Caching can be used for all sorts of situations, and because it is built into Drupal you can also rely on the cache management functionality that Drupal has. The basic rule of thumb is never to regenerate anything that can be cached and returned instead. This is especially important if generating the output is a time consuming and expensive task. A good example of output that can be cached easily is a news feed that is updated once day. In this case the output only needs to be regenerated when the content is updated and can be kept for a while before needing to be regenerated again. You can also cache more complicated things like search results or the facets on a search page, both of these things can be time consuming to generate and yet don't tend to change much from page request to the next.

Static Cache In Drupal

The drupal_static() function works by creating a global cache that persists for the duration of the page request and is used all over Drupal core. This is useful if you are calling the same function several times during the request but not expecting the output to change. A prime example of this in action is when loading a node that needs to be displayed in different areas of the page, not uncommon if the node appears on the page and in a block of related links. Obviously, once Drupal has loaded a node there is little gained from loading it a second time and so the drupal_static() function is used to store that the data in the static cache and retrieve it again if needed. Here is a really simple example of drupal_static() in action.

  1. function my_module_load() {
  2. static $my_module_data;
  3. $my_module_data = &drupal_static(__FUNCTION__);
  4. if (empty($my_module_data)) {
  5. // Run expensive actions and add the data to the $my_module_data variable.
  6. }
  7. return $my_module_data;
  8. }

The __FUNCTION__ seen in the above code is a PHP magic constant and represents the function name. The drupal_static() function takes a single required parameter, which is the name of the cache item to store. In the above code the __FUNCTION__ parameter can be replaced with 'my_module_load' to maintain the same functionality. If you want to be more specific with this tag you can concatenate this with arguments so that you are only storing and retrieving specific items in the cache.

  1. function my_module_load($id) {
  2. static $my_module_data;
  3. $my_module_data = &drupal_static(__FUNCTION__ . $id);
  4. if (empty($my_module_data)) {
  5. // Run expensive actions and add the data to the $my_module_data variable.
  6. }
  7. return $my_module_data;
  8. }

Remember that you can call this function multiple times during the page request, but you'll always receive the same return value. It's only on subsequent page requests that the value will change.

If you need to clear out the static cache you can run the drupal_static_reset() function, passing it the name of the static cache that is to be cleared. If you used the __FUNCTION__ magic constant you would exchange this for the name of the function you defined the cache in.

  1. drupal_static_reset('my_module_load');

A More Permanent Cache

Static caching is fine, but real improvements in speed can be seen when you allow the cached content to be saved to disk (or persistent cache) and read on subsequent page views. This is where cache_set() and cache_get() come into play.

The cache_set() function is the most complex of these two functions and is used (as the name suggests) to write any data to the cache. This function takes a number of parameters:

  • $cid : (Required) The first parameter in the cache_set() function is the name of the cache, which is used to retrieve the cache again.
  • $data : (Required) The second parameter is the data that you want to cache. It's important to note that complex data types (like arrays and objects) will be automatically serialized before they are stored. Any serialized data will be unserialized when returned from the cache, but you'll need to ensure that any objects you have contain __sleep() and __wake() methods to put your objects back to their original state.
  • $bin : There are a number of caches available in Drupal, which are referred to as 'bins'. The default behaviour of this function is to write the cache to the main cache bin, which by default is the table called 'cache' in the Drupal database. This can be altered to a number of different bins, but you'll probably either use the default 'cache' or the 'cache_block' for block caching.
  • $expire : This tells Drupal how long to keep the cache data around for.
    • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.
    • CACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.
    • A Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.

You would typically use the cache_set() function like this.

cache_set('my_module_cache', $data, 'cache', CACHE_TEMPORARY);

The cache_get() function returns the contents of the cache and takes two parameters:

  • $cid : (Required) This is the name of the cache, which should be the same as the value you used when using cache_set().
  • $bin : This is an optional parameter that allows you to select a different cache bin to get the cache from. Again, this should be the same cache bin that you used to store the cache in cache_set().

The cache_get() function doesn't only contain the data you stored in the cache, it is an object that contains both the data you stored and some other information about that data. Here is a typical example of a cache object:

  1. stdClass Object
  2. (
  3. [cid] => my_module_cache
  4. [data] => text
  5. [created] => 1427730490
  6. [expire] => -1
  7. [serialized] => 0
  8. )

The important part here is the 'data' parameter, which you should access to get your stored cache value. The other parameters tell you the cache ID (cid) when the cache was created (created) if the cache is currently expired (expire) and if the original value was serialized (serialized).

Here is a simple example of using the cache_get() function.

  1. $cache = cache_get($cache_id, 'cache');
  2. $data = $cache->data;

Of course, it's best to use these two functions together. Here is an example of a simple way in which these functions can be used to create a cached resource for complex output. I've created some simple markup here to show it in action.

  1. function my_module_create_markup() {
  2.  
  3. // Retrieve from cache backend.
  4. if ($cache = cache_get('my_module_cache', 'cache')) {
  5. $markup = $cache->data;
  6. }
  7.  
  8. // If we don't have a cached version already then build the markup.
  9. if (empty($markup)) {
  10. $markup = '';
  11.  
  12. // Do stuff and add it to the markup variable.
  13. $markup .= date('c');
  14.  
  15. for ($i = 0; $i
  16.  
  17. <p>When putting all of these functions together I normally include a parameter that I can use to turn on or off the cache functionality via a single config setting. This makes development much easier and allows you to automatically start using the cache on the live system once the module is active. It is also good practice to separate out the cache ID into a variable, which allows the the possibility of creating a context based cache. This means that if you want to use user input to create the cache then you don't have to modify the code too much. I also tend to use the drupal_static() function in addition to the cache functions as this provides further resilience to multiple calls to the same function.</p>
  18.  
  19. <p>Here is an example of a fully featured cache function.</p>
  20.  
  21. <code class="php">function my_module_create_markup() {
  22. // Initialise the static
  23. static $markup;
  24.  
  25. // Check if a cache should be used.
  26. $use_cache = variable_get('my_module_use_cache', TRUE);
  27.  
  28. // Set a cache ID to store and retrieve the cache.
  29. $cache_id = 'my_module_cache';
  30.  
  31. if ($use_cache) {
  32.  
  33. // Try to retrieve from static cache.
  34. $markup = &drupal_static($cache_id);
  35.  
  36. if (empty($markup)) {
  37. // Retrieve from cache backend.
  38. if ($cache = cache_get($cache_id, 'cache')) {
  39. $markup = $cache->data;
  40. }
  41. }
  42. }
  43.  
  44. // If we don't have a cached version already then build the markup.
  45. if (empty($markup)) {
  46.  
  47. // Initialize the markup variable.
  48. $markup = '';
  49.  
  50. // Do stuff and add it to the markup variable.
  51. $markup .= date('c');
  52. for ($i = 0; $i
  53.  
  54. <p>Drupal will automatically clear the cached data when it has expired, but you can force the cache to be cleared by using the <strong>cache_clear_all()</strong> function. This function takes three optional parameters as it is possible to call it with no parameters to clear all the caches in Drupal.</p>
  55.  
  56. <ul><li><strong>$cid</strong> : This is the cache ID of the cache you wish to clear. Essentially, the first parameter in your cache_set() call. You can also pass an array of cache IDs in order to clear multiple caches. The $wildcard argument will be ignored if set to NULL.</li>
  57. <li><strong>$bin</strong> : This is the cache bin that you used to store your cache in. This is required if you supplied the $cid parameter.</li>
  58. <li><strong>$wildcard</strong> : If TRUE, the $cid argument must contain a string value and cache IDs starting with $cid are deleted in addition to the exact cache ID specified by $cid. If $wildcard is TRUE and $cid is '*', the entire cache is emptied.</li>
  59. </ul><p>You would use this function in other places in your code when you knew that a certain change would effect the output of the cache. For example, you might have a section that shows certain nodes, in which case you would need to add a hook_node_presave() hook so that you can clear out the cache of that section if the user has saved a node. To clear the cache created in the above code you would use the following.</p>
  60.  
  61. <code class="php">cache_clear_all('my_module_cache', 'cache');

Obviously there can be a little complexity involved in caching in Drupal, but if you design your code around the cache_set() and cache_get() functions then you can create responsive sites that store repeating data without having to regenerate everything. It is possible to extend the available cache bins in Drupal, but you are strongly encouraged to use the cache_set() and cache_get() functions to interact with all caches. Even the Views cache (which creates cache_views and cache_views_data as cache bins in Drupal) still uses cache_get() to get information out of the cache.

It is also possible to alter the cache mechanisms in Drupal to write the cache to different storage mechanisms. This is accomplished via the use of modules like APC, Redis and Memcache, but also require that those systems exist before hand.

Comments

Permalink
The article presented great in serious manner. Well, this is what I'm talking about.

logan zachary (Mon, 04/06/2015 - 07:53)

Add new comment

The content of this field is kept private and will not be shown publicly.