1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Cache\Adapters; |
16
|
|
|
|
17
|
|
|
use Quantum\Exceptions\CacheException; |
18
|
|
|
use Psr\SimpleCache\CacheInterface; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
use Memcached; |
21
|
|
|
use Exception; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class MemcachedAdapter |
25
|
|
|
* @package Quantum\Libraries\Cache\Adapters |
26
|
|
|
*/ |
27
|
|
|
class MemcachedAdapter implements CacheInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $ttl = 30; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $prefix; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Memcached |
42
|
|
|
*/ |
43
|
|
|
private $memcached; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* MemcachedAdapter constructor |
47
|
|
|
* @param array $params |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $params) |
50
|
|
|
{ |
51
|
|
|
$this->ttl = $params['ttl']; |
52
|
|
|
$this->prefix = $params['prefix']; |
53
|
|
|
|
54
|
|
|
$this->memcached = new Memcached(); |
55
|
|
|
$this->memcached->addServer($params['host'], $params['port']); |
56
|
|
|
|
57
|
|
|
if (!$this->memcached->getStats()) { |
58
|
|
|
throw CacheException::cantConnect('Memcached server'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function get($key, $default = null) |
66
|
|
|
{ |
67
|
|
|
if ($this->has($key)) { |
68
|
|
|
$cacheItem = $this->memcached->get($this->keyHash($key)); |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
return unserialize($cacheItem); |
72
|
|
|
} catch (Exception $e) { |
73
|
|
|
$this->delete($key); |
74
|
|
|
return $default; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $default; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
*/ |
84
|
|
|
public function getMultiple($keys, $default = null) |
85
|
|
|
{ |
86
|
|
|
if (!is_array($keys)) { |
87
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$result = []; |
91
|
|
|
|
92
|
|
|
foreach ($keys as $key) { |
93
|
|
|
$result[$key] = $this->get($key, $default); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
public function has($key): bool |
103
|
|
|
{ |
104
|
|
|
$cacheItem = $this->memcached->get($this->keyHash($key)); |
105
|
|
|
|
106
|
|
|
if (!$cacheItem) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function set($key, $value, $ttl = null) |
117
|
|
|
{ |
118
|
|
|
return $this->memcached->set($this->keyHash($key), serialize($value), $this->ttl); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritDoc |
123
|
|
|
* @throws InvalidArgumentException |
124
|
|
|
*/ |
125
|
|
|
public function setMultiple($values, $ttl = null) |
126
|
|
|
{ |
127
|
|
|
if (!is_array($values)) { |
128
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$results = []; |
132
|
|
|
|
133
|
|
|
foreach ($values as $key => $value) { |
134
|
|
|
$results[] = $this->set($key, $value, $ttl); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return !in_array(false, $results, true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
|
|
public function delete($key) |
144
|
|
|
{ |
145
|
|
|
return $this->memcached->delete($this->keyHash($key)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritDoc |
150
|
|
|
* @throws InvalidArgumentException |
151
|
|
|
*/ |
152
|
|
|
public function deleteMultiple($keys) |
153
|
|
|
{ |
154
|
|
|
if (!is_array($keys)) { |
155
|
|
|
throw new InvalidArgumentException(t(_message('exception.non_iterable_value', '$values')), E_WARNING); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$results = []; |
159
|
|
|
|
160
|
|
|
foreach ($keys as $key) { |
161
|
|
|
$results[] = $this->delete($key); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return !in_array(false, $results, true); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public function clear() |
171
|
|
|
{ |
172
|
|
|
return $this->memcached->flush(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Gets the hashed key |
177
|
|
|
* @param string $key |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
private function keyHash(string $key): string |
181
|
|
|
{ |
182
|
|
|
return sha1($this->prefix . $key); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|