From f45b9179a4df64a49bc5c58c2fa8dde1027bc5cd Mon Sep 17 00:00:00 2001 From: Volker Machon Date: Tue, 18 Apr 2017 14:23:10 +0200 Subject: [PATCH 1/2] check laravel database config and grab redis driver --- src/Mutex.php | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Mutex.php b/src/Mutex.php index aaa69fc..d9ebba3 100644 --- a/src/Mutex.php +++ b/src/Mutex.php @@ -7,10 +7,10 @@ use NinjaMutex\Lock\FlockLock; use NinjaMutex\Lock\MemcachedLock; use NinjaMutex\Lock\MySqlLock; +use NinjaMutex\Lock\PhpRedisLock; use NinjaMutex\Lock\PredisRedisLock; use NinjaMutex\Mutex as Ninja; use Predis\Client; -use Redis; class Mutex { @@ -41,7 +41,7 @@ public function getStrategy() ); case 'redis': - return new PredisRedisLock($this->getRedisClient()); + return $this->getRedisLock(config('database.redis.client', 'predis')); case 'memcached': return new MemcachedLock(Cache::getStore()->getMemcached()); @@ -52,12 +52,24 @@ public function getStrategy() } } - public function getRedisClient() + /** + * @return Client|\Redis + */ + public function getRedisClient($driver = 'predis') { - $connection = Redis::connection(); + $connection = \Illuminate\Support\Facades\Redis::connection(); - /* @laravel-versions */ - $redisClient = ($connection instanceof Client) ? $connection : $connection->client(); + switch ($driver) { + case 'phpredis': + /* @laravel-versions */ + $redisClient = ($connection instanceof \Redis) ? $connection : $connection->client(); + break; + case 'predis': + default: + /* @laravel-versions */ + $redisClient = ($connection instanceof Client) ? $connection : $connection->client(); + break; + } return $redisClient; } @@ -66,4 +78,15 @@ public function __call($method, $parameters) { return call_user_func_array([$this->ninja, $method], $parameters); } + + private function getRedisLock($driver) + { + switch ($driver) { + case 'phpredis': + return new PhpRedisLock($this->getRedisClient($driver)); + case 'predis': + default: + return new PredisRedisLock($this->getRedisClient($driver)); + } + } } From ff69e42b1bb4c1cb7da09f90f9e019421e458d9a Mon Sep 17 00:00:00 2001 From: Volker Machon Date: Tue, 18 Apr 2017 14:28:32 +0200 Subject: [PATCH 2/2] added php docs --- src/Mutex.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Mutex.php b/src/Mutex.php index d9ebba3..19a8768 100644 --- a/src/Mutex.php +++ b/src/Mutex.php @@ -25,6 +25,9 @@ public function __construct(Command $command) $this->ninja = new Ninja($command->getMutexName(), $this->strategy); } + /** + * @return FlockLock|MemcachedLock|MySqlLock|PhpRedisLock|PredisRedisLock + */ public function getStrategy() { if (!empty($this->strategy)) { @@ -79,6 +82,10 @@ public function __call($method, $parameters) return call_user_func_array([$this->ninja, $method], $parameters); } + /** + * @param $driver + * @return PhpRedisLock|PredisRedisLock + */ private function getRedisLock($driver) { switch ($driver) {