Skip to content

Instantly share code, notes, and snippets.

@xthiago
Last active April 27, 2016 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xthiago/9566cb63dd67cdfa237d15da314c0e30 to your computer and use it in GitHub Desktop.
Save xthiago/9566cb63dd67cdfa237d15da314c0e30 to your computer and use it in GitHub Desktop.
Drupal 7 - example with static and regular cache
<?php
function my_module_function() {
$my_data = &drupal_static(__FUNCTION__);
if (!empty($my_data)) {
return $my_data;
}
if ($cache = cache_get('my_module_data')) {
$my_data = $cache->data;
return $my_data;
}
// Do your expensive calculations here and populate $my_data
$my_data = expensive_calculations();
// Store the result of calculation in cache.
$cache_expiration = date('U', strtotime('1 hour'));
cache_set('my_module_data', $my_data, 'cache', $cache_expiration);
return $my_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment