/** * newManager. * * @param int $validAdaptersCount * @param int $invalidAdaptersCount * @param int $ttl * @param int $retries * @param int $retryMaxDelay * * @return LockManager */ private function newManager($validAdaptersCount = 1, $invalidAdaptersCount = 0, $ttl = 60, $retries = 3, $retryMaxDelay = 1) { $manager = new LockManager(new HalfPlusOneQuorum(), new DefaultKeyGenerator(), new LockTypeManager(), $ttl, $retries, $retryMaxDelay); $count = count($this->validAdapters); if ($validAdaptersCount < $count) { $count = $validAdaptersCount; } foreach ($this->validAdapters as $key => $value) { if ($count == 0) { break; } $count--; $manager->addAdapter(new PredisAdapter(new \Predis\Client(array('host' => $value['host'], 'port' => $value['port'], 'timeout' => $value['timeout'], 'async' => $value['async'])))); } $count = count($this->invalidAdapters); if ($invalidAdaptersCount < $count) { $count = $invalidAdaptersCount; } foreach ($this->invalidAdapters as $key => $value) { if ($count == 0) { break; } $count--; $manager->addAdapter(new PredisAdapter(new \Predis\Client(array('host' => $value['host'], 'port' => $value['port'], 'timeout' => $value['timeout'])))); } $manager->releaseAllLocks(); $manager->clearAllLocks(); return $manager; }
<?php use Everlution\Redlock\Manager\LockManager; use Everlution\Redlock\Manager\LockTypeManager; use Everlution\Redlock\Quorum\HalfPlusOneQuorum; use Everlution\Redlock\KeyGenerator\DefaultKeyGenerator; use Everlution\Redlock\Adapter\PredisAdapter; $manager = new LockManager(new HalfPlusOneQuorum(), new DefaultKeyGenerator(), new LockTypeManager(), 60, 3, 10); $manager->addAdapter(new PredisAdapter(new \Predis\Client(array('host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0, 'async' => false))))->addAdapter(new PredisAdapter(new \Predis\Client(array('host' => '127.0.0.1', 'port' => 6380, 'timeout' => 0, 'async' => false))))->addAdapter(new PredisAdapter(new \Predis\Client(array('host' => '127.0.0.1', 'port' => 6381, 'timeout' => 0, 'async' => false)))); $resourceName = 'printer'; $nlLock = new Lock($resourceName, LockType::NULL, $token1); $cwLock = new Lock($resourceName, LockType::CONCURRENT_WRITE, $token1); $exLock = new Lock($resourceName, LockType::EXCLUSIVE, $token2); $crLock = new Lock($resourceName, LockType::CONCURRENT_READ, $token1); // current locks: [] $manager->acquireLock($nlLock); // true // current locks: [NL] $manager->acquireLock($cwLock); // true // current locks: [NL, CW] $manager->acquireLock($exLock); // false // current locks: [NL, CW] $manager->acquireLock($crLock); // true // current locks: [NL, CW, CR] $manager->releaseLock($cwLock); // current locks: [NL, CR] $manager->acquireLock($exLock); // false
/** * newManager. * * @param int $validAdaptersCount * @param int $invalidAdaptersCount * @param int $ttl * @param int $retries * @param int $retryMaxDelay * * @return LockManager */ private function newManager($validAdaptersCount = 1, $invalidAdaptersCount = 0, $ttl = 60, $retries = 3, $retryMaxDelay = 1) { $manager = new LockManager(new HalfPlusOneQuorum(), new DefaultKeyGenerator(), new LockTypeManager(), $ttl, $retries, $retryMaxDelay); $count = count($this->validAdapters); if ($validAdaptersCount < $count) { $count = $validAdaptersCount; } foreach ($this->validAdapters as $key => $value) { if ($count == 0) { break; } $count--; $manager->addAdapter($this->getValidAdapter($value)); } $count = count($this->invalidAdapters); if ($invalidAdaptersCount < $count) { $count = $invalidAdaptersCount; } foreach ($this->invalidAdapters as $key => $value) { if ($count == 0) { break; } $count--; $manager->addAdapter($this->getInvalidAdapter($value)); } $manager->releaseAllLocks(); $manager->clearAllLocks(); return $manager; }