1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace voku\cache; |
4
|
|
|
|
5
|
|
|
use voku\cache\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* AdapterMemcached: Memcached-adapter |
9
|
|
|
* |
10
|
|
|
* @package voku\cache |
11
|
|
|
*/ |
12
|
|
|
class AdapterMemcached implements iAdapter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
public $installed = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Memcached |
21
|
|
|
*/ |
22
|
|
|
private $memcached; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* __construct |
26
|
|
|
* |
27
|
|
|
* @param \Memcached|null $memcached |
28
|
|
|
*/ |
29
|
9 |
|
public function __construct($memcached = null) |
30
|
|
|
{ |
31
|
9 |
|
if ($memcached instanceof \Memcached) { |
|
|
|
|
32
|
9 |
|
$this->setMemcached($memcached); |
33
|
9 |
|
} |
34
|
9 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \Memcached $memcached |
38
|
|
|
*/ |
39
|
9 |
|
public function setMemcached(\Memcached $memcached) { |
40
|
9 |
|
$this->memcached = $memcached; |
41
|
9 |
|
$this->installed = true; |
42
|
|
|
|
43
|
9 |
|
$this->setSettings(); |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
2 |
|
public function exists($key) |
50
|
|
|
{ |
51
|
2 |
|
return $this->get($key) !== false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
5 |
|
public function get($key) |
58
|
|
|
{ |
59
|
5 |
|
return $this->memcached->get($key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
9 |
|
public function installed() |
66
|
|
|
{ |
67
|
9 |
|
return $this->installed; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function remove($key) |
74
|
|
|
{ |
75
|
|
|
return $this->memcached->delete($key); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function removeAll() |
82
|
|
|
{ |
83
|
|
|
return $this->memcached->flush(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
3 |
View Code Duplication |
public function set($key, $value) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// Make sure we are under the proper limit |
92
|
3 |
|
if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) { |
93
|
|
|
throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true)); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
return $this->memcached->set($key, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
1 |
|
public function setExpired($key, $value, $ttl) |
103
|
|
|
{ |
104
|
1 |
|
if ($ttl > 2592000) { |
105
|
|
|
$ttl = 2592000; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $this->memcached->set($key, $value, $ttl); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the MemCached settings. |
113
|
|
|
*/ |
114
|
9 |
|
private function setSettings() |
115
|
|
|
{ |
116
|
|
|
// Use faster compression if available |
117
|
9 |
|
if (\Memcached::HAVE_IGBINARY) { |
118
|
|
|
$this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY); |
119
|
|
|
} |
120
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT); |
121
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true); |
122
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true); |
123
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true); |
124
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_COMPRESSION, false); |
125
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2); |
126
|
9 |
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.