Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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