Skip to content

Instantly share code, notes, and snippets.

@Andrewsville
Created November 7, 2012 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Andrewsville/4030322 to your computer and use it in GitHub Desktop.
Save Andrewsville/4030322 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());
}
@fprochazka
Copy link

Kolik dáváš timeout? Dle úkolu?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment