1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix\Cache; |
4
|
|
|
|
5
|
|
|
use Soupmix\Cache\Exceptions\InvalidArgumentException; |
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use Redis; |
8
|
|
|
|
9
|
|
|
class RedisCache implements CacheInterface |
10
|
|
|
{ |
11
|
|
|
const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':']; |
12
|
|
|
|
13
|
|
|
private $handler = null; |
14
|
|
|
|
15
|
|
|
private $serializer = 'PHP'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Connect to Redis service |
19
|
|
|
* |
20
|
|
|
* @param Redis $handler Configuration values that has dbIndex name and host's IP address |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
8 |
|
public function __construct(Redis $handler) |
24
|
|
|
{ |
25
|
8 |
|
if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) { |
26
|
|
|
$handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); |
27
|
|
|
} |
28
|
8 |
|
$this->handler = $handler; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
|
|
public function getConnection() |
32
|
|
|
{ |
33
|
|
|
return $this->handler; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function get($key, $default = null) |
40
|
|
|
{ |
41
|
2 |
|
$value = $this->handler->get($key); |
42
|
2 |
|
return $value ? $value : $default; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function set($key, $value, $ttl = null) |
49
|
|
|
{ |
50
|
2 |
|
$ttl = (int) $ttl; |
51
|
2 |
|
$value = $value; |
|
|
|
|
52
|
2 |
|
if ($ttl === 0) { |
53
|
2 |
|
return $this->handler->set($key, $value); |
54
|
|
|
} |
55
|
|
|
return $this->handler->set($key, $value, $ttl); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
4 |
|
public function delete($key) |
62
|
|
|
{ |
63
|
4 |
|
return (bool) $this->handler->delete($key); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
8 |
|
public function clear() |
69
|
|
|
{ |
70
|
8 |
|
return $this->handler->flushDb(); |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
2 |
|
public function getMultiple($keys, $default = null) |
76
|
|
|
{ |
77
|
2 |
|
$defaults = array_fill(0, count($keys), $default); |
78
|
2 |
|
foreach ($keys as $key) { |
79
|
2 |
|
$this->checkReservedCharacters($key); |
80
|
2 |
|
} |
81
|
2 |
|
return array_merge(array_combine($keys, $this->handler->mGet($keys)), $defaults); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
2 |
|
public function setMultiple($values, $ttl = null) |
87
|
|
|
{ |
88
|
2 |
|
foreach ($values as $key => $value) { |
89
|
2 |
|
$this->checkReservedCharacters($key); |
90
|
2 |
|
} |
91
|
2 |
|
if (($ttl === null) || ($ttl === 0)) { |
92
|
2 |
|
return $this->handler->mSet($values); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$return =[]; |
96
|
|
|
foreach ($values as $key => $value) { |
97
|
|
|
$return[$key] = $this->set($key, $value, $ttl); |
98
|
|
|
} |
99
|
|
|
return $return; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
2 |
|
public function deleteMultiple($keys) |
105
|
|
|
{ |
106
|
2 |
|
foreach ($keys as $key) { |
107
|
2 |
|
$this->checkReservedCharacters($key); |
108
|
2 |
|
} |
109
|
2 |
|
$return =[]; |
110
|
2 |
|
foreach ($keys as $key) { |
111
|
2 |
|
$return[$key] = (bool) $this->delete($key); |
112
|
2 |
|
} |
113
|
2 |
|
return $return; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
/** |
116
|
|
|
* Increment a value atomically in the cache by its step value, which defaults to 1 |
117
|
|
|
* |
118
|
|
|
* @param string $key The cache item key |
119
|
|
|
* @param integer $step The value to increment by, defaulting to 1 |
120
|
|
|
* |
121
|
|
|
* @return int|bool The new value on success and false on failure |
122
|
|
|
*/ |
123
|
2 |
|
public function increment($key, $step = 1) |
124
|
|
|
{ |
125
|
2 |
|
return $this->handler->incr($key, $step); |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* Decrement a value atomically in the cache by its step value, which defaults to 1 |
129
|
|
|
* |
130
|
|
|
* @param string $key The cache item key |
131
|
|
|
* @param integer $step The value to decrement by, defaulting to 1 |
132
|
|
|
* |
133
|
|
|
* @return int|bool The new value on success and false on failure |
134
|
|
|
*/ |
135
|
2 |
|
public function decrement($key, $step = 1) |
136
|
|
|
{ |
137
|
2 |
|
return $this->handler->decr($key, $step); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritDoc} |
145
|
|
|
*/ |
146
|
|
|
public function has($key) |
147
|
|
|
{ |
148
|
|
|
$this->checkReservedCharacters($key); |
149
|
|
|
return $this->handler->exists($key); |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
private function checkReservedCharacters($key) |
153
|
|
|
{ |
154
|
|
|
|
155
|
2 |
|
if (!is_string($key)) { |
156
|
|
|
$message = sprintf('key %s is not a string.', $key); |
157
|
|
|
throw new InvalidArgumentException($message); |
158
|
|
|
} |
159
|
2 |
|
foreach (self::PSR16_RESERVED_CHARACTERS as $needle) { |
160
|
2 |
|
if (strpos($key, $needle) !== false) { |
161
|
|
|
$message = sprintf('%s string is not a legal value.', $key); |
162
|
|
|
throw new InvalidArgumentException($message); |
163
|
|
|
} |
164
|
2 |
|
} |
165
|
2 |
|
} |
166
|
|
|
} |
167
|
|
|
|
This checks looks for cases where a variable has been assigned to itself.
This assignement can be removed without consequences.