Last active
April 27, 2016 11:16
-
-
Save xthiago/9566cb63dd67cdfa237d15da314c0e30 to your computer and use it in GitHub Desktop.
Drupal 7 - example with static and regular cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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