Skip to content

Commit b7a561d

Browse files
committed
Implemented #598 // Ability to use custom predis/redis client
1 parent 8c8f4f2 commit b7a561d

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

lib/Phpfastcache/Drivers/Predis/Config.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class Config extends ConfigurationOption
3232
*/
3333
protected $database = 0;
3434

35+
/**
36+
* @var \Predis\Client
37+
*/
38+
protected $predisClient;
39+
3540
/**
3641
* @return string
3742
*/
@@ -116,4 +121,22 @@ public function getPredisConfigArray(): array
116121
'database' => $this->getDatabase(),
117122
];
118123
}
124+
125+
/**
126+
* @return \Predis\Client|null
127+
*/
128+
public function getPredisClient()
129+
{
130+
return $this->predisClient;
131+
}
132+
133+
/**
134+
* @param \Predis\Client $predisClient|null
135+
* @return Config
136+
*/
137+
public function setPredisClient(\Predis\Client $predisClient = null): Config
138+
{
139+
$this->predisClient = $predisClient;
140+
return $this;
141+
}
119142
}

lib/Phpfastcache/Drivers/Predis/Driver.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface};
2020
use Phpfastcache\Entities\DriverStatistic;
2121
use Phpfastcache\Exceptions\{
22-
PhpfastcacheInvalidArgumentException, PhpfastcacheDriverException
22+
PhpfastcacheInvalidArgumentException, PhpfastcacheDriverException, PhpfastcacheLogicException
2323
};
2424
use Phpfastcache\Util\ArrayObject;
2525
use Predis\Client as PredisClient;
@@ -52,9 +52,26 @@ public function driverCheck(): bool
5252
/**
5353
* @return bool
5454
* @throws PhpfastcacheDriverException
55+
* @throws PhpfastcacheLogicException
5556
*/
5657
protected function driverConnect(): bool
5758
{
59+
if ($this->instance instanceof PredisClient) {
60+
throw new PhpfastcacheLogicException('Already connected to Predis server');
61+
}
62+
63+
/**
64+
* In case of an user-provided
65+
* Predis client just return here
66+
*/
67+
if($this->getConfig()->getPredisClient() instanceof PredisClient){
68+
$this->instance = $this->getConfig()->getPredisClient();
69+
if(!$this->instance->isConnected()){
70+
$this->instance->connect();
71+
}
72+
return true;
73+
}
74+
5875
if(!empty($this->getConfig()->getPath())){
5976
$this->instance = new PredisClient([
6077
'scheme' => 'unix',

lib/Phpfastcache/Drivers/Redis/Config.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Phpfastcache\Drivers\Redis;
1010

1111
use Phpfastcache\Config\ConfigurationOption;
12+
use Redis as RedisClient;
1213

1314
class Config extends ConfigurationOption
1415
{
@@ -37,6 +38,11 @@ class Config extends ConfigurationOption
3738
*/
3839
protected $timeout = 5;
3940

41+
/**
42+
* @var RedisClient
43+
*/
44+
protected $redisClient;
45+
4046
/**
4147
* @return string
4248
*/
@@ -126,4 +132,22 @@ public function setTimeout(int $timeout): self
126132
$this->timeout = $timeout;
127133
return $this;
128134
}
135+
136+
/**
137+
* @return RedisClient|null
138+
*/
139+
public function getRedisClient()
140+
{
141+
return $this->redisClient;
142+
}
143+
144+
/**
145+
* @param RedisClient $predisClient|null
146+
* @return Config
147+
*/
148+
public function setRedisClient(RedisClient $redisClient = null): Config
149+
{
150+
$this->redisClient = $redisClient;
151+
return $this;
152+
}
129153
}

lib/Phpfastcache/Drivers/Redis/Driver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ protected function driverConnect(): bool
5555
throw new PhpfastcacheLogicException('Already connected to Redis server');
5656
}
5757

58+
/**
59+
* In case of an user-provided
60+
* Redis client just return here
61+
*/
62+
if($this->getConfig()->getRedisClient() instanceof RedisClient){
63+
/**
64+
* Unlike Predis, we can't test if we're are connected
65+
* or not, so let's just assume that we are
66+
*/
67+
$this->instance = $this->getConfig()->getRedisClient();
68+
return true;
69+
}
70+
5871
$this->instance = $this->instance ?: new RedisClient();
5972

6073
/**

0 commit comments

Comments
 (0)