Помогите с PHP call_user_func и интегрируйте функцию в класс?

Ниже приведена функция, которую я нашел около года назад, которая должна блокировать ключ memcache, чтобы вы могли обновить его значение без каких-либо проблем с двумя запросами, пытающимися обновить ключ одновременно.

Это довольно просто, но мне не помешала бы небольшая помощь, чтобы понять, как его использовать на 100%. Часть, в которой я не уверен, это то, где она передается в $updateFunction, которая затем передается в **

call_user_func($updateFunction, $data); // update data

Я никогда раньше не использовал call_user_func(). Основываясь на этом коде, кто-нибудь может показать мне базовый пример того, как может выглядеть функция, которую он вызывает (без использования mysql или чего-либо еще, просто так, как это будет работать). Смогу ли я передать несколько свойств в функцию вызова?

Спасибо за любую помощь. Кроме того, если у меня уже есть имя класса Memcache, у которого есть соединение или объект с этим $this->connection, нужно ли мне что-либо изменить в приведенной ниже функции, чтобы добавить его в мой класс Memcache? Кроме того, когда эта функция станет частью моего существующего класса memcache, сможет ли эта функция обратного вызова вызывать методы в других классах?

Надеюсь, это не сбивает с толку.

// {{{ locked_mecache_update($memcache,$key,$updateFunction,$expiryTime,$waitUTime,$maxTries)
/**
 * A function to do ensure only one thing can update a memcache at a time.
 *
 * Note that there are issues with the $expiryTime on memcache not being
 * fine enough, but this is the best I can do. The idea behind this form
 * of locking is that it takes advantage of the fact that
 * {@link memcache_add()}'s are atomic in nature.
 *
 * It would be possible to be a more interesting limiter (say that limits
 * updates to no more than 1/second) simply by storing a timestamp or
 * something of that nature with the lock key (currently stores "1") and
 * not deleitng the memcache entry.
 *
 * @param $memcache memcache the memcache object
 * @param $key string the key to do the update on
 * @param $updateFunction mixed the function to call that accepts the data
 *  from memcache and modifies it (use pass by reference).
 * @param $expiryTime integer time in seconds to allow the key to last before
 *  it will expire. This should only happen if the process dies during update.
 *  Choose a number big enough so that $updateFunction will take much less
 *  time to execute.
 * @param $waitUTime integer the amount of time in microseconds to wait before
 *  checking for the lock to release
 * @param $maxTries integer maximum number of attempts before it gives up
 *  on the locks. Note that if $maxTries is 0, then it will RickRoll forever
 *  (never give up). The default number ensures that it will wait for three
 *  full lock cycles to crash before it gives up also.
 * @return boolean success or failure
 */
function locked_memcache_update(
    $memcache,          // Memcache Object
    $key,               // Key to update
    $updateFunction,   // Function to pass key/value into to update
    $expiryTime=3,     // time before key expires
    $waitUtime=101,    // time to wait for to check for lock release
    $maxTries=100000) //  number of times to try to get lock
    {

    $lock = 'lock:'.$key;

    // get the lock {{{
    if ($maxTries>0) {
        for ($tries=0; $tries< $maxTries; ++$tries) {
            if ($memcache->add($lock,1,0,$expiryTime)) { break; }
            usleep($waitUtime);
        }
        if ($tries == $maxTries) {
            // handle failure case (use exceptions and try-catch if you need to be nice)
            trigger_error(sprintf('Lock failed for key: %s',$key), E_USER_NOTICE);
            return false;
        }
    } else {
        while (!$memcache->add($lock,1,0,$expiryTime)) {
            usleep($waitUtime);
        }
    }
    // }}}

    //Now we have a lock so we can update of key/value
    // modify data in cache {{{
    $data = $memcache->get($key, $flag);
    call_user_func($updateFunction, $data); // update data
    $memcache->set($key, $data, $flag);
    // }}}

    // Update complete so we release our lock
    // clear the lock
    $memcache->delete($lock,0);
    return true;
}
// }}}
?>

ОБНОВЛЕНИЕ

Вот мой существующий класс Memcache, с которым я хотел бы интегрировать его.

<?PHP
class MemCache
{

    // Memcache object
    public $connection;

    function __construct()
    {
        $this->connection = new MemCache;
    }

    // Set a key/value to memcache with Expire time
    function store($key, $data, $ttl)
    {
        return $this->connection->set($key, $data, 0, $ttl);
    }

    // Retrieve a Value from Memcache with a KEY
    function fetch($key)
    {
        return $this->connection->get($key);
    }

    // DELETE a Key/Value from Memcache with a KEY
    function delete($key)
    {
        return $this->connection->delete($key);
    }

    // Add a server connection to memcache
    function addServer($host, $port = 11211, $weight = 10)
    {
        $this->connection->addServer($host, $port, true, $weight);
    }

    // Clear all memcache data
    function flush()
    {
        return $this->connection->flush();
    }

