1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Cache; |
6
|
|
|
|
7
|
|
|
use Suricate; |
8
|
|
|
use Predis\Client; |
9
|
|
|
use \Exception; |
10
|
|
|
use \BadMethodCallException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Redis extension for Suricate |
14
|
|
|
* |
15
|
|
|
* @package Suricate |
16
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @property string $host redis host (default: localhost) |
19
|
|
|
* @property int $port redis port (default: 6379) |
20
|
|
|
* @property int $defaultExpiry Key default expiry |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
class Redis extends Suricate\Cache |
24
|
|
|
{ |
25
|
|
|
protected $parametersList = ['host', 'port', 'defaultExpiry']; |
26
|
|
|
|
27
|
|
|
/** @var Client|false $handler */ |
28
|
|
|
private $handler; |
29
|
|
|
|
30
|
4 |
|
public function __construct() |
31
|
|
|
{ |
32
|
4 |
|
parent::__construct(); |
33
|
|
|
|
34
|
4 |
|
$this->handler = false; |
35
|
4 |
|
$this->host = 'localhost'; |
36
|
4 |
|
$this->port = 6379; |
37
|
4 |
|
$this->defaultExpiry = 3600; |
38
|
4 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get redis host |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
public function getHost(): string |
46
|
|
|
{ |
47
|
1 |
|
return $this->host; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set Redis host |
52
|
|
|
* |
53
|
|
|
* @param string $host redis hostname/ip |
54
|
|
|
* |
55
|
|
|
* @return Redis |
56
|
|
|
*/ |
57
|
1 |
|
public function setHost(string $host): Redis |
58
|
|
|
{ |
59
|
1 |
|
$this->host = $host; |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get Redis port |
66
|
|
|
* |
67
|
|
|
* @return integer |
68
|
|
|
*/ |
69
|
1 |
|
public function getPort(): int |
70
|
|
|
{ |
71
|
1 |
|
return $this->port; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set Redis port |
76
|
|
|
* |
77
|
|
|
* @param int $port Redis port |
78
|
|
|
* |
79
|
|
|
* @return Redis |
80
|
|
|
*/ |
81
|
1 |
|
public function setPort(int $port): Redis |
82
|
|
|
{ |
83
|
1 |
|
$this->port = intval($port); |
84
|
|
|
|
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get default cache expiration duration |
90
|
|
|
* |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
1 |
|
public function getDefaultExpiry(): int |
94
|
|
|
{ |
95
|
1 |
|
return $this->defaultExpiry; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set default cache expiration duration |
100
|
|
|
* |
101
|
|
|
* @param integer $expiry |
102
|
|
|
* |
103
|
|
|
* @return Redis |
104
|
|
|
*/ |
105
|
1 |
|
public function setDefaultExpiry(int $expiry): Redis |
106
|
|
|
{ |
107
|
1 |
|
$this->defaultExpiry = $expiry; |
108
|
|
|
|
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Connect to Redis host |
114
|
|
|
* |
115
|
|
|
* @throws Exception |
116
|
|
|
* @throws BadMethodCallException |
117
|
|
|
* |
118
|
|
|
* @return Redis |
119
|
|
|
*/ |
120
|
|
|
private function connect(): Redis |
121
|
|
|
{ |
122
|
|
|
if ($this->handler === false) { |
123
|
|
|
if (class_exists('\Predis\Client')) { |
124
|
|
|
$redisHandler = new Client([ |
125
|
|
|
'scheme' => 'tcp', |
126
|
|
|
'host' => $this->host, |
127
|
|
|
'port' => $this->port |
128
|
|
|
]); |
129
|
|
|
if ($redisHandler->connect() === false) { |
|
|
|
|
130
|
|
|
throw new Exception('Can\'t connect to redis server'); |
131
|
|
|
} |
132
|
|
|
$this->handler = $redisHandler; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
throw new BadMethodCallException('Can\'t find Redis extension'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Put a value into redis |
145
|
|
|
* @param string $keyname Key name |
146
|
|
|
* @param mixed $value Value |
147
|
|
|
* @param int $expiry Cache expiry |
148
|
|
|
* @throws Exception |
149
|
|
|
* @throws BadMethodCallException |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function set(string $keyname, $value, $expiry = null) |
154
|
|
|
{ |
155
|
|
|
$this->connect(); |
156
|
|
|
if ($this->handler === false) { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($expiry === null) { |
161
|
|
|
$expiry = $this->defaultExpiry; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$retVal = $this->handler->set($keyname, $value); |
165
|
|
|
$expireRetVal = true; |
166
|
|
|
if ($expiry !== -1) { |
167
|
|
|
$expireRetVal = $this->handler->expire($keyname, $expiry); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $retVal && $expireRetVal; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get a cached value from keyname |
175
|
|
|
* |
176
|
|
|
* @param string $keyname |
177
|
|
|
* @throws Exception |
178
|
|
|
* @throws BadMethodCallException |
179
|
|
|
* |
180
|
|
|
* @return string|null |
181
|
|
|
*/ |
182
|
|
|
public function get(string $keyname): ?string |
183
|
|
|
{ |
184
|
|
|
$this->connect(); |
185
|
|
|
if ($this->handler) { |
186
|
|
|
return $this->handler->get($keyname); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return null; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Delete a variable from redis |
194
|
|
|
* |
195
|
|
|
* @param string $keyname |
196
|
|
|
* @throws Exception |
197
|
|
|
* @throws BadMethodCallException |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function delete(string $keyname): bool |
202
|
|
|
{ |
203
|
|
|
$this->connect(); |
204
|
|
|
if ($this->handler) { |
205
|
|
|
return (bool) $this->handler->del([$keyname]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|