Horde_Memcache::lock PHP Метод

lock() публичный Метод

Obtain lock on a key.
public lock ( string $key )
$key string The key to lock.
    public function lock($key)
    {
        $i = 0;
        while ($this->_lockAdd($key) === false) {
            usleep(min(pow(2, $i++) * 10000, 100000));
        }
        /* Register a shutdown handler function here to catch cases where PHP
         * suffers a fatal error. Must be done via shutdown function, since
         * a destructor will not be called in this case.
         * Only trigger on error, since we must assume that the code that
         * locked will also handle unlocks (which may occur in the destruct
         * phase, e.g. session handling).
         * @todo: $this is not usable in closures until PHP 5.4+ */
        if (empty($this->_locks)) {
            $self = $this;
            register_shutdown_function(function () use($self) {
                $e = error_get_last();
                if ($e['type'] & E_ERROR) {
                    /* Try to do cleanup at very end of shutdown methods. */
                    register_shutdown_function(array($self, 'shutdown'));
                }
            });
        }
        $this->_locks[$key] = true;
    }

Usage Example

Пример #1
0
 /**
  * Do garbage collection for session tracking information.
  */
 public function trackGC()
 {
     $this->_memcache->lock($this->_trackID);
     $ids = $this->_memcache->get($this->_trackID);
     if (empty($ids)) {
         $this->_memcache->unlock($this->_trackID);
         return;
     }
     $tstamp = time() - $this->_params['track_lifetime'];
     $alter = false;
     foreach ($ids as $key => $val) {
         if ($tstamp > $val) {
             unset($ids[$key]);
             $alter = true;
         }
     }
     if ($alter) {
         $this->_memcache->set($this->_trackID, $ids);
     }
     $this->_memcache->unlock($this->_trackID);
 }
All Usage Examples Of Horde_Memcache::lock