    // Show Memcache stats
    function stats()
    {
        return statDetails($this->connection->getStats());
    }

    // Show Memcache stats in a table
    function statDetails($status)
    {
        echo "<table border='1'>";
        echo "<tr><td>Memcache Server version:</td><td> " . $status["version"] .
            "</td></tr>";
        echo "<tr><td>Process id of this server process </td><td>" . $status["pid"] .
            "</td></tr>";
        echo "<tr><td>Number of seconds this server has been running </td><td>" . $status["uptime"] .
            "</td></tr>";
        echo "<tr><td>Accumulated user time for this process </td><td>" . $status["rusage_user"] .
            " seconds</td></tr>";
        echo "<tr><td>Accumulated system time for this process </td><td>" . $status["rusage_system"] .
            " seconds</td></tr>";
        echo "<tr><td>Total number of items stored by this server ever since it started </td><td>" .
            $status["total_items"] . "</td></tr>";
        echo "<tr><td>Number of open connections </td><td>" . $status["curr_connections"] .
            "</td></tr>";
        echo "<tr><td>Total number of connections opened since the server started running </td><td>" .
            $status["total_connections"] . "</td></tr>";
        echo "<tr><td>Number of connection structures allocated by the server </td><td>" .
            $status["connection_structures"] . "</td></tr>";
        echo "<tr><td>Cumulative number of retrieval requests </td><td>" . $status["cmd_get"] .
            "</td></tr>";
        echo "<tr><td> Cumulative number of storage requests </td><td>" . $status["cmd_set"] .
            "</td></tr>";
        $percCacheHit = ((real)$status["get_hits"] / (real)$status["cmd_get"] * 100);
        $percCacheHit = round($percCacheHit, 3);
        $percCacheMiss = 100 - $percCacheHit;
        echo "<tr><td>Number of keys that have been requested and found present </td><td>" .
            $status["get_hits"] . " ($percCacheHit%)</td></tr>";
        echo "<tr><td>Number of items that have been requested and not found </td><td>" .
            $status["get_misses"] . "($percCacheMiss%)</td></tr>";
        $MBRead = (real)$status["bytes_read"] / (1024 * 1024);
        echo "<tr><td>Total number of bytes read by this server from network </td><td>" .
            $MBRead . " Mega Bytes</td></tr>";
        $MBWrite = (real)$status["bytes_written"] / (1024 * 1024);
        echo "<tr><td>Total number of bytes sent by this server to network </td><td>" .
            $MBWrite . " Mega Bytes</td></tr>";
        $MBSize = (real)$status["limit_maxbytes"] / (1024 * 1024);
        echo "<tr><td>Number of bytes this server is allowed to use for storage.</td><td>" .
            $MBSize . " Mega Bytes</td></tr>";
        echo "<tr><td>Number of valid items removed from cache to free memory for new items.</td><td>" .
            $status["evictions"] . "</td></tr>";
        echo "</table>";
    }

}

?>

person JasonDavis    schedule 21.01.2010    source источник


Ответы (2)


документация call_user_func() содержит ссылку на псевдотип обратного вызова. Короче говоря, это либо имя функции, либо массив. Массив должен содержать либо две строки (имя класса и имя функции — обратный вызов для статических функций), либо объект и строку (объект и метод для вызова этого объекта — для функций-членов). Вот примеры из документации:

<?php 

// An example callback function
function my_callback_function() {
    echo 'hello world!';
}

// An example callback method
class MyClass {
    static function myCallbackMethod() {
        echo 'Hello World!';
    }
}

// Type 1: Simple callback
call_user_func('my_callback_function');


// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod')); 

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

class B extends A {
    public static function who() {
        echo "B\n";
    }
}

call_user_func(array('B', 'parent::who')); // A
?>

И еще: ваша блокировка может быть прервана между add($lock,... и get(). Используя increment() и decrement() решит эту проблему.

person soulmerge    schedule 21.01.2010
comment
Я читал increment() и decrement(). Я не уверен, как их можно использовать здесь, не могли бы вы объяснить немного подробнее, как их можно использовать, чтобы помочь с блокировкой, пожалуйста? - person JasonDavis; 21.01.2010
comment
if increment($lock) != 1, объект уже был заблокирован, вернитесь с помощью decrement($lock). В противном случае вы успешно установили блокировку и можете выполнить свою операцию и вызвать decrement(), чтобы снова снять блокировку. Ключевым моментом здесь является то, что increment() и decrement() являются атомарными операциями, которые считывают и значения последовательно без перерыва. - person soulmerge; 21.01.2010

Не напрямую ответ, но стоит отметить, что на самом деле вам не нужно использовать call_user_func - вы можете просто вызвать переменную функцию:

function ham($jam) {
    echo $jam;
}

$bar = 'eggs';
$foo = 'ham';

$foo($bar);

Это должно хорошо работать и с экземплярами объектов:

$bees->$honey($sting);

Дополнительную информацию можно найти в документах.

person PeterJCLaw    schedule 21.01.2010