Last active
January 27, 2016 19:42
-
-
Save gabesullice/98de3fd1ae4f097b5c24 to your computer and use it in GitHub Desktop.
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 | |
| 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