33
44use SplitIO \Component \Cache \Storage \Exception \AdapterException ;
55use SplitIO \Component \Utils as SplitIOUtils ;
6- use SplitIO \Component \Common \Context ;
76
87/**
98 * Class PRedis
@@ -15,6 +14,9 @@ class PRedis implements CacheStorageAdapterInterface
1514 /** @var \Predis\Client|null */
1615 private $ client = null ;
1716
17+ /** @var string */
18+ private $ prefix = "" ;
19+
1820 /**
1921 * @param array $options
2022 * @throws AdapterException
@@ -26,7 +28,11 @@ public function __construct(array $options)
2628 }
2729 $ _redisConfig = $ this ->getRedisConfiguration ($ options );
2830
29- $ this ->client = new \Predis \Client ($ _redisConfig ['redis ' ], $ _redisConfig ['options ' ]);
31+ $ this ->client = new \Predis \Client ($ _redisConfig ['parameters ' ], $ _redisConfig ['options ' ]);
32+
33+ if (isset ($ _redisConfig ['options ' ]['prefix ' ])) {
34+ $ this ->prefix = $ _redisConfig ['options ' ]['prefix ' ];
35+ }
3036 }
3137
3238 /**
@@ -48,24 +54,6 @@ private function isValidConfigArray($nodes, $type)
4854 return null ;
4955 }
5056
51- /**
52- * @param array $sentinels
53- * @param array $options
54- * @return bool
55- * @throws AdapterException
56- */
57- private function isValidSentinelConfig ($ sentinels , $ options )
58- {
59- $ msg = $ this ->isValidConfigArray ($ sentinels , 'sentinel ' );
60- if (!is_null ($ msg )) {
61- throw new AdapterException ($ msg );
62- }
63- if (!isset ($ options ['service ' ])) {
64- throw new AdapterException ('Master name is required in replication mode for sentinel. ' );
65- }
66- return true ;
67- }
68-
6957 private function validateKeyHashTag ($ keyHashTag )
7058 {
7159 if (!is_string ($ keyHashTag )) {
@@ -121,83 +109,32 @@ function ($value) {
121109 if (empty ($ filteredArray )) {
122110 throw new AdapterException ('keyHashTags size is zero after filtering valid elements. ' );
123111 }
124- return $ selected = $ filteredArray [array_rand ($ filteredArray , 1 )];
112+ return $ filteredArray [array_rand ($ filteredArray , 1 )];
125113 }
126114
127115
128- /**
129- * @param array $clusters
130- * @return bool
131- * @throws AdapterException
132- */
133- private function isValidClusterConfig ($ clusters )
134- {
135- $ msg = $ this ->isValidConfigArray ($ clusters , 'clusterNode ' );
136- if (!is_null ($ msg )) {
137- throw new AdapterException ($ msg );
138- }
139- return true ;
140- }
141-
142116 /**
143117 * @param mixed $options
144118 * @return array
145- * @throws AdapterException
146119 */
147120 private function getRedisConfiguration ($ options )
148121 {
149122 $ redisConfigutation = array (
150- 'redis ' => null ,
151- 'options ' => null
123+ 'parameters ' => ( isset ( $ options [ ' parameters ' ])) ? $ options [ ' parameters ' ] : null ,
124+ 'options ' => null ,
152125 );
153126
154- $ parameters = (isset ($ options ['parameters ' ])) ? $ options ['parameters ' ] : null ;
155- $ sentinels = (isset ($ options ['sentinels ' ])) ? $ options ['sentinels ' ] : null ;
156- $ clusters = (isset ($ options ['clusterNodes ' ])) ? $ options ['clusterNodes ' ] : null ;
157127 $ _options = (isset ($ options ['options ' ])) ? $ options ['options ' ] : null ;
158-
159- if (isset ($ _options ['distributedStrategy ' ]) && isset ($ parameters ['tls ' ])) {
160- throw new AdapterException ("SSL/TLS cannot be used together with sentinel/cluster yet " );
161- }
162-
163128 if ($ _options && isset ($ _options ['prefix ' ])) {
164129 $ _options ['prefix ' ] = self ::normalizePrefix ($ _options ['prefix ' ]);
165130 }
166131
167- if (isset ($ parameters )) {
168- $ redisConfigutation ['redis ' ] = $ parameters ;
169- } else {
170- // @TODO remove this statement when replication will be deprecated
171- if (isset ($ _options ['replication ' ])) {
172- Context::getLogger ()->warning ("'replication' option was deprecated please use 'distributedStrategy' " );
173- if (!isset ($ _options ['distributedStrategy ' ])) {
174- $ _options ['distributedStrategy ' ] = $ _options ['replication ' ];
175- }
176- }
177- if (isset ($ _options ['distributedStrategy ' ])) {
178- switch ($ _options ['distributedStrategy ' ]) {
179- case 'cluster ' :
180- if ($ this ->isValidClusterConfig ($ clusters )) {
181- $ keyHashTag = $ this ->selectKeyHashTag ($ _options );
182- $ _options ['cluster ' ] = 'redis ' ;
183- $ redisConfigutation ['redis ' ] = $ clusters ;
184- $ prefix = isset ($ _options ['prefix ' ]) ? $ _options ['prefix ' ] : '' ;
185- $ _options ['prefix ' ] = $ keyHashTag . $ prefix ;
186- }
187- break ;
188- case 'sentinel ' :
189- if ($ this ->isValidSentinelConfig ($ sentinels , $ _options )) {
190- $ _options ['replication ' ] = 'sentinel ' ;
191- $ redisConfigutation ['redis ' ] = $ sentinels ;
192- }
193- break ;
194- default :
195- throw new AdapterException ("Wrong configuration of redis 'distributedStrategy'. " );
196- }
197- } else {
198- throw new AdapterException ("Wrong configuration of redis. " );
199- }
132+ if (isset ($ _options ['cluster ' ])) {
133+ $ keyHashTag = $ this ->selectKeyHashTag ($ _options );
134+ $ prefix = isset ($ _options ['prefix ' ]) ? $ _options ['prefix ' ] : '' ;
135+ $ _options ['prefix ' ] = $ keyHashTag . $ prefix ;
200136 }
137+
201138 $ redisConfigutation ['options ' ] = $ _options ;
202139 return $ redisConfigutation ;
203140 }
@@ -249,31 +186,8 @@ public function isOnList($key, $value)
249186
250187 public function getKeys ($ pattern = '* ' )
251188 {
252- $ prefix = null ;
253- if ($ this ->client ->getOptions ()->__isset ("prefix " )) {
254- $ prefix = $ this ->client ->getOptions ()->__get ("prefix " )->getPrefix ();
255- }
256-
257- if ($ this ->client ->getOptions ()->__isset ("distributedStrategy " ) &&
258- $ this ->client ->getOptions ()->__get ("distributedStrategy " ) == "cluster " ) {
259- $ keys = array ();
260- foreach ($ this ->client as $ nodeClient ) {
261- $ nodeClientKeys = $ nodeClient ->keys ($ pattern );
262- $ keys = array_merge ($ keys , $ nodeClientKeys );
263- }
264- } else {
265- $ keys = $ this ->client ->keys ($ pattern );
266- }
267- if ($ prefix ) {
268- if (is_array ($ keys )) {
269- foreach ($ keys as $ index => $ key ) {
270- $ keys [$ index ] = str_replace ($ prefix , '' , $ key );
271- }
272- } else {
273- $ keys = str_replace ($ prefix , '' , $ keys );
274- }
275- }
276- return $ keys ;
189+ $ keys = $ this ->client ->keys ($ pattern );
190+ return str_replace ($ this ->prefix , '' , $ keys );
277191 }
278192
279193 private static function normalizePrefix ($ prefix )
0 commit comments