Created
November 7, 2012 09:15
-
-
Save Andrewsville/4030322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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
Kolik dáváš timeout? Dle úkolu?