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 (float) before the lock expires (minimum of 0.001).

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_storage.inc, line 48
A memcached based implementation of a locking mechanism.

Code

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

  // Insure that the timeout is at least 1 second.
  $expire = (int) max($timeout, 1);
  if (isset($locks[$name])) {

    // Try to extend the expiration of a lock we already acquired.
    $success = MemcacheStorageAPI::replace($name, _lock_id(), $expire, 'semaphore');
    if (!$success) {

      // The lock was broken.
      unset($locks[$name]);
    }
  }
  else {

    // Add new lock.
    if (MemcacheStorageAPI::add($name, _lock_id(), $expire, 'semaphore')) {

      // We track all acquired locks in the global variable.
      $locks[$name] = TRUE;
    }
  }
  return isset($locks[$name]);
}