Back to ApcuStorage class

Method lock

public mixed
lock
(mixed $id, mixed $group, mixed $locktime)
Lock cached item
Parameters
  • string $id The cache data ID
  • string $group The cache data group
  • int $locktime Cached item max lock time
Returns
  • mixed Boolean false if locking failed or an object containing properties lock and locklooped
Since
  • 3.5
Class: ApcuStorage
Project: Joomla

Method lock - Source code

/**
 * Lock cached item
 *
 * @param   string   $id        The cache data ID
 * @param   string   $group     The cache data group
 * @param   integer  $locktime  Cached item max lock time
 *
 * @return  mixed  Boolean false if locking failed or an object containing properties lock and locklooped
 *
 * @since   3.5
 */
public function lock($id, $group, $locktime)
{
    $returning = new \stdClass();
    $returning->locklooped = false;
    $looptime = $locktime * 10;
    $cache_id = $this->_getCacheId($id, $group) . '_lock';
    $data_lock = apcu_add($cache_id, 1, $locktime);
    if ($data_lock === false) {
        $lock_counter = 0;
        // Loop until you find that the lock has been released.
        // That implies that data get from other thread has finished
        while ($data_lock === false) {
            if ($lock_counter > $looptime) {
                $returning->locked = false;
                $returning->locklooped = true;
                break;
            }
            usleep(100);
            $data_lock = apcu_add($cache_id, 1, $locktime);
            $lock_counter++;
        }
    }
    $returning->locked = $data_lock;
    return $returning;
}