You are here

public static function LockMemcache::lock in Ultimate Cron 8.2

Acquire lock.

Parameters

string $name: The name of the lock to acquire.

float $timeout: The timeout in seconds for the lock.

Return value

string The lock id acquired.

2 calls to LockMemcache::lock()
LockMemcache::reLock in src/Lock/LockMemcache.php
Relock.
LockMemcache::unlock in src/Lock/LockMemcache.php
Release lock.

File

src/Lock/LockMemcache.php, line 46

Class

LockMemcache
Class for handling lock functions.

Namespace

Drupal\ultimate_cron\Lock

Code

public static function lock($name, $timeout = 30.0) {

  // First, ensure cleanup.
  if (!isset(self::$locks)) {
    self::$locks = array();
    ultimate_cron_register_shutdown_function(array(
      'Drupal\\ultimate_cron\\Lock\\LockMemcache',
      'shutdown',
    ));
  }

  // Ensure that the timeout is at least 1 sec. This is a limitation
  // imposed by memcached.
  $timeout = (int) max($timeout, 1);
  $bin = variable_get('ultimate_cron_lock_memcache_bin', 'semaphore');
  $lock_id = $name . ':' . uniqid('memcache-lock', TRUE);
  if (dmemcache_add($name, $lock_id, $timeout, $bin)) {
    self::$locks[$lock_id] = $lock_id;
    return $lock_id;
  }
  return FALSE;
}