1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Cache\Adapters; |
16
|
|
|
|
17
|
|
|
use Psr\SimpleCache\CacheInterface; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Exception; |
20
|
|
|
use Redis; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class RedisAdapter |
24
|
|
|
* @package Quantum\Libraries\Cache\Adapters |
25
|
|
|
*/ |
26
|
|
|
class RedisAdapter implements CacheInterface |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $ttl = 30; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $prefix; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Redis |
41
|
|
|
*/ |
42
|
|
|
private $redis; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* RedisAdapter constructor |
46
|
|
|
* @param array $params |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $params) |
49
|
|
|
{ |
50
|
|
|
$this->ttl = $params['ttl']; |
51
|
|
|
$this->prefix = $params['prefix']; |
52
|
|
|
|
53
|
|
|
$this->redis = new Redis(); |
54
|
|
|
$this->redis->connect($params['host'], $params['port']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function get($key, $default = null) |
61
|
|
|
{ |
62
|
|
|
if ($this->has($key)) { |
63
|
|
|
$cacheItem = $this->redis->get($this->keyHash($key)); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
return unserialize($cacheItem); |
67
|
|
|
} catch (Exception $e) { |
68
|
|
|
$this->delete($key); |
69
|
|
|
return $default; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $default; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function getMultiple($keys, $default = null) |
80
|
|
|
{ |
81
|
|
|
if (!is_array($keys)) { |
82
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$result = []; |
86
|
|
|
|
87
|
|
|
foreach ($keys as $key) { |
88
|
|
|
$result[$key] = $this->get($key, $default); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function has($key): bool |
98
|
|
|
{ |
99
|
|
|
$cacheItem = $this->redis->get($this->keyHash($key)); |
100
|
|
|
|
101
|
|
|
if (!$cacheItem) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
|
|
public function set($key, $value, $ttl = null) |
112
|
|
|
{ |
113
|
|
|
return $this->redis->set($this->keyHash($key), serialize($value), $this->ttl); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
* @throws InvalidArgumentException |
119
|
|
|
*/ |
120
|
|
|
public function setMultiple($values, $ttl = null) |
121
|
|
|
{ |
122
|
|
|
if (!is_array($values)) { |
123
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$results = []; |
127
|
|
|
|
128
|
|
|
foreach ($values as $key => $value) { |
129
|
|
|
$results[] = $this->set($key, $value, $ttl); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return !in_array(false, $results, true); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function delete($key) |
139
|
|
|
{ |
140
|
|
|
return (bool) $this->redis->del($this->keyHash($key)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
* @throws InvalidArgumentException |
146
|
|
|
*/ |
147
|
|
|
public function deleteMultiple($keys) |
148
|
|
|
{ |
149
|
|
|
if (!is_array($keys)) { |
150
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$results = []; |
154
|
|
|
|
155
|
|
|
foreach ($keys as $key) { |
156
|
|
|
$results[] = $this->delete($key); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return !in_array(false, $results, true); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
|
|
public function clear() |
166
|
|
|
{ |
167
|
|
|
return $this->redis->flushdb(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Gets the hashed key |
172
|
|
|
* @param string $key |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
private function keyHash(string $key): string |
176
|
|
|
{ |
177
|
|
|
return sha1($this->prefix . $key); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|