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