The basics

function my_module_function() { $my_data = &drupal_static(__FUNCTION__); if (!isset($my_data)) { // Do your expensive calculations here, and populate $my_data // with the correct stuff.. } return $my_data; }

drupal_static()

drupal_static()

drupal_static()

Making it stick: Drupal's cache functions

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

Keeping up to date

Controlling where cached data is stored

hook_schema()

function mymodule_schema() { $schema['cache_mymodule'] = drupal_get_schema_unprocessed('system', 'cache'); return $schema; }

drupal_get_schema_unprocessed()

Advanced caching with renderable content

drupal_render()

drupal_render()

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

cache_get()

cache_set()

A few caveats

Go west, young Drupaler!