You are here

public static function UltimateCronLockMemcache::lock in Ultimate Cron 7.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 UltimateCronLockMemcache::lock()
UltimateCronLockMemcache::reLock in ./ultimate_cron.memcache-lock.inc
Relock.
UltimateCronLockMemcache::unlock in ./ultimate_cron.memcache-lock.inc
Release lock.

File

./ultimate_cron.memcache-lock.inc, line 51
A memcached-mediated implementation of a locking mechanism.

Class

UltimateCronLockMemcache
Class for handling lock functions.

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(
      'UltimateCronLockMemcache',
      '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;
}