1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Linio\Component\Cache\Adapter; |
5
|
|
|
|
6
|
|
|
use Linio\Component\Cache\Exception\KeyNotFoundException; |
7
|
|
|
use Predis\Client; |
8
|
|
|
|
9
|
|
|
class RedisAdapter extends AbstractAdapter implements AdapterInterface |
10
|
|
|
{ |
11
|
|
|
const EXPIRE_RESOLUTION_EX = 'ex'; |
12
|
|
|
const EXPIRE_RESOLUTION_PX = 'px'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \Predis\Client |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $ttl; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
public function __construct(array $config = [], bool $lazy = true) |
30
|
|
|
{ |
31
|
|
|
$this->config = $config; |
32
|
|
|
|
33
|
|
|
if (!$lazy) { |
34
|
|
|
$this->getClient(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function getClient(): Client |
39
|
|
|
{ |
40
|
|
|
if (!$this->client instanceof Client) { |
41
|
|
|
$this->createClient($this->config); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->client; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function get(string $key) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$value = $this->getClient()->get($key); |
50
|
|
|
|
51
|
|
|
if ($value === null && !$this->getClient()->exists($key)) { |
52
|
|
|
throw new KeyNotFoundException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getMulti(array $keys): array |
59
|
|
|
{ |
60
|
|
|
$result = $this->getClient()->mget($keys); |
61
|
|
|
$values = []; |
62
|
|
|
|
63
|
|
|
foreach ($keys as $index => $key) { |
64
|
|
|
if ($result[$index] !== null) { |
65
|
|
|
$values[$key] = $result[$index]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $values; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function set(string $key, $value): bool |
73
|
|
|
{ |
74
|
|
|
if ($this->ttl == 0) { |
75
|
|
|
$result = $this->getClient()->set($key, $value); |
76
|
|
|
} else { |
77
|
|
|
$result = $this->getClient()->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result->getPayload() == 'OK'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setMulti(array $data): bool |
84
|
|
|
{ |
85
|
|
|
$responses = $this->getClient()->pipeline( |
86
|
|
|
function ($pipe) use ($data) { |
87
|
|
|
foreach ($data as $key => $value) { |
88
|
|
|
if ($this->ttl == 0) { |
89
|
|
|
$pipe->set($key, $value); |
90
|
|
|
} else { |
91
|
|
|
$pipe->set($key, $value, static::EXPIRE_RESOLUTION_EX, $this->ttl); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$result = true; |
98
|
|
|
foreach ($responses as $response) { |
|
|
|
|
99
|
|
|
/* @var $response \Predis\Response\Status */ |
100
|
|
|
$result = $result && ($response->getPayload() == 'OK'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function contains(string $key): bool |
107
|
|
|
{ |
108
|
|
|
return $this->getClient()->exists($key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function delete(string $key): bool |
112
|
|
|
{ |
113
|
|
|
$this->getClient()->del($key); |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function deleteMulti(array $keys): bool |
119
|
|
|
{ |
120
|
|
|
foreach ($keys as $key) { |
121
|
|
|
$this->getClient()->del($key); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function flush(): bool |
128
|
|
|
{ |
129
|
|
|
$result = $this->getClient()->flushAll(); |
130
|
|
|
|
131
|
|
|
return $result->getPayload() == 'OK'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setClient(Client $client) |
135
|
|
|
{ |
136
|
|
|
$this->client = $client; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function createClient(array $config) |
140
|
|
|
{ |
141
|
|
|
$this->client = new Client($this->getConnectionParameters($config), ['prefix' => null]); |
142
|
|
|
|
143
|
|
|
if (isset($config['ttl'])) { |
144
|
|
|
$this->ttl = (int) $config['ttl']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (isset($config['cache_not_found_keys'])) { |
148
|
|
|
$this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys']; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
protected function getConnectionParameters(array $config): array |
153
|
|
|
{ |
154
|
|
|
$connectionParameters = []; |
155
|
|
|
$connectionParameters['host'] = $config['host'] ?? '127.0.0.1'; |
156
|
|
|
$connectionParameters['port'] = $config['port'] ?? 6379; |
157
|
|
|
if (isset($config['database'])) { |
158
|
|
|
$connectionParameters['database'] = $config['database']; |
159
|
|
|
} |
160
|
|
|
if (isset($config['password'])) { |
161
|
|
|
$connectionParameters['password'] = $config['password']; |
162
|
|
|
} |
163
|
|
|
if (isset($config['connection_persistent'])) { |
164
|
|
|
$connectionParameters['connection_persistent'] = $config['connection_persistent']; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $connectionParameters; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function setNamespace(string $namespace) |
171
|
|
|
{ |
172
|
|
|
$this->getClient()->getOptions()->prefix->setPrefix($namespace . ':'); |
|
|
|
|
173
|
|
|
parent::setNamespace($namespace); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setTtl(int $ttl) |
177
|
|
|
{ |
178
|
|
|
$this->ttl = $ttl; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.