Created
June 21, 2010 06:03
-
-
Save pifantastic/446476 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 | |
| /** | |
| * Call a function but memoize the results. | |
| * | |
| * @param $callback | |
| * The function to call. | |
| * @param $arguments | |
| * The arguments to pass to callback | |
| */ | |
| function memoize($callback) { | |
| $args = array_slice(func_get_args(), 1); | |
| // Serializing is slow but allows us to accept any arguments | |
| $key = 'memoize-'.md5(serialize($callback).serialize($args)); | |
| $item = cache_get($key); | |
| if ($item) { | |
| return $item->data; | |
| } | |
| $result = call_user_func_array($callback, $args); | |
| cache_set($key, $result); | |
| return $result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment