Skip to content

Instantly share code, notes, and snippets.

@pifantastic
Created June 21, 2010 06:03
Show Gist options
  • Select an option

  • Save pifantastic/446476 to your computer and use it in GitHub Desktop.

Select an option

Save pifantastic/446476 to your computer and use it in GitHub Desktop.
<?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