1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon Simple Cache package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\SimpleCache\Driver; |
14
|
|
|
|
15
|
|
|
use Shieldon\SimpleCache\CacheProvider; |
16
|
|
|
use Shieldon\SimpleCache\Exception\CacheException; |
17
|
|
|
use Memcached as MemcachedServer; |
18
|
|
|
use Exception; |
19
|
|
|
use function array_keys; |
20
|
|
|
use function unserialize; |
21
|
|
|
use function serialize; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A cache driver class provided by libMemcached |
25
|
|
|
* |
26
|
|
|
* @see https://www.php.net/manual/en/book.memcached.php |
27
|
|
|
*/ |
28
|
|
|
class Memcached extends CacheProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The Memcached instance. |
32
|
|
|
* |
33
|
|
|
* @var Memcached|null |
34
|
|
|
*/ |
35
|
|
|
protected $memcached = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param array $setting The settings. |
41
|
|
|
* |
42
|
|
|
* @throws CacheException |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $setting = []) |
45
|
|
|
{ |
46
|
|
|
$config = [ |
47
|
|
|
'host' => '127.0.0.1', |
48
|
|
|
'port' => 11211, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
foreach (array_keys($config) as $key) { |
52
|
|
|
if (isset($setting[$key])) { |
53
|
|
|
$config[$key] = $setting[$key]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->connect($config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Connect to Memchaced server. |
62
|
|
|
* |
63
|
|
|
* @param array $config The settings. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
* |
67
|
|
|
* @throws CacheException |
68
|
|
|
*/ |
69
|
|
|
protected function connect(array $config): void |
70
|
|
|
{ |
71
|
|
|
if (extension_loaded('memcached')) { |
72
|
|
|
try { |
73
|
|
|
$this->memcached = new MemcachedServer(); |
|
|
|
|
74
|
|
|
$this->memcached->addServer( |
75
|
|
|
$config['host'], |
76
|
|
|
$config['port'], |
77
|
|
|
1 |
78
|
|
|
); |
79
|
|
|
// @codeCoverageIgnoreStart |
80
|
|
|
} catch (Exception $e) { |
81
|
|
|
throw new CacheException($e->getMessage()); |
82
|
|
|
} |
83
|
|
|
// @codeCoverageIgnoreEnd |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// @codeCoverageIgnoreStart |
88
|
|
|
throw new CacheException( |
89
|
|
|
'PHP Memcached extension is not installed on your system.' |
90
|
|
|
); |
91
|
|
|
// @codeCoverageIgnoreEnd |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Fetch a cache by an extended Cache Driver. |
96
|
|
|
* |
97
|
|
|
* @param string $key The key of a cache. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
protected function doGet(string $key): array |
102
|
|
|
{ |
103
|
|
|
$content = $this->memcached->get($key); |
|
|
|
|
104
|
|
|
|
105
|
|
|
if (empty($content)) { |
106
|
|
|
return []; |
107
|
|
|
} |
108
|
|
|
$data = unserialize($content); |
109
|
|
|
|
110
|
|
|
return $data; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set a cache by an extended Cache Driver. |
115
|
|
|
* |
116
|
|
|
* @param string $key The key of a cache. |
117
|
|
|
* @param mixed $value The value of a cache. (serialized) |
118
|
|
|
* @param int $ttl The time to live for a cache. |
119
|
|
|
* @param int $timestamp The time to store a cache. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
124
|
|
|
{ |
125
|
|
|
$contents = [ |
126
|
|
|
'timestamp' => $timestamp, |
127
|
|
|
'ttl' => $ttl, |
128
|
|
|
'value' => $value |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$result = $this->memcached->set( |
132
|
|
|
$key, |
133
|
|
|
serialize($contents), |
134
|
|
|
$ttl |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $result; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Delete a cache by an extended Cache Driver. |
142
|
|
|
* |
143
|
|
|
* @param string $key The key of a cache. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
protected function doDelete(string $key): bool |
148
|
|
|
{ |
149
|
|
|
return $this->memcached->delete($key); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Delete all caches by an extended Cache Driver. |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
protected function doClear(): bool |
158
|
|
|
{ |
159
|
|
|
return $this->memcached->flush(); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Check if the cache exists or not. |
164
|
|
|
* |
165
|
|
|
* @param string $key The key of a cache. |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
protected function doHas(string $key): bool |
170
|
|
|
{ |
171
|
|
|
$content = $this->memcached->get($key); |
172
|
|
|
|
173
|
|
|
if (empty($content)) { |
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..