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