Created
July 30, 2014 07:55
-
-
Save msankhala/45e0689d1e988dd085c8 to your computer and use it in GitHub Desktop.
When values don’t change between requests (unlike static variables), Drupal’s caching system is a great, simple way to reduce the workload on your site and provide a flexible way to configure the storage duration of this data. Using just our handy cache_get() and cache_set() functions, we can implement an elegant solution to increasing performan…
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
//First, let’s analyze the cache_set() parameters - | |
<?php | |
function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) { | |
/* ... */ | |
} | |
?> | |
//Example #1 (using a simple cache) | |
<?php | |
$value = cache_get('cache_key'); | |
if (FALSE == $value) { | |
// generate $value | |
cache_set('cache_key', $value); | |
} | |
?> | |
//Example #2 (creating your own cache) | |
<?php | |
/* @file my_module.install */ | |
function my_module_schema() { | |
$schema['cache_my_cache'] = drupal_get_schema_unprocessed('system', 'cache'); | |
$schema['cache_my_cache']['description'] = 'A cache table for my module.'; | |
return $schema; | |
} | |
?> | |
<?php | |
/* @file my_module.module */ | |
function my_module_flush_caches() { | |
return array('cache_my_cache'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment