File tree Expand file tree Collapse file tree 4 files changed +78
-1
lines changed Expand file tree Collapse file tree 4 files changed +78
-1
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1919use Phpfastcache \Core \Pool \{DriverBaseTrait , ExtendedCacheItemPoolInterface };
2020use Phpfastcache \Entities \DriverStatistic ;
2121use Phpfastcache \Exceptions \{
22- PhpfastcacheInvalidArgumentException , PhpfastcacheDriverException
22+ PhpfastcacheInvalidArgumentException , PhpfastcacheDriverException , PhpfastcacheLogicException
2323};
2424use Phpfastcache \Util \ArrayObject ;
2525use 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 ' ,
Original file line number Diff line number Diff line change 99namespace Phpfastcache \Drivers \Redis ;
1010
1111use Phpfastcache \Config \ConfigurationOption ;
12+ use Redis as RedisClient ;
1213
1314class 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}
Original file line number Diff line number Diff 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 /**
You can’t perform that action at this time.
0 commit comments