1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\Cache\Engine; |
4
|
|
|
|
5
|
|
|
use ByJG\Cache\CacheEngineInterface; |
6
|
|
|
use Memcached; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
|
9
|
|
|
class MemcachedEngine implements CacheEngineInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var Memcached |
15
|
|
|
*/ |
16
|
|
|
protected $memCached = null; |
17
|
|
|
|
18
|
|
|
protected $logger = null; |
19
|
|
|
|
20
|
|
|
protected $servers = null; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function __construct($servers = null, $logger = null) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->servers = (array)$servers; |
25
|
|
|
if (is_null($servers)) { |
26
|
|
|
$this->servers = [ |
27
|
|
|
'127.0.0.1:11211' |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->logger = $logger; |
32
|
|
|
if (is_null($logger)) { |
33
|
|
|
$this->logger = new NullLogger(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function lazyLoadMemCachedServers() |
38
|
|
|
{ |
39
|
|
|
if (is_null($this->memCached)) { |
40
|
|
|
$this->memCached = new Memcached(); |
41
|
|
|
foreach ($this->servers as $server) { |
42
|
|
|
$data = explode(":", $server); |
43
|
|
|
$this->memCached->addServer($data[0], $data[1]); |
44
|
|
|
|
45
|
|
|
$stats = $this->memCached->getStats(); |
46
|
|
|
if (!isset($stats[$server]) || $stats[$server]['pid'] === -1) { |
47
|
|
|
throw new \Exception("Memcached server $server is down"); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $key The object KEY |
55
|
|
|
* @param int $ttl IGNORED IN MEMCACHED. |
56
|
|
|
* @return object Description |
57
|
|
|
*/ |
58
|
|
|
public function get($key, $ttl = 0) |
59
|
|
|
{ |
60
|
|
|
$this->lazyLoadMemCachedServers(); |
61
|
|
|
|
62
|
|
|
$value = $this->memCached->get($key); |
63
|
|
View Code Duplication |
if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) { |
|
|
|
|
64
|
|
|
$this->logger->info("[Memcached] Cache '$key' missed with status " . $this->memCached->getResultCode()); |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $key The object Key |
73
|
|
|
* @param object $object The object to be cached |
74
|
|
|
* @param int $ttl The time to live in seconds of this objects |
75
|
|
|
* @return bool If the object is successfully posted |
76
|
|
|
*/ |
77
|
|
|
public function set($key, $object, $ttl = 0) |
78
|
|
|
{ |
79
|
|
|
$this->lazyLoadMemCachedServers(); |
80
|
|
|
|
81
|
|
|
$this->memCached->set($key, $object, $ttl); |
82
|
|
|
$this->logger->info("[Memcached] Set '$key' result " . $this->memCached->getResultCode()); |
83
|
|
View Code Duplication |
if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) { |
|
|
|
|
84
|
|
|
$this->logger->error("[Memcached] Set '$key' failed with status " . $this->memCached->getResultCode()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->memCached->getResultCode() === Memcached::RES_SUCCESS; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Unlock resource |
92
|
|
|
* @param string $key |
93
|
|
|
*/ |
94
|
|
|
public function release($key) |
95
|
|
|
{ |
96
|
|
|
$this->lazyLoadMemCachedServers(); |
97
|
|
|
|
98
|
|
|
$this->memCached->delete($key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @param string $str |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function append($key, $str) |
108
|
|
|
{ |
109
|
|
|
$this->lazyLoadMemCachedServers(); |
110
|
|
|
|
111
|
|
|
$this->logger->info("[Memcached] Append '$key' in Memcached"); |
112
|
|
|
return $this->memCached->append($key, $str); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Lock resource before set it. |
117
|
|
|
* @param string $key |
118
|
|
|
*/ |
119
|
|
|
public function lock($key) |
120
|
|
|
{ |
121
|
|
|
$this->lazyLoadMemCachedServers(); |
122
|
|
|
|
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* UnLock resource after set it |
128
|
|
|
* @param string $key |
129
|
|
|
*/ |
130
|
|
|
public function unlock($key) |
131
|
|
|
{ |
132
|
|
|
$this->lazyLoadMemCachedServers(); |
133
|
|
|
|
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function isAvailable() |
138
|
|
|
{ |
139
|
|
|
if (!class_exists('\Memcached')) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
try { |
144
|
|
|
$this->lazyLoadMemCachedServers(); |
145
|
|
|
return true; |
146
|
|
|
} catch (\Exception $ex) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.