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.apc.inc, line 30
An APC 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. APC works with integer TTL.
  $timeout = (int) max($timeout, 1);
  $key = _apdqc_apc_get_lock_key($name);
  if (apc_add($key, _lock_id(), $timeout)) {
    $locks[$name] = _lock_id();
  }
  elseif (apc_fetch($key) && isset($locks[$name]) && $locks[$name] == _lock_id()) {

    // Only renew the lock if we already set it and it has not expired.
    apc_store($key, _lock_id(), $timeout);
  }
  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]);
}