You are here

public function MemcacheLockBackend::acquire in Memcache API and Integration 8.2

Acquires a lock.

Parameters

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

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

Return value

bool

Overrides LockBackendInterface::acquire

File

src/Lock/MemcacheLockBackend.php, line 54

Class

MemcacheLockBackend
Defines a Memcache lock backend.

Namespace

Drupal\memcache\Lock

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 (isset($this->locks[$name])) {

    // Try to extend the expiration of a lock we already acquired.
    $success = !$this
      ->lockMayBeAvailable($name) && $this->memcache
      ->set($name, $lock_id, $timeout);
    if (!$success) {

      // The lock was broken.
      unset($this->locks[$name]);
    }
    return $success;
  }
  else {
    if ($this
      ->lockMayBeAvailable($name)) {
      $success = $this->memcache
        ->add($name, $lock_id, $timeout);
      if (!$success) {
        return FALSE;
      }

      // We track all acquired locks in the global variable, if successful.
      $this->locks[$name] = TRUE;
    }
    else {
      return FALSE;
    }
  }
  return isset($this->locks[$name]);
}