Caching data in Drupal 7

For a project at work we're working with a bunch of api's for this one application we produce. Through this experience i've had to create methods of caching for it so that we're not constantly hitting API endpoints when we don't need to necessarily.

First in my module i've created a my_module.install file which will include a hook_schema() implementation. This is where we will be creating the new cache table.

<?php 
    function my_module_schema() {
        $schema = array();
        $schema['cache_my_module'] = drupal_get_schema_unprocessed('system', 'cache');
        return $schema;
    }
?>

Note: remember to replace "my_module" with what ever your module is called. For example, example_cache.module or example_cache_schema() for the hook implementation.

To explain the above code it's pretty simple. First we define the $schema variable as an empty array that we will populate with our database schema. In this case we're creating a cache table and we can rely on Drupal's default format for this by calling drupal_get_schema_unprocessed($module, $table). The arguments we're passing in refer to which module and which table we're trying to get the unprocessed schema structure from.

Now, when you go to enable your module it will install this new cache table. A couple things to note here, first since it's a cache table go with the standard cache prefix drupal uses. Follow this up with your module name to further namespace this and not have it conflict with other modules.

Storing values in your cache table

Now this is where were going to obtain the data we want to store in the table.

<?php
    function my_module_get_api_response() {
          $response = array();
          $cache = cache_get(__FUNCTION__, 'cache_my_module');
          if($cache){
              $response = $cache->data;
          } else {
              // get the remote data from the api
            // this is obmitted since the process might be different
 
            cache_set(__FUNCTION__, $response, 'cache_my_module');
          }
          return $response;              
    }
?>

Breaking this down we have a empty $response array for use later in the function. Next we have the initial cache_get() here we try to get the value from the cache table. If it exists it will be unserialized and saved to $repsonse. Otherwise, we jump in to grabbing a new copy of the data from the remote source, and where we would then populate $response before saving it to the cache. And the final bit is the cache_set($cid, $data, $table) which is broken down as a unique key for the cache entry, the data that will be auto serialized, and the table you want to cache it too. If you obmit the table it will be cached to the default cache table.

If you want to use this data now in a different function you can call it using this previous function and if it's avaliable in the cache it will be used from there and if not it will be grabbed and stored in the cache.

This might not be the best instructions but it's my experience over the last few weeks. Any tips or suggestions are welcome.