1 | <?php |
||
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['persistent_id'])) { |
||
37 | $persistentId = (string) $config['persistent_id']; |
||
38 | } |
||
39 | |||
40 | $this->memcached = new Memcached($persistentId); |
||
41 | $this->memcached->addServers($config['servers']); |
||
42 | |||
43 | if (isset($config['options']) && !empty($config['options'])) { |
||
44 | $this->memcached->setOptions($config['options']); |
||
|
|||
45 | } |
||
46 | |||
47 | if ($this->namespace) { |
||
48 | $this->memcached->setOption(Memcached::OPT_PREFIX_KEY, $this->namespace); |
||
49 | } |
||
50 | |||
51 | if (isset($config['cache_not_found_keys'])) { |
||
52 | $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys']; |
||
53 | } |
||
54 | } |
||
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 | return $this->memcached->getMulti($keys); |
||
70 | } |
||
71 | |||
72 | public function set(string $key, $value): bool |
||
73 | { |
||
74 | return $this->memcached->set($key, $value, $this->ttl); |
||
75 | } |
||
76 | |||
77 | public function setMulti(array $data): bool |
||
78 | { |
||
79 | return $this->memcached->setMulti($data, $this->ttl); |
||
80 | } |
||
81 | |||
82 | public function contains(string $key): bool |
||
83 | { |
||
84 | try { |
||
85 | $this->get($key); |
||
86 | } catch (KeyNotFoundException $exception) { |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | return true; |
||
91 | } |
||
92 | |||
93 | public function delete(string $key): bool |
||
94 | { |
||
95 | $this->memcached->delete($key); |
||
96 | |||
97 | return true; |
||
98 | } |
||
99 | |||
100 | public function deleteMulti(array $keys): bool |
||
101 | { |
||
102 | $this->memcached->deleteMulti($keys); |
||
103 | |||
104 | return true; |
||
105 | } |
||
106 | |||
107 | public function flush(): bool |
||
111 | |||
112 | protected function validateMemcacheConfiguration(array $config) |
||
136 | } |
||
137 |
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.