3
3
4
4
use SplitIO \Component \Cache \Storage \Exception \AdapterException ;
5
5
use SplitIO \Component \Utils as SplitIOUtils ;
6
- use SplitIO \Component \Common \Context ;
7
6
8
7
/**
9
8
* Class PRedis
@@ -15,6 +14,9 @@ class PRedis implements CacheStorageAdapterInterface
15
14
/** @var \Predis\Client|null */
16
15
private $ client = null ;
17
16
17
+ /** @var string */
18
+ private $ prefix = "" ;
19
+
18
20
/**
19
21
* @param array $options
20
22
* @throws AdapterException
@@ -26,7 +28,11 @@ public function __construct(array $options)
26
28
}
27
29
$ _redisConfig = $ this ->getRedisConfiguration ($ options );
28
30
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
+ }
30
36
}
31
37
32
38
/**
@@ -48,24 +54,6 @@ private function isValidConfigArray($nodes, $type)
48
54
return null ;
49
55
}
50
56
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
-
69
57
private function validateKeyHashTag ($ keyHashTag )
70
58
{
71
59
if (!is_string ($ keyHashTag )) {
@@ -121,83 +109,32 @@ function ($value) {
121
109
if (empty ($ filteredArray )) {
122
110
throw new AdapterException ('keyHashTags size is zero after filtering valid elements. ' );
123
111
}
124
- return $ selected = $ filteredArray [array_rand ($ filteredArray , 1 )];
112
+ return $ filteredArray [array_rand ($ filteredArray , 1 )];
125
113
}
126
114
127
115
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
-
142
116
/**
143
117
* @param mixed $options
144
118
* @return array
145
- * @throws AdapterException
146
119
*/
147
120
private function getRedisConfiguration ($ options )
148
121
{
149
122
$ redisConfigutation = array (
150
- 'redis ' => null ,
151
- 'options ' => null
123
+ 'parameters ' => ( isset ( $ options [ ' parameters ' ])) ? $ options [ ' parameters ' ] : null ,
124
+ 'options ' => null ,
152
125
);
153
126
154
- $ parameters = (isset ($ options ['parameters ' ])) ? $ options ['parameters ' ] : null ;
155
- $ sentinels = (isset ($ options ['sentinels ' ])) ? $ options ['sentinels ' ] : null ;
156
- $ clusters = (isset ($ options ['clusterNodes ' ])) ? $ options ['clusterNodes ' ] : null ;
157
127
$ _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
-
163
128
if ($ _options && isset ($ _options ['prefix ' ])) {
164
129
$ _options ['prefix ' ] = self ::normalizePrefix ($ _options ['prefix ' ]);
165
130
}
166
131
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 ;
200
136
}
137
+
201
138
$ redisConfigutation ['options ' ] = $ _options ;
202
139
return $ redisConfigutation ;
203
140
}
@@ -249,31 +186,8 @@ public function isOnList($key, $value)
249
186
250
187
public function getKeys ($ pattern = '* ' )
251
188
{
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 );
277
191
}
278
192
279
193
private static function normalizePrefix ($ prefix )
0 commit comments