Static caching

Public
</> CopyGet raw version
php
  1. <?php
  2. function example_function() {
  3. // Use the advanced drupal_static() pattern, since this is called very often.
  4. static $drupal_static_fast;
  5. if (!isset($drupal_static_fast)) {
  6. $drupal_static_fast = &drupal_static(__FUNCTION__);
  7. }
  8.  
  9. // Get our static data.
  10. $example_data = &$drupal_static_fast;
  11.  
  12. if (!isset($example_data)) {
  13. if ($cache = cache_get('example_data')) {
  14. // Load the data straight from the cache.
  15. $example_data = $cache->data;
  16. }
  17. else {
  18. // We haven't calculated this yet so load it now.
  19. $example_data = example_data_load();
  20. // Store our calculated data in the cache.
  21. cache_set('example_data', $example_data);
  22. }
  23. }
  24.  
  25. return $example_data;
  26. }