Skip to content

Instantly share code, notes, and snippets.

@msankhala
Created July 30, 2014 07:55
Show Gist options
  • Save msankhala/45e0689d1e988dd085c8 to your computer and use it in GitHub Desktop.
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…
//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