Static Caching in Drupal

If you’re a PHP veteran, then you know static caching is a relatively simple thing to do in PHP, but it can result in inconsistent behavior and sloppy code, such as adding an extra parameter to a function for the sole purpose of resetting that function’s internal cache.

Drupal’s static caching system improves upon the old way by providing a simple and consistent way for creating your own cache store and resetting cache stores set by other systems within Drupal.  The entire functionality of this system is wrapped up in the drupal_static() function, with a side function, drupal_static_reset(), for resetting any existing cache store.

Using static caching in Drupal 7 is a little scary at first, especially if you’ve never worried too much about references or magic constants.  Please be aware that the following example works only in Drupal 7.  Prior versions of Drupal do not provide a static caching system.  Here’s how static caching works:


function bark($who) {
$barked = &drupal_static(__FUNCTION__, array());

$barked[] = $who;


The ampersand (&) in front of the function call indicates that the function drupal_static() returns a reference variable (recall that PHP is by default a pass-by-value language).  The magic constant __FUNCTION__ is the name of the calling function (bark, in this case).  The function’s name, “bark”, will be used as the key that points to the data you are storing.  There’s no need to check if $barked has been set because drupal_static() will handle that for you.

Static caching is useful for storing objects that are expensive to generate.  For example, Drupal uses a tree data structure to represent menus.  Menu trees are expensive to generate since Drupal has to spend time determining parent-child relationships, ordering items by weight, and running hooks.  That, on top of the database queries needed to simply retrieve the menu information.  So it would make sense, then, that Drupal would generate the menu only once during a request and stuff it away in the static cache.  Just as important is the fact that plenty of other modules rely on the menu system in order to function, and so caching is not only convenient but absolutely necessary for exposing menu data.

Caching should be used responsibly, however.  Caching anything and everything is naive and can do more bad than good.  Recall that PHP is a garbage-collected language.  Without getting into the gritty details of how it works, it’s enough to say that static data will NOT be reclaimed under the regular rules of garbage collection.  It stays around– even outside the function scope– until you reset it or the request ends.   Since websites can serve many visitors at once, it’s not hard to see why this can become a big problem very quickly.

In summary, remember that caching (of any kind, not just static caching) is to be done in a responsible manner.  Overzealous usage of caching floods memory which can crash high traffic websites.  Remember also that caching should be used for large data structures that are expensive to generate.  Lastly, use caching only if you know for sure that what you cache will be needed again (e.g., menu data in Drupal).  There’s no point caching a list of names or addresses or colors if that list will never be used again.  That would be like saving your old car tires after you put new ones on.


Comments are closed.