|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace voku\cache; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* AdapterMemcached: Memcached-adapter |
|
7
|
|
|
* |
|
8
|
|
|
* @package voku\cache |
|
9
|
|
|
*/ |
|
10
|
|
|
class AdapterMemcached implements iAdapter |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var bool |
|
14
|
|
|
*/ |
|
15
|
|
|
public $installed = false; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Memcached |
|
19
|
|
|
*/ |
|
20
|
|
|
private $memcached; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* __construct |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Memcached $memcached |
|
26
|
|
|
*/ |
|
27
|
9 |
|
public function __construct($memcached) |
|
28
|
|
|
{ |
|
29
|
9 |
|
if ($memcached instanceof \Memcached) { |
|
|
|
|
|
|
30
|
9 |
|
$this->memcached = $memcached; |
|
31
|
9 |
|
$this->installed = true; |
|
32
|
|
|
|
|
33
|
9 |
|
$this->setSettings(); |
|
34
|
9 |
|
} |
|
35
|
9 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* set Memcached settings |
|
39
|
|
|
*/ |
|
40
|
9 |
|
private function setSettings() |
|
41
|
|
|
{ |
|
42
|
|
|
// Use faster compression if available |
|
43
|
9 |
|
if (\Memcached::HAVE_IGBINARY) { |
|
44
|
|
|
$this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY); |
|
45
|
|
|
} |
|
46
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT); |
|
47
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true); |
|
48
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true); |
|
49
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true); |
|
50
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_COMPRESSION, false); |
|
51
|
9 |
|
$this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2); |
|
52
|
9 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* set cache-item by key => value |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @param mixed $value |
|
59
|
|
|
* |
|
60
|
|
|
* @return mixed|void |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function set($key, $value) |
|
63
|
|
|
{ |
|
64
|
|
|
// Make sure we are under the proper limit |
|
65
|
|
|
/* |
|
|
|
|
|
|
66
|
|
|
if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) { |
|
67
|
|
|
throw new \Exception('The passed cache key is over 250 bytes'); |
|
68
|
|
|
} |
|
69
|
|
|
*/ |
|
70
|
|
|
|
|
71
|
3 |
|
return $this->memcached->set($key, $value); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* set cache-item by key => value + ttl |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $key |
|
78
|
|
|
* @param mixed $value |
|
79
|
|
|
* @param int $ttl |
|
80
|
|
|
* |
|
81
|
|
|
* @return boolean |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function setExpired($key, $value, $ttl) |
|
84
|
|
|
{ |
|
85
|
1 |
|
if ($ttl > 2592000) { |
|
86
|
|
|
$ttl = 2592000; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return $this->memcached->set($key, $value, $ttl); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* remove cached-item by key |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
* |
|
97
|
|
|
* @return boolean |
|
98
|
|
|
*/ |
|
99
|
|
|
public function remove($key) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->memcached->delete($key); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* remove all cached items |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function removeAll() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->memcached->flush(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* check if cached-item exists |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $key |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function exists($key) |
|
122
|
|
|
{ |
|
123
|
2 |
|
return $this->get($key) !== false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* get cached-item by key |
|
128
|
|
|
* |
|
129
|
|
|
* @param String $key |
|
130
|
|
|
* |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
6 |
View Code Duplication |
public function get($key) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
6 |
|
static $memcachedCache; |
|
136
|
|
|
|
|
137
|
6 |
|
if (isset($memcachedCache[$key])) { |
|
138
|
2 |
|
return $memcachedCache[$key]; |
|
139
|
|
|
} else { |
|
140
|
4 |
|
$return = $this->memcached->get($key); |
|
141
|
4 |
|
$memcachedCache[$key] = $return; |
|
142
|
4 |
|
return $return; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* check if cache is installed |
|
148
|
|
|
* |
|
149
|
|
|
* @return boolean |
|
150
|
|
|
*/ |
|
151
|
9 |
|
public function installed() |
|
152
|
|
|
{ |
|
153
|
9 |
|
return $this->installed; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto 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
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.