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
@@ -26,7 +25,7 @@ public function __construct(array $options)
2625 }
2726 $ _redisConfig = $ this ->getRedisConfiguration ($ options );
2827
29- $ this ->client = new \Predis \Client ($ _redisConfig ['redis ' ], $ _redisConfig ['options ' ]);
28+ $ this ->client = new \Predis \Client ($ _redisConfig ['parameters ' ], $ _redisConfig ['options ' ]);
3029 }
3130
3231 /**
@@ -48,24 +47,6 @@ private function isValidConfigArray($nodes, $type)
4847 return null ;
4948 }
5049
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-
6950 private function validateKeyHashTag ($ keyHashTag )
7051 {
7152 if (!is_string ($ keyHashTag )) {
@@ -121,83 +102,32 @@ function ($value) {
121102 if (empty ($ filteredArray )) {
122103 throw new AdapterException ('keyHashTags size is zero after filtering valid elements. ' );
123104 }
124- return $ selected = $ filteredArray [array_rand ($ filteredArray , 1 )];
105+ return $ filteredArray [array_rand ($ filteredArray , 1 )];
125106 }
126107
127108
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-
142109 /**
143110 * @param mixed $options
144111 * @return array
145- * @throws AdapterException
146112 */
147113 private function getRedisConfiguration ($ options )
148114 {
149115 $ redisConfigutation = array (
150- 'redis ' => null ,
151- 'options ' => null
116+ 'parameters ' => ( isset ( $ options [ ' parameters ' ])) ? $ options [ ' parameters ' ] : null ,
117+ 'options ' => null ,
152118 );
153119
154- $ parameters = (isset ($ options ['parameters ' ])) ? $ options ['parameters ' ] : null ;
155- $ sentinels = (isset ($ options ['sentinels ' ])) ? $ options ['sentinels ' ] : null ;
156- $ clusters = (isset ($ options ['clusterNodes ' ])) ? $ options ['clusterNodes ' ] : null ;
157120 $ _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-
163121 if ($ _options && isset ($ _options ['prefix ' ])) {
164122 $ _options ['prefix ' ] = self ::normalizePrefix ($ _options ['prefix ' ]);
165123 }
166124
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- }
125+ if (isset ($ _options ['cluster ' ])) {
126+ $ keyHashTag = $ this ->selectKeyHashTag ($ _options );
127+ $ prefix = isset ($ _options ['prefix ' ]) ? $ _options ['prefix ' ] : '' ;
128+ $ _options ['prefix ' ] = $ keyHashTag . $ prefix ;
200129 }
130+
201131 $ redisConfigutation ['options ' ] = $ _options ;
202132 return $ redisConfigutation ;
203133 }
@@ -254,8 +184,7 @@ public function getKeys($pattern = '*')
254184 $ prefix = $ this ->client ->getOptions ()->__get ("prefix " )->getPrefix ();
255185 }
256186
257- if ($ this ->client ->getOptions ()->__isset ("distributedStrategy " ) &&
258- $ this ->client ->getOptions ()->__get ("distributedStrategy " ) == "cluster " ) {
187+ if ($ this ->client ->getOptions ()->__isset ("cluster " )) {
259188 $ keys = array ();
260189 foreach ($ this ->client as $ nodeClient ) {
261190 $ nodeClientKeys = $ nodeClient ->keys ($ pattern );
0 commit comments