Skip to content

Instantly share code, notes, and snippets.

@gabesullice
Last active January 27, 2016 19:42
Show Gist options
  • Select an option

  • Save gabesullice/98de3fd1ae4f097b5c24 to your computer and use it in GitHub Desktop.

Select an option

Save gabesullice/98de3fd1ae4f097b5c24 to your computer and use it in GitHub Desktop.
<?php
function get($thing, $reset = FALSE) {
return cache_anything(__FUNC__, 'something_expensive', array($thing), $reset);
}
function get_something_else($other_thing, $reset = FALSE) {
$slow = function ($other_thing) {
// slow query with $other_thing
};
return cache_anything(__FUNC__, $slow, array($other_thing), $reset);
}
function cache_anything(string $cache_key, callable $func, array $args, bool $reset = FALSE) {
$key = md5($cache_key . serialize($args));
$cache = &drupal_static($key, NULL, $reset)
if (!reset) $cache = call_user_func_array($func, $args);
return $cache;
}
function something_expensive($thing) {
// the slow, it burns!!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment