Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Forked from Andrewsville/gist:4030322
Created November 7, 2012 09:24
Show Gist options
  • Save fprochazka/4030362 to your computer and use it in GitHub Desktop.
Save fprochazka/4030362 to your computer and use it in GitHub Desktop.
/**
* Acquires an indexing lock if possible.
*
* @return boolean
*/
private function acquireLock()
{
$redis = $this->getRedis();
if ($redis->setNX(self::LOCK_KEY, $this->getTimeout())) {
return true;
}
$redis->watch(self::LOCK_KEY);
$lockExpiration = $redis->get(self::LOCK_KEY);
if ($lockExpiration < time()) {
try {
$redis->multi();
$this->updateLockTimeout();
$redis->exec();
return true;
} catch (Predis\Transaction\AbortedMultiExecException $e) {
return false;
}
}
$redis->unwatch();
return false;
}
/**
* Updates the indexing lock timeout.
*/
private function updateLockTimeout()
{
$this->getRedis()->set(self::LOCK_KEY, $this->getTimeout());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment