Some computations take a while to process. If you have an instance where this is the case, or if you wish to optimize your PHP code and not run a computation every page load, caching may be the answer. Drupal has a nice caching system that it utilizes for many of its operations. This includes menus, blocks, and views. By default, it uses the SQL database for cache, but can easily be set up to use alternatives like APC or memcache. When a developer wants to also cache his or her own operations, this is quite feasible as well. In this article, I will demonstrate how to set up a custom cache using Drupal 7's Cache API.
There are two methods of caching data in Drupal. The first and simplest method is to store your data in an existing cache bin. This is a good method if you are only dealing with one piece of data. If you are dealing with multiple pieces of data, like data that's unique to each user, then you will want the second method, which creates your own cache bin.
Let's go over the simple method first. First, perform all your calculations and get them into a variable. Let's say our data is stored in $data. Next, come up with a unique ID for your data, say 'my_data'. Now, all we have to do is store our data in an existing cache bin.
cache_set('my_data', $data);
Whenever we want to retrieve this data, we would use this:
cache_get('my_data');
See how easy that is? Again, this only works if you don't need many versions of the cached data. If you need many versions, then you'll need to use the second method: a custom cache bin.
The custom cache bin method needs more than just a .module file, so you may want to create a separate custom module just for it. Let's call ours “my_custom_module”. The first step we need to do is create the actual custom cache bin. By default, the cache is stored in the database, so we need to create a custom cache table within my_custom_module.install:
/**
* Implementation of hook_schema().
*/
function my_custom_module_schema() {
$schema = array();
$schema['cache_my_custom_module'] = drupal_get_schema_unprocessed('system', 'cache');
return $schema;
}
Drupal uses the “cache_” prefix for its cache tables, so it's a good idea to follow suit. Next, we need to register our cache table when the cache gets cleared, so in my_custom_module.module we add this code:
/**
* Implements hook_flush_caches().
*/
function my_custom_module_flush_caches() {
return array('cache_my_custom_module');
}
Now that our custom cache bin is set up, we can store and retrieve data from it. Since we are using a custom cache bin because of the amount of data we'll be caching, we'll probably want to relate the data to some sort of unique ID. In this example, we relate it using $id.
cache_set('my_data_' . $id, $data, 'cache_my_custom_module');
cache_get('my_data_' . $id, 'cache_my_custom_module');
Here we have a basic implementation of Drupal 7's Cache API. We've seen how we can either use an existing cache bin or create our own, depending on our needs. Since this was just an introduction, we haven't gone over cache expiration or other advanced techniques. I'll go over those in my next post, but this should be enough to get you started.
Need a fresh perspective on a tough project?
Let’s talk about how RDG can help.