Amitav Roy

Blog on web and travel

Drupal cache_get and cache_set

Posted on July 2019

Drupal makes me happy

Drupal cache is very important when you are working on a site which will have a lot of visitors daily and performance becomes a crucial part. In this tutorial, I will demonstrate how you can use the cache_set() drupal function to save something in the cache and retrieve that on user request instead of unnecessary processing if the content has not changed.

Let’s start with this piece of code:

function learn_demoBlock() {
      
    $currNode = node_load(1);
    $currNodeLang = $currNode->language;
    $currNodeTitle = $currNode->title;
    $currNodeTags = $currNode->field_tags;
      
    $output = "";
      
    $output .= "Title of the node is: <strong>" . $currNodeTitle . "</strong>
 and it contains ";
      
    foreach ($currNodeTags[$currNodeLang] as $term) {
        $output .= $term['taxonomy_term']->name . ", ";
    }
      
    $output .= " tags.";
    return $output;    
}

In this function, I have loaded a single node with node id 1. And the rest is simple – building the output with the node title and the tags inside the node. And then I am calling this function inside a block which I can place anywhere I want. Here is the code:

<?php print learn_demoBlock(); ?>

Now, this is a piece of code that is not going to change, but we would definitely like to have the dynamic nature of the block. Like the featured article on my website which I don’t change very often (you can check the tutorial for this here: Making settings form in Drupal using forms API and a bit more). But I prefer a small text box where I can just change the node id and that particular article would be displayed as a featured article. So, we can easily store the final HTML of the output in cache table and simply do away with the node load and all other processing when the page is loaded.

function learn_demoBlock() {
      
    $cache = cache_get("demoblock","cache_block");
    if ($cache) {
        print "From cache";
        return $cache->data;
    }
      
    $currNode = node_load(1);
    $currNodeLang = $currNode->language;
    $currNodeTitle = $currNode->title;
    $currNodeTags = $currNode->field_tags;
      
    $output = "";
      
    $output .= "Title of the node is: <strong>" . $currNodeTitle . "</strong>
 and it contains ";
      
    foreach ($currNodeTags[$currNodeLang] as $term) {
        $output .= $term['taxonomy_term']->name . ", ";
    }
      
    $output .= " tags.";
    cache_set("demoblock",$output,"cache_block");
    print "Not from cache";
    return $output;
  
}

The code is almost the same except for few lines at the top from 3 to 7 which uses Drupal function cache_get() and checks if it already exists. This is the best thing. If the cache data is found, then it will instantly return the data from the cache table and all the code is not required to be executed. And if the cache data is not found then the code will be executed and then the cache will be set using drupal function cache_set() in line number 23. So, the next time you load the page, the output is already in cache and so your result will come from the cache.

For visible result, I have printed two different strings and also you can check the cache_block table in the database for the row entry. And I have attached a zip file of the code in case you have any problem.