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 \Redis |
36
|
|
|
*/ |
37
|
|
|
private $redis; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* RedisAdapter constructor |
41
|
|
|
* @param array $params |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $params) |
44
|
|
|
{ |
45
|
|
|
$this->ttl = $params['ttl']; |
46
|
|
|
|
47
|
|
|
$this->redis = new Redis(); |
48
|
|
|
$this->redis->connect($params['host'], $params['port']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritDoc |
53
|
|
|
*/ |
54
|
|
|
public function get($key, $default = null) |
55
|
|
|
{ |
56
|
|
|
if ($this->has($key)) { |
57
|
|
|
$cacheItem = $this->redis->get(sha1($key)); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
return unserialize($cacheItem); |
61
|
|
|
} catch (Exception $e) { |
62
|
|
|
$this->delete($key); |
63
|
|
|
return $default; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $default; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function getMultiple($keys, $default = null) |
74
|
|
|
{ |
75
|
|
|
if (!is_array($keys)) { |
76
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$result = []; |
80
|
|
|
|
81
|
|
|
foreach ($keys as $key) { |
82
|
|
|
$result[$key] = $this->get($key, $default); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function has($key): bool |
92
|
|
|
{ |
93
|
|
|
$cacheItem = $this->redis->get(sha1($key)); |
94
|
|
|
|
95
|
|
|
if (!$cacheItem) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function set($key, $value, $ttl = null) |
106
|
|
|
{ |
107
|
|
|
return $this->redis->set(sha1($key), serialize($value), $this->ttl); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
* @throws InvalidArgumentException |
113
|
|
|
*/ |
114
|
|
|
public function setMultiple($values, $ttl = null) |
115
|
|
|
{ |
116
|
|
|
if (!is_array($values)) { |
117
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$results = []; |
121
|
|
|
|
122
|
|
|
foreach ($values as $key => $value) { |
123
|
|
|
$results[] = $this->set($key, $value, $ttl); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return !in_array(false, $results, true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
|
|
public function delete($key) |
133
|
|
|
{ |
134
|
|
|
return (bool) $this->redis->del(sha1($key)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritDoc |
139
|
|
|
* @throws InvalidArgumentException |
140
|
|
|
*/ |
141
|
|
|
public function deleteMultiple($keys) |
142
|
|
|
{ |
143
|
|
|
if (!is_array($keys)) { |
144
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$results = []; |
148
|
|
|
|
149
|
|
|
foreach ($keys as $key) { |
150
|
|
|
$results[] = $this->delete($key); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return !in_array(false, $results, true); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function clear() |
160
|
|
|
{ |
161
|
|
|
return $this->redis->flushdb(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|