Caching in Drupal

The $variable[‘messages’] is not always available and, thus, it fails in situations where you need to have messages be available anytime and everywhere in the code during single request. As solution to my problem, I utilized Drupal Caching

In search for solution, I started by creating the following function:

function get_messages(){
    $status_messages = array();
    //loop through each message type
        foreach (drupal_get_messages(null, false) as $type => $messages) {
               foreach($messages as $key => $message){
                   $status_messages[$type]['enabled'] = true;
                   $status_messages[$type]['messages'] = array(
                       'message' => $message
                   );
               }
        }
return $status_messages;
}

This works well, however. The messages don’t get to be reset. As a result, messages build up from previous requests something that is unacceptable. We could try to reset messages by calling Drupal API function drupal_get_messages with parameter ‘true’, but then the messages got to be reset on the first time execution of our function – get_messages() and all other times it returns no messages. My challange

I had two ideas on how to approach this problem:

  1. Keep & Reset at the End of Request

    One option is to clear messages at the last point of the request

  2. Cache messages and Reset at the Start of Request

    At first time calling drupal_get_messages(), we would cache the messages and then reset at the same time by calling the function with parameter “TRUE”

For the first solution, it wasn’t clear where exactly is the last point of request good for reset messages. I was also concerned having to touch code in two places to solve single problem
For the second solution, I liked it because it would kill two birds with one stone. We would solve our problem and improve the performance by caching messages and, thus, minimizing execution.

To solve this problem, I turn to Drupal caching mechanism.

Cache During One Request

Drupal provides reference to static storage that is permanent during single execution. To utilize it as cache, here is common pattern of the function as follows:

function some_function(){
 $my_data = &drupal_static(__FUNCTION__);
  if (!isset($my_data)) {
    // Do your expensive calculations here, and populate $my_data
  }
  return $my_data;
}

Here, in the second line we got the static reference with name of our function(i.e. __FUNCTION__). For the first time, this reference will not be set, so we populate it with the data that we like to cache. For all other executions, the static reference is holding our data that we return

Here, is how our message function looks with the Drupal cache mechanism:

function get_messages(){
    $status_messages = &drupal_static(__FUNCTION__);

    if(!isset($status_messages)){
        //loop through each message type
        foreach (drupal_get_messages(null, true) as $type => $messages) {
               foreach($messages as $key => $message){
                   $status_messages[$type]['enabled'] = true;
                   $status_messages[$type]['messages'] = array(
                       'message' => $message
                   );
               }
        }
     }
     return $status_messages;
}

Here, we get the static reference first and then check if its already set. if not, we retrieve the messages and reset at the same time by calling Drupal API function drupal_get_messages with “true” parameter. As result, we reset the messages that solves our problem and, in addition, our performance is improved due to caching

Cache Over Multiple Requests

Perhaps, there is going to be time when you will need to cache over multiple requests instead only one. To do so, Drupal provides cache functions(i.e. cache_get, cache_set, cache_clear_all) that store cache into Database. There is an excellent article on caching in including Drupal cache functions at Beginners Guide Caching Data Drupal 7

References

  • https://www.lullabot.com/blog/article/beginners-guide-caching-data-drupal-7
  • https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_static/7

Leave a Reply

Your email address will not be published. Required fields are marked *