Created
June 19, 2013 05:06
-
-
Save anonymous/5811802 to your computer and use it in GitHub Desktop.
A simple example to illustrate how data can be cached in a Drupal application.
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 | |
// Fetch data from the database | |
function my_mod_fetch_from_db() { | |
// Remember the number of dimensions of the static cache depends | |
// on the number of parameters that this function can take. | |
$cache = &drupal_static(__FUNCTION__, array()); | |
// Static cache miss | |
if (!isset($cache) { | |
// Check persistent cache | |
$cached_item = cache_get(__FUNCTION__, 'cache'); | |
// Persistent cache miss | |
if (!$cached_item) { | |
// Fill 'er up from the DB. | |
// Save in the static cache. | |
$cache = db_query('SELECT id, data from {my_mod_table};')->fetchAllKeyed(0, 1); | |
// Save in the persistent cache. | |
cache_set(__FUNCTION__, 'cache', $cache, CACHE_PERMANENT); | |
} | |
} | |
// Return | |
return $cache; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment