4

I'm making a simple tool to cache function results

It look like:

global $function_results;
$function_results = array();

function getMembers($conditions, $default = array('order' => 'name', array('abc', 'def'))){

    //****Help need from here******
    //make unique id from parameters value and function name
    //ex: $uid;
    //****to here******

    global $function_results;
    if(isset($function_results[$uid])) return $function_results[$uid];
    else{
        //do function logic...
    }
}

(the function and its parameters are just an example)

Any suggestions?

4
  • I need help to make unique id from parameters value and function name
    – StoneHeart
    Aug 22, 2010 at 6:12
  • It's hard to say how to make a UID when we don't know what type of variables you are expecting. Can you be more specific. Aug 22, 2010 at 6:14
  • parameter value of function can be anything: object, array, string, number ....
    – StoneHeart
    Aug 22, 2010 at 6:15
  • Look into memoization ;) Aug 22, 2010 at 6:31

4 Answers 4

10

Here is your missing code:

$numargs = func_num_args();
$arg_list = func_get_args();
$md5this = '';
for ($i = 0; $i < $numargs; $i++) {
    $md5this .= $i.':'.serialize($arg_list[$i]).';';
}
$uid = md5($md5this);

For your reference: http://php.net/manual/en/function.func-get-args.php

4
  • $arg_list[$i] will return "Array" string if it is array.
    – StoneHeart
    Aug 22, 2010 at 6:27
  • @StoneHeart, added serialize. Its complete now.
    – Alec Smart
    Aug 22, 2010 at 6:29
  • 1
    Why we don't use $arg_list = func_get_args(); $uid = serialize($arg_list);
    – StoneHeart
    Aug 22, 2010 at 6:39
  • 3
    @StoneHeart you could do that too. But suppose two parameters have '' as output then it will equal to another output which has '' as single parameter.
    – Alec Smart
    Aug 22, 2010 at 8:14
5

Here is a generic memoization solution that is compatible with PHP 5.2.x.

The call_memoized function:

  1. Generates a unique cache key based on the function name and arguments.
  2. If the cache is empty, invokes the function and updates the cache with the result.
  3. Returns the cached value.

Instead of using call_memoized directly, use the provided memoize function, which returns a new version of the given function that will automatically cache results from the same combination of parameters.

See example below.

Enjoy!

global $MEMO_CACHE;
$MEMO_CACHE = array();

/** Returns the result of the function with the given arguments. 
 * Invokes the function only once, thereafter returns the result 
 * cached by a key based on the function name and arguments. */
function call_memoized($fun, $args=array()) {
    global $MEMO_CACHE;

    // generate a cache key based on the function name and arguments
    $uid = md5(
        implode("|", array_merge((array)$fun, array_map(
            "serialize",
            $args)
        ))
    );

    // if there result hasn't been cached, call the function 
    // and update the cache with the result.
    if(!array_key_exists($uid, $MEMO_CACHE)) {
        $MEMO_CACHE[$uid] = call_user_func_array($fun, $args);
    }

    return $MEMO_CACHE[$uid];
}

/** Returns a memoized version of the given function that will cache 
 * its results for each unique set of inputs. */
function memoize($fun) {
    return create_function(
        '', 
        "\$args = func_get_args(); return call_memoized('$fun', \$args);"
    );
}

Example:

/** Returns a random number with the given greeting. */
function random($greeting) {
    return "$greeting! " . rand();
};

print("Five random numbers:</br />");
for($i=0; $i<5; $i++) {
    print(random("Spin the wheel") . "<br />");
}
print "<br />";

print("After memoizing the random function, it's not so random:<br />");
$not_so_random = memoize("random");
for($i=0; $i<5; $i++) {
    print($not_so_random("Spin the wheel") . "<br />");
}
print "<br />";

print("The same memoized function is invoked with a different argument, and 
       thus creates a different cache key:<br />");
for($i=0; $i<5; $i++) {
    print($not_so_random("Twirl the tire") . "<br />");
}

/* OUTPUT

Five random numbers:
Spin the wheel! 26488
Spin the wheel! 20049
Spin the wheel! 14006
Spin the wheel! 28599
Spin the wheel! 804

After memoizing the random function, it's not so random:
Spin the wheel! 32397
Spin the wheel! 32397
Spin the wheel! 32397
Spin the wheel! 32397
Spin the wheel! 32397

The same memoized function is invoked with a different argument, and 
thus creates a different cache key:
Twirl the tire! 2114
Twirl the tire! 2114
Twirl the tire! 2114
Twirl the tire! 2114
Twirl the tire! 2114
*/
1
  • 1
    Instead of using a global, you can use a static local variable, can't you? And why array_map when you can serialize the whole array? Also wouldn't it be better to choose a stronger hashing algorithm (fewer collisions) or not hash at all?
    – mpen
    Dec 7, 2012 at 16:36
3

I suppose $conditions is an array of some values and you want to create a unique identifier for each variant of that array? There is several ways to do that, eg.:

$uid = md5(serialize($conditions));
3
  • Yes, that what I need. I think I will use func_get_args to return all parameters of functions. Should I use serialize or json_encode?
    – StoneHeart
    Aug 22, 2010 at 6:21
  • serialize() but in fact in that case both would do the job.
    – Crozin
    Aug 22, 2010 at 6:52
  • use json_encode for security resons ! and care about "StoneHeart" comment on accepted answer Mar 4, 2014 at 13:21
3

Have a look at memoized function implementation from Nspl:

function memoized(callable $function)
{
    return function() use ($function) {
        static $memory = array();
        $args = func_get_args();
        $key = serialize($args);
        if (!isset($memory[$key]) && !array_key_exists($key, $memory)) {
            $memory[$key] = call_user_func_array($function, $args);
        }

        return $memory[$key];
    };
}

Or if you looking for caching function results between server requests you can use Cachalot:

$cache = new \Cachalot\ApcCache();
$result = $cache->getCached($function, $args);

Different back-ends are supported: Apc, Xcache, Memcached, Redis, Couchbase.

2
  • 1
    Thanks for pointing to Cachalot for caching between server requests, that's exactly what I needed.
    – KeitelDOG
    Oct 11, 2018 at 11:48
  • Note that it builds caching key based on the function name and the arguments. So for Closures or inherited functions, you need to provide cache suffix to avoid cache collision. Oct 12, 2018 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.