You are here

public function MemcacheLockBackend::acquire in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 modules/memcache/src/MemcacheLockBackend.php \Drupal\memcache\MemcacheLockBackend::acquire()

Acquires a lock.

Parameters

string $name: Lock name. Limit of name's length is 255 characters.

float $timeout = 30.0: (optional) Lock lifetime in seconds.

Return value

bool

Overrides LockBackendInterface::acquire

File

modules/memcache/src/MemcacheLockBackend.php, line 51
Contains \Drupal\memcache\MemcacheLockBackend.

Class

MemcacheLockBackend
Defines a Memcache lock backend.

Namespace

Drupal\memcache

Code

public function acquire($name, $timeout = 30.0) {

  // Ensure that the timeout is at least 1 sec. This is a limitation imposed
  // by memcached.
  $timeout = (int) max($timeout, 1);
  $lock_id = $this
    ->getLockId();
  if ($this->memcache
    ->set($name, $lock_id, $timeout)) {
    $this->locks[$name] = $lock_id;
  }
  elseif (($result = $this->memcache
    ->get($name)) && isset($this->locks[$name]) && $this->locks[$name] == $lock_id) {

    // Only renew the lock if we already set it and it has not expired.
    $this->memcache
      ->set($name, $lock_id, $timeout);
  }
  else {

    // Failed to acquire the lock. Unset the key from the $locks array even if
    // not set.
    unset($this->locks[$name]);
  }
  return isset($this->locks[$name]);
}