-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcachedHandler.php
More file actions
executable file
·189 lines (173 loc) · 5.16 KB
/
MemcachedHandler.php
File metadata and controls
executable file
·189 lines (173 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace MaplePHP\Cache\Handlers;
use Psr\Cache\CacheItemInterface;
use MaplePHP\Cache\Exceptions\CacheException;
use MaplePHP\Cache\CachePoolAbstract;
use Memcached;
class MemcachedHandler extends CachePoolAbstract
{
public const HOST = "127.0.0.1";
public const PORT = 11211;
public const WEIGHT = 0;
private $handler;
private $servers = [];
private $stats;
/**
* Init the memcache handler
* @param string|array $host
* @param int|null $port
* @param int $weight
*/
public function __construct(string|array $host, ?int $port = null, int $weight = 0)
{
if (!class_exists("Memcached")) {
throw new CacheException("The PHP package \"Memcached\" is missing!", 1);
}
$this->handler = new Memcached();
if (is_string($host)) {
$this->servers[] = [$host, $port, $weight];
} else {
$this->servers = $host;
}
$this->connect();
}
/**
* Get all set keys
* e.g. Some key may have already expired and won't be removed before @mem->get("KEY_NAME") has been called!
* @return array
*/
public function getAllKeys(): array
{
$arr = $this->handler->getAllKeys();
if ($arr === false) {
$this->validate();
return [];
}
return $arr;
}
/**
* This will Pass on cache content to CacheAbstract::getItem
* @param CacheItemInterface $item
* @return void
*/
protected function setItem(CacheItemInterface $item): void
{
$key = $item->getKey();
if ($data = $this->handler->get($key)) {
if (($data = unserialize($data)) && isset($data['expiresAfter'])) {
$item->set($data['value']);
$item->expiresAfter((int)$data['expiresAfter']);
}
}
}
/**
* Clear and remove all cache items and data
* @return bool
*/
protected function setClear(): bool
{
if (($data = $this->getAllKeys()) && count($data) > 0) {
$rsp = $this->handler->deleteMulti($data);
$this->validate();
foreach ($rsp as $v) {
if ((int)$v !== 1) {
return false;
}
}
return true;
}
return false;
}
/**
* Clear and remove cache item and data
* @param string $key
* @return bool
*/
protected function setDelete($key): bool
{
$bool = $this->handler->delete($key);
$this->validate();
return $bool;
}
/**
* Create cache
* @param CacheItemInterface $item
* @return bool
*/
protected function setSave(CacheItemInterface $item): bool
{
$data = serialize([
"value" => $item->get(),
"expiresAfter" => $this->setExpiration($item)
]);
$bool = $this->handler->set($item->getKey(), $data, $item->getExpiration());
$this->validate();
return $bool;
}
/**
* Get Memcached handler
* @return Memcached
*/
final public function getMemcached(): Memcached
{
return $this->handler;
}
/**
* Validate response
* @return bool
*/
final protected function validate(): bool
{
if (
$this->handler->getResultCode() !== Memcached::RES_SUCCESS &&
$this->handler->getResultCode() !== Memcached::RES_NOTFOUND
) {
throw new CacheException($this->handler->getResultMessage(), 1);
//return false;
}
return true;
}
/**
* Connect to server-/s
* @return void
*/
final protected function connect(): void
{
if ($this->stats === null) {
$this->validateServers();
$this->handler->addServers($this->servers);
$this->stats = $this->handler->getStats();
if ($this->stats === false) {
throw new CacheException("One or more server connection has failed!", 1);
}
}
}
/**
* Validate server information
* @return void
*/
final protected function validateServers(): void
{
if (count($this->servers) > 0) {
foreach ($this->servers as $server) {
$host = ($server[0] ?? null);
$port = ($server[1] ?? null);
$weight = ($server[1] ?? 0);
if (!is_string($host)) {
throw new CacheException("Expecting a string value in argumnet 1 (IP/Host) but " .
"got instead a " . gettype($host) . ".", 1);
}
if (!is_int($port)) {
throw new CacheException("Expecting a int value argumnet 2 (port) but " .
"got instead a " . gettype($port) . ".", 1);
}
if (!is_int($weight)) {
throw new CacheException("Expecting a int value argumnet 3 (weight) but " .
"got instead a " . gettype($weight) . ".", 1);
}
}
} else {
throw new CacheException("Could not find any servers", 1);
}
}
}