-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPRedis.php
More file actions
305 lines (279 loc) · 9.6 KB
/
PRedis.php
File metadata and controls
305 lines (279 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace SplitIO\Component\Cache\Storage\Adapter;
use SplitIO\Component\Cache\Storage\Exception\AdapterException;
use SplitIO\Component\Utils as SplitIOUtils;
use SplitIO\Component\Common\Context;
/**
* Class PRedis
*
* @package SplitIO\Component\Cache\Storage\Adapter
*/
class PRedis implements CacheStorageAdapterInterface
{
/** @var \Predis\Client|null */
private $client = null;
/**
* @param array $options
* @throws AdapterException
*/
public function __construct(array $options)
{
if (!class_exists('\Predis\Client')) {
throw new AdapterException("PRedis class is not loaded");
}
$_redisConfig = $this->getRedisConfiguration($options);
$this->client = new \Predis\Client($_redisConfig['redis'], $_redisConfig['options']);
}
/**
* @param array $nodes
* @param string $type
* @return string|null
*/
private function isValidConfigArray($nodes, $type)
{
if (!is_array($nodes)) {
return $type . "s must be an array.";
}
if (empty($nodes)) {
return "At least one " . $type . " is required.";
}
if (SplitIOUtils\isAssociativeArray($nodes)) {
return $type . "s must not be an associative array.";
}
return null;
}
/**
* @param array $sentinels
* @param array $options
* @return bool
* @throws AdapterException
*/
private function isValidSentinelConfig($sentinels, $options)
{
$msg = $this->isValidConfigArray($sentinels, 'sentinel');
if (!is_null($msg)) {
throw new AdapterException($msg);
}
if (!isset($options['service'])) {
throw new AdapterException('Master name is required in replication mode for sentinel.');
}
return true;
}
private function validateKeyHashTag($keyHashTag)
{
if (!is_string($keyHashTag)) {
return array('valid' => false, 'msg' => 'keyHashTag must be string.');
}
if ((strlen($keyHashTag) < 3) || ($keyHashTag[0] != "{") ||
(substr($keyHashTag, -1) != "}") || (substr_count($keyHashTag, "{") != 1) ||
(substr_count($keyHashTag, "}") != 1)) {
return array('valid' => false, 'msg' => 'keyHashTag is not valid.');
}
return array('valid' => true, 'msg' => '');
}
/**
* @param mixed $options
* @return string
* @throws AdapterException
*/
private function getDefaultKeyHashTag($options)
{
if (!isset($options['keyHashTag'])) {
return "{SPLITIO}";
}
$validation = $this->validateKeyHashTag($options['keyHashTag']);
if (!($validation['valid'])) {
throw new AdapterException($validation['msg']);
}
return $options['keyHashTag'];
}
/**
* @param mixed $options
* @return string
* @throws AdapterException
*/
private function selectKeyHashTag($options)
{
if (!isset($options['keyHashTags'])) { // check if array keyHashTags is set
return $this->getDefaultKeyHashTag($options); // defaulting to keyHashTag or {SPLITIO}
}
$keyHashTags = $options['keyHashTags'];
$msg = $this->isValidConfigArray($keyHashTags, 'keyHashTag'); // check if is valid array
if (!is_null($msg)) {
throw new AdapterException($msg);
}
$filteredArray = array_filter( // filter to only use string element {X}
$keyHashTags,
function ($value) {
return $this->validateKeyHashTag($value)['valid'];
}
);
if (empty($filteredArray)) {
throw new AdapterException('keyHashTags size is zero after filtering valid elements.');
}
return $selected = $filteredArray[array_rand($filteredArray, 1)];
}
/**
* @param array $clusters
* @return bool
* @throws AdapterException
*/
private function isValidClusterConfig($clusters)
{
$msg = $this->isValidConfigArray($clusters, 'clusterNode');
if (!is_null($msg)) {
throw new AdapterException($msg);
}
return true;
}
/**
* @param mixed $options
* @return array
* @throws AdapterException
*/
private function getRedisConfiguration($options)
{
$redisConfigutation = array(
'redis' => null,
'options' => null
);
$parameters = (isset($options['parameters'])) ? $options['parameters'] : null;
$sentinels = (isset($options['sentinels'])) ? $options['sentinels'] : null;
$clusters = (isset($options['clusterNodes'])) ? $options['clusterNodes'] : null;
$_options = (isset($options['options'])) ? $options['options'] : null;
if (isset($_options['distributedStrategy']) && isset($parameters['tls'])) {
throw new AdapterException("SSL/TLS cannot be used together with sentinel/cluster yet");
}
if ($_options && isset($_options['prefix'])) {
$_options['prefix'] = self::normalizePrefix($_options['prefix']);
}
if (isset($parameters)) {
$redisConfigutation['redis'] = $parameters;
} else {
// @TODO remove this statement when replication will be deprecated
if (isset($_options['replication'])) {
Context::getLogger()->warning("'replication' option was deprecated please use 'distributedStrategy'");
if (!isset($_options['distributedStrategy'])) {
$_options['distributedStrategy'] = $_options['replication'];
}
}
if (isset($_options['distributedStrategy'])) {
switch ($_options['distributedStrategy']) {
case 'cluster':
if ($this->isValidClusterConfig($clusters)) {
$keyHashTag = $this->selectKeyHashTag($_options);
$_options['cluster'] = 'redis';
$redisConfigutation['redis'] = $clusters;
$prefix = isset($_options['prefix']) ? $_options['prefix'] : '';
$_options['prefix'] = $keyHashTag . $prefix;
}
break;
case 'sentinel':
if ($this->isValidSentinelConfig($sentinels, $_options)) {
$_options['replication'] = 'sentinel';
$redisConfigutation['redis'] = $sentinels;
}
break;
default:
throw new AdapterException("Wrong configuration of redis 'distributedStrategy'.");
}
} else {
throw new AdapterException("Wrong configuration of redis.");
}
}
$redisConfigutation['options'] = $_options;
return $redisConfigutation;
}
/**
* @param string $key
* @return string
*/
public function get($key)
{
return $this->client->get($key);
}
/**
* Returns a traversable set of cache items.
*
* @param array $keys
* An indexed array of keys of items to retrieve.
*
* @throws \InvalidArgumentException
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return array
*/
public function fetchMany(array $keys = array())
{
$toReturn = array();
if (empty($keys)) {
return $toReturn;
}
$values = $this->client->mget($keys);
foreach ($keys as $index => $key) {
$toReturn[$key] = $values[$index];
}
return $toReturn;
}
/**
* @param $key
* @param $value
* @return mixed
*/
public function isOnList($key, $value)
{
return $this->client->sIsMember($key, $value);
}
public function getKeys($pattern = '*')
{
$prefix = null;
if ($this->client->getOptions()->__isset("prefix")) {
$prefix = $this->client->getOptions()->__get("prefix")->getPrefix();
}
if ($this->client->getOptions()->__isset("distributedStrategy") &&
$this->client->getOptions()->__get("distributedStrategy") == "cluster") {
$keys = array();
foreach ($this->client as $nodeClient) {
$nodeClientKeys = $nodeClient->keys($pattern);
$keys = array_merge($keys, $nodeClientKeys);
}
} else {
$keys = $this->client->keys($pattern);
}
if ($prefix) {
if (is_array($keys)) {
foreach ($keys as $index => $key) {
$keys[$index] = str_replace($prefix, '', $key);
}
} else {
$keys = str_replace($prefix, '', $keys);
}
}
return $keys;
}
private static function normalizePrefix($prefix)
{
if ($prefix && is_string($prefix) && strlen($prefix)) {
if ($prefix[strlen($prefix) - 1] == '.') {
return $prefix;
} else {
return $prefix.'.';
}
} else {
return null;
}
}
public function rightPushQueue($queueName, $item)
{
if (!is_array($item)) {
return $this->client->rpush($queueName, array($item));
} else {
return $this->client->rpush($queueName, $item);
}
}
public function expireKey($key, $ttl)
{
return $this->client->expire($key, $ttl);
}
}