You are here

function _apdqc_lock_acquire in Asynchronous Prefetch Database Query Cache 7

Same name in this branch
  1. 7 apdqc.lock.apc.inc \_apdqc_lock_acquire()
  2. 7 apdqc.lock.db.inc \_apdqc_lock_acquire()
  3. 7 apdqc.lock.memcache_storage.inc \_apdqc_lock_acquire()
  4. 7 apdqc.lock.memcache.inc \_apdqc_lock_acquire()
  5. 7 apdqc.lock.redis.inc \_apdqc_lock_acquire()

Acquire (or renew) a lock, but do not block if it fails.

Parameters

string $name: The name of the lock.

int $timeout: A number of seconds (int) before the lock expires (minimum of 1).

Return value

bool TRUE if the lock was acquired, FALSE if it failed.

1 call to _apdqc_lock_acquire()
apdqc.lock.inc in ./apdqc.lock.inc
A database-mediated implementation of a locking mechanism.
1 string reference to '_apdqc_lock_acquire'
apdqc.lock.inc in ./apdqc.lock.inc
A database-mediated implementation of a locking mechanism.

File

./apdqc.lock.memcache.inc, line 33
A memcache based implementation of a locking mechanism.

Code

function _apdqc_lock_acquire($name, $timeout = 30) {
  global $locks;

  // Ensure that the timeout is at least 1 sec. This is a limitation
  // imposed by memcached.
  $timeout = (int) max($timeout, 1);
  if (dmemcache_add($name, _lock_id(), $timeout, 'semaphore')) {
    $locks[$name] = _lock_id();
  }
  elseif (dmemcache_get($name, 'semaphore') && isset($locks[$name]) && $locks[$name] == _lock_id()) {

    // Only renew the lock if we already set it and it has not expired.
    dmemcache_set($name, _lock_id(), $timeout, 'semaphore');
  }
  else {

    // Failed to acquire the lock.  Unset the key from the $locks array even
    // if not set, PHP 5+ allows this without error or warning.
    unset($locks[$name]);
  }
  return isset($locks[$name]);
}