Skip to content

Instantly share code, notes, and snippets.

@thbourlove
Created January 21, 2014 10:16
Show Gist options
  • Save thbourlove/8537547 to your computer and use it in GitHub Desktop.
Save thbourlove/8537547 to your computer and use it in GitHub Desktop.
<?php
$funcache = function ($callable) {
static $backend = [];
return function () use ($callable, &$backend) {
$args = func_get_args();
$key = json_encode($args);
if (array_key_exists($key, $backend)) {
return $backend[$key];
}
return $backend[$key] = call_user_func_array($callable, func_get_args());
};
};
$fab = $funcache(function ($num) {
sleep(1);
return $num;
});
echo $fab(1);
echo $fab(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment