1

I read this interesting article on Drupal caching, but I'm not sure which caching method I should be using.

My code is implementing a menu callback, and returning a renderable array.
What is the difference between method #1 and method #2?

Please see the question embedded as a comment in method#2

// Method #1.
function my_module_menucallback() {
  $render_array = &drupal_static(__FUNCTION__);
  if (!isset($my_data)) {
    if ($cache = cache_get('my_module_data')) {
      $render_array = $cache->data;
    }
    else {
      // Do your expensive calculations here, and populate $render_array
      // with the correct stuff..
      cache_set('my_module_data', $render_array, 'cache');
    }
  }
  return $render_array;
}

// Method #2.
function my_module_menucallback() {
  // ** Do the calculations and population of $render_array that would occur here
  // get called repeatedly if this method of caching is used??? **

  $render_array ['my_content'] = array(
    '#cache' => array(
      'cid' => 'my_module_data',
      'bin' => 'cache',
      'expire' => time() + 360,
      ),
    // Other element properties go here...
    );
  return $render_array;
}

Please ignore the time specified, as this is just sample code.

1

So I tested and there is a significant difference between the two that I failed to understand originally. Calculations not surrounded with ...

$render_array = &drupal_static(__FUNCTION__);
  if (!isset($my_data)) {
    if ($cache = cache_get('my_module_data')) {
      $render_array = $cache->data;
    }
    else {
      // Do your expensive calculations here
      cache_set('my_module_data', $render_array, 'cache');
    }
  }

...are executed every time the menu callback function is called (every time user visits that page).

Method #2 caches only the final, rendered output. So if your calculations/processing in the method has changed the output value and you've cached the rendered output, you will not see the updated value until the cache is cleared.

| improve this answer | |
0

I think, but I'm not sure (we shall do some benchmark for it), that the second method is a little bit slower than the first one.

With the first method, you call directly the lower functions to set the cache and get the cache. With the second method, according to the documentation and code, severals checks are done before calling cache_set() and cache_get(). In the other hand, you already rendered the code.

I think the use of one or another method depends on what you need, raw data or directly rendered code.

| improve this answer | |
  • Sorry Yvan, I clarified the question. The time isn't a factor as I was only posting sample code. I'm looking to see if there is any difference for the two methods in terms of performance. – nmc Aug 30 '11 at 15:38
  • Ok, I changed my answer :) – yvan Aug 30 '11 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.