Функция кэширования результатов в PHP - PullRequest
2 голосов
/ 22 августа 2010

Я делаю простой инструмент для кеширования результатов функции

Это выглядит так:

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...
    }
}

(функция и ее параметры являются только примером)

Есть предложения?

Ответы [ 4 ]

9 голосов
/ 22 августа 2010

Вот ваш отсутствующий код:

$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);

Для справки: http://php.net/manual/en/function.func-get-args.php

5 голосов
/ 29 мая 2011

Вот общее решение памятка , которое совместимо с PHP 5.2.x.

Функция call_memoized:

  1. Создает уникальный ключ кэша на основе имени функции и аргументов.
  2. Если кеш пуст, вызывает функцию и обновляет кеш с результатом.
  3. Возвращает кэшированное значение.

Вместо непосредственного использования call_memoized используйте предоставленную функцию memoize, которая возвращает новую версию данной функции, которая будет автоматически кэшировать результаты из той же комбинации параметров.

См. Пример ниже.

Наслаждайтесь!

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);"
    );
}
* ** 1 022 тысяча двадцать-один * Пример:
/** 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
*/
3 голосов
/ 11 января 2016

Посмотрите на запомненную реализацию функции от 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];
    };
}

Или если вы ищете результаты кэширования между запросами к серверу, вы можете использовать Cachalot :

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

Поддерживаются различные серверные части: Apc, Xcache, Memcached, Redis, Couchbase.

2 голосов
/ 22 августа 2010

Полагаю, $conditions - это массив некоторых значений, и вы хотите создать уникальный идентификатор для каждого варианта этого массива?Есть несколько способов сделать это, например:

1003 *
...