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
|
6 |
|
public function __construct($memcached) |
28
|
|
|
{ |
29
|
6 |
|
if ($memcached instanceof \Memcached) { |
|
|
|
|
30
|
6 |
|
$this->memcached = $memcached; |
31
|
6 |
|
$this->installed = true; |
32
|
|
|
|
33
|
6 |
|
$this->setSettings(); |
34
|
6 |
|
} |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* set Memcached settings |
39
|
|
|
*/ |
40
|
6 |
|
private function setSettings() |
41
|
|
|
{ |
42
|
|
|
// Use faster compression if available |
43
|
6 |
|
if (\Memcached::HAVE_IGBINARY) { |
44
|
|
|
$this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
48
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT); |
49
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true); |
50
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true); |
51
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true); |
52
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_COMPRESSION, true); |
53
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_COMPRESSION_TYPE, \Memcached::COMPRESSION_FASTLZ); |
54
|
6 |
|
$this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* set cache-item by key => value |
59
|
|
|
* |
60
|
|
|
* @param string $key |
61
|
|
|
* @param mixed $value |
62
|
|
|
* |
63
|
|
|
* @return mixed|void |
64
|
|
|
*/ |
65
|
2 |
|
public function set($key, $value) |
66
|
|
|
{ |
67
|
|
|
// Make sure we are under the proper limit |
68
|
|
|
/* |
|
|
|
|
69
|
|
|
if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) { |
70
|
|
|
throw new \Exception('The passed cache key is over 250 bytes'); |
71
|
|
|
} |
72
|
|
|
*/ |
73
|
|
|
|
74
|
2 |
|
return $this->memcached->set($key, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* set cache-item by key => value + ttl |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @param int $ttl |
83
|
|
|
* |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
1 |
|
public function setExpired($key, $value, $ttl) |
87
|
|
|
{ |
88
|
1 |
|
if ($ttl > 2592000) { |
89
|
|
|
$ttl = 2592000; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $this->memcached->set($key, $value, $ttl); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* remove cached-item by key |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* |
100
|
|
|
* @return boolean |
101
|
|
|
*/ |
102
|
|
|
public function remove($key) |
103
|
|
|
{ |
104
|
|
|
return $this->memcached->delete($key); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* check if cached-item exists |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
1 |
|
public function exists($key) |
115
|
|
|
{ |
116
|
1 |
|
return $this->get($key) !== false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* get cached-item by key |
121
|
|
|
* |
122
|
|
|
* @param String $key |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
4 |
View Code Duplication |
public function get($key) |
|
|
|
|
127
|
|
|
{ |
128
|
4 |
|
static $memcachedCache; |
129
|
|
|
|
130
|
4 |
|
if (isset($memcachedCache[$key])) { |
131
|
1 |
|
return $memcachedCache[$key]; |
132
|
|
|
} else { |
133
|
3 |
|
$return = $this->memcached->get($key); |
134
|
3 |
|
$memcachedCache[$key] = $return; |
135
|
3 |
|
return $return; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* check if cache is installed |
141
|
|
|
* |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
6 |
|
public function installed() |
145
|
|
|
{ |
146
|
6 |
|
return $this->installed; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
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.