Drupal Static and Extra caching
This article really does a great job teaching the concepts:
http://www.lullabot.com/blog/article/beginners-guide-caching-data-drupal-7
https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal...
The code snippet excerpts
To cache for just a page load. So repeated calls to the same function that would return the same results are faster.
<?php
function my_module_function() {
$this_function_returns = &drupal_static(__FUNCTION__);
if (!isset($this_function_returns)) {
// Do heavy lifting here, and populate $this_function_returns
$this_function_returns = ????
}
return $this_function_returns;
}
?>
Database caching to make the heavy lifting last longer
<?php
function my_module_function() {
$this_function_returns = &drupal_static(__FUNCTION__);
if (!isset($this_function_returns)) {
if ($cache = cache_get('UNIQUE_CACHE_ID')) {
$this_function_returns = $cache->data;
}
else {
// Do heavy lifting and populate $this_function_returns
$this_function_returns = ???
cache_set('UNIQUE_CACHE_ID', $this_function_returns, 'cache');
}
}
return $this_function_returns;
}
?>