1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Linio\Component\Cache\Adapter; |
6
|
|
|
|
7
|
|
|
use Linio\Component\Cache\Exception\InvalidConfigurationException; |
8
|
|
|
use Linio\Component\Cache\Exception\KeyNotFoundException; |
9
|
|
|
use Memcached; |
10
|
|
|
|
11
|
|
|
class MemcachedAdapter extends AbstractAdapter implements AdapterInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
protected $ttl; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Memcached |
20
|
|
|
*/ |
21
|
|
|
protected $memcached; |
22
|
|
|
|
23
|
|
|
public function __construct(array $config = []) |
24
|
|
|
{ |
25
|
|
|
$this->validateMemcacheConfiguration($config); |
26
|
|
|
|
27
|
|
|
// default config |
28
|
|
|
$this->ttl = 0; |
29
|
|
|
|
30
|
|
|
// config |
31
|
|
|
if (isset($config['ttl'])) { |
32
|
|
|
$this->ttl = $config['ttl']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$persistentId = null; |
36
|
|
|
if (isset($config['connection_persistent']) && $config['connection_persistent']) { |
37
|
|
|
$persistentId = '1'; |
38
|
|
|
|
39
|
|
|
if (isset($config['pool_size']) && $config['pool_size'] > 1) { |
40
|
|
|
$persistentId = (string) mt_rand(1, $config['pool_size']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->memcached = new Memcached($persistentId); |
45
|
|
|
$this->memcached->addServers($config['servers']); |
46
|
|
|
|
47
|
|
|
if (isset($config['options']) && !empty($config['options'])) { |
48
|
|
|
$this->memcached->setOptions($config['options']); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->namespace) { |
52
|
|
|
$this->memcached->setOption(Memcached::OPT_PREFIX_KEY, $this->namespace); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (isset($config['cache_not_found_keys'])) { |
56
|
|
|
$this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys']; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function get(string $key) |
61
|
|
|
{ |
62
|
|
|
$value = $this->memcached->get($key); |
63
|
|
|
|
64
|
|
|
if ($this->memcached->getResultCode() == Memcached::RES_NOTFOUND) { |
65
|
|
|
throw new KeyNotFoundException(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getMulti(array $keys): array |
72
|
|
|
{ |
73
|
|
|
return $this->memcached->getMulti($keys); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function set(string $key, $value): bool |
77
|
|
|
{ |
78
|
|
|
return $this->memcached->set($key, $value, $this->ttl); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setMulti(array $data): bool |
82
|
|
|
{ |
83
|
|
|
return $this->memcached->setMulti($data, $this->ttl); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function contains(string $key): bool |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
$this->get($key); |
90
|
|
|
} catch (KeyNotFoundException $exception) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function delete(string $key): bool |
98
|
|
|
{ |
99
|
|
|
$this->memcached->delete($key); |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function deleteMulti(array $keys): bool |
105
|
|
|
{ |
106
|
|
|
$this->memcached->deleteMulti($keys); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function flush(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->memcached->flush(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function validateMemcacheConfiguration(array $config) |
117
|
|
|
{ |
118
|
|
|
if (!array_key_exists('servers', $config)) { |
119
|
|
|
throw new InvalidConfigurationException('Missing configuration parameter: servers'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!is_array($config['servers'])) { |
123
|
|
|
throw new InvalidConfigurationException('Invalid configuration parameter: servers'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($config['servers'] as $server) { |
127
|
|
|
if (!is_array($server) || count($server) < 2 || count($server) > 3 || !is_numeric($server[1]) || (isset($server[2]) && !is_numeric($server[2]))) { |
128
|
|
|
throw new InvalidConfigurationException('Invalid configuration parameter: servers'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (array_key_exists('options', $config) && !is_array($config['options'])) { |
133
|
|
|
throw new InvalidConfigurationException('Invalid configuration parameter: options'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (isset($config['ttl']) && !is_numeric($config['ttl'])) { |
137
|
|
|
throw new InvalidConfigurationException('Invalid configuration parameter: ttl'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setNamespace(string $namespace) |
142
|
|
|
{ |
143
|
|
|
parent::setNamespace($namespace); |
144
|
|
|
$this->memcached->setOption(Memcached::OPT_PREFIX_KEY, $this->namespace); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.