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