|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soupmix\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Redis; |
|
6
|
|
|
|
|
7
|
|
|
class RedisCache implements CacheInterface |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
private static $defaults = [ |
|
11
|
|
|
'persistent' => null, |
|
12
|
|
|
'bucket' => 'default', |
|
13
|
|
|
'dbIndex' => 0, |
|
14
|
|
|
'port' => 6379, |
|
15
|
|
|
'timeout' => 2.5, |
|
16
|
|
|
'persistentId' => null, |
|
17
|
|
|
'reconnectAttempt' => 100 |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
private $serializer = Redis::SERIALIZER_PHP; |
|
21
|
|
|
|
|
22
|
|
|
public $handler = null; |
|
23
|
|
|
/** |
|
24
|
|
|
* Connect to Redis service |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $config Configuration values that has dbIndex name and host's IP address |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $config) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->handler = new Redis(); |
|
32
|
|
|
$redisConfig= $this::$defaults; |
|
33
|
|
|
foreach ($config as $key=>$value) { |
|
34
|
|
|
$redisConfig[$key] = $value; |
|
35
|
|
|
} |
|
36
|
|
|
if (file_exists('igbinary_serialize')) { |
|
37
|
|
|
$this->serializer = Redis::SERIALIZER_IGBINARY; |
|
38
|
|
|
} |
|
39
|
|
|
if ( isset($redisConfig['persistent']) && ($redisConfig['persistent'] === true)) { |
|
40
|
|
|
return $this->connect($redisConfig); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
$this->persistentConnect($redisConfig); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
private function connect( array $redisConfig){ |
|
|
|
|
|
|
46
|
|
|
$this->handler->connect( |
|
47
|
|
|
$redisConfig['host'], |
|
48
|
|
|
$redisConfig['port'], |
|
49
|
|
|
$redisConfig['timeout'], |
|
50
|
|
|
null, |
|
51
|
|
|
$redisConfig['reconnectAttempt'] |
|
52
|
|
|
); |
|
53
|
|
|
return $this->handler->select($redisConfig['dbIndex']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
private function persistentConnect( array $redisConfig){ |
|
|
|
|
|
|
57
|
|
|
$this->handler->pconnect( |
|
58
|
|
|
$redisConfig['host'], |
|
59
|
|
|
$redisConfig['port'], |
|
60
|
|
|
$redisConfig['timeout'], |
|
61
|
|
|
$redisConfig['persistentId'] |
|
62
|
|
|
); |
|
63
|
|
|
return $this->handler->select($redisConfig['dbIndex']); |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Fetch a value from the cache. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $key The unique key of this item in the cache |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed The value of the item from the cache, or null in case of cache miss |
|
73
|
|
|
*/ |
|
74
|
|
|
public function get($key) |
|
75
|
|
|
{ |
|
76
|
|
|
$value = $this->handler->get($key); |
|
77
|
|
|
return ($value) ? $this->unserialize($value) : null; |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* Persist data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $key The key of the item to store |
|
83
|
|
|
* @param mixed $value The value of the item to store |
|
84
|
|
|
* @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and the driver supports TTL |
|
85
|
|
|
* then the library may set a default value for it or let the driver take care of that. |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool True on success and false on failure |
|
88
|
|
|
*/ |
|
89
|
|
|
public function set($key, $value, $ttl = null){ |
|
90
|
|
|
$ttl = intval($ttl); |
|
91
|
|
|
$value = $this->serialize($value); |
|
92
|
|
|
if($ttl ==0 ){ |
|
93
|
|
|
return $this->handler->set($key, $value); |
|
94
|
|
|
} |
|
95
|
|
|
return $this->handler->set($key, $value, $ttl); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function serialize($value){ |
|
99
|
|
|
return ($this->serializer === Redis::SERIALIZER_IGBINARY) ? igbinary_serialize($value) : serialize($value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function unserialize($value){ |
|
103
|
|
|
return ($this->serializer === Redis::SERIALIZER_IGBINARY) ? igbinary_unserialize($value) : unserialize($value); |
|
104
|
|
|
} |
|
105
|
|
|
/** |
|
106
|
|
|
* Delete an item from the cache by its unique key |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $key The unique cache key of the item to delete |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool True on success and false on failure |
|
111
|
|
|
*/ |
|
112
|
|
|
public function delete($key){ |
|
113
|
|
|
return (bool) $this->handler->delete($key); |
|
114
|
|
|
} |
|
115
|
|
|
/** |
|
116
|
|
|
* Wipe clean the entire cache's keys |
|
117
|
|
|
* |
|
118
|
|
|
* @return bool True on success and false on failure |
|
119
|
|
|
*/ |
|
120
|
|
|
public function clear(){ |
|
121
|
|
|
return $this->handler->flushDb(); |
|
122
|
|
|
} |
|
123
|
|
|
/** |
|
124
|
|
|
* Obtain multiple cache items by their unique keys |
|
125
|
|
|
* |
|
126
|
|
|
* @param array|Traversable $keys A list of keys that can obtained in a single operation. |
|
127
|
|
|
* |
|
128
|
|
|
* @return array An array of key => value pairs. Cache keys that do not exist or are stale will have a value of null. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getMultiple($keys) |
|
131
|
|
|
{ |
|
132
|
|
|
return array_combine($keys, $this->handler->mGet($keys)); |
|
133
|
|
|
} |
|
134
|
|
|
/** |
|
135
|
|
|
* Persisting a set of key => value pairs in the cache, with an optional TTL. |
|
136
|
|
|
* |
|
137
|
|
|
* @param array|Traversable $items An array of key => value pairs for a multiple-set operation. |
|
138
|
|
|
* @param null|integer|DateInterval $ttl Optional. The amount of seconds from the current time that the item will exist in the cache for. |
|
139
|
|
|
* If this is null then the cache backend will fall back to its own default behaviour. |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool True on success and false on failure |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setMultiple($items, $ttl = null) |
|
144
|
|
|
{ |
|
145
|
|
|
if (($ttl === null) || ($ttl === 0)) { |
|
146
|
|
|
return $this->handler->mSet($items); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$return =[]; |
|
150
|
|
|
foreach ($items as $key=>$value) { |
|
151
|
|
|
$return[$key] = $this->set($key, $value, $ttl); |
|
152
|
|
|
} |
|
153
|
|
|
return $return; |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
/** |
|
156
|
|
|
* Delete multiple cache items in a single operation |
|
157
|
|
|
* |
|
158
|
|
|
* @param array|Traversable $keys The array of string-based keys to be deleted |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool True on success and false on failure |
|
161
|
|
|
*/ |
|
162
|
|
|
public function deleteMultiple($keys) |
|
163
|
|
|
{ |
|
164
|
|
|
$return =[]; |
|
165
|
|
|
foreach ($keys as $key) { |
|
166
|
|
|
$return[$key] = (bool) $this->delete($key); |
|
167
|
|
|
} |
|
168
|
|
|
return $return; |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
/** |
|
171
|
|
|
* Increment a value atomically in the cache by its step value, which defaults to 1 |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $key The cache item key |
|
174
|
|
|
* @param integer $step The value to increment by, defaulting to 1 |
|
175
|
|
|
* |
|
176
|
|
|
* @return int|bool The new value on success and false on failure |
|
177
|
|
|
*/ |
|
178
|
|
|
public function increment($key, $step = 1) |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->handler->incr($key, $step); |
|
181
|
|
|
} |
|
182
|
|
|
/** |
|
183
|
|
|
* Decrement a value atomically in the cache by its step value, which defaults to 1 |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $key The cache item key |
|
186
|
|
|
* @param integer $step The value to decrement by, defaulting to 1 |
|
187
|
|
|
* |
|
188
|
|
|
* @return int|bool The new value on success and false on failure |
|
189
|
|
|
*/ |
|
190
|
|
|
public function decrement($key, $step = 1) |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->handler->decr($key, $step); |
|
193
|
|
|
} |
|
194
|
|
|
} |