I have been using this snippet lately here and there, but after re-thinking , it seems like it could be a good example of over-engineered code:
<?php
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;
}
?>
Is this truly an optimal way to cache things? I am not clear on the low-ends of it all but it seems the drupal_static call is overhead since the static call would get only called once before the data would be cached. Maybe I am seeing it wrong, and rather the cached variable would be set static for the whole request, and static access is faster than cache access?
I am also not clear if the cache is global for all users or on a per user basis. I would imagine the cache is global, but please correct me if I am wrong!.