1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NilPortugues\Cache\Adapter; |
4
|
|
|
|
5
|
|
|
use NilPortugues\Cache\Adapter\Memcached\Memcached; |
6
|
|
|
use NilPortugues\Cache\CacheAdapter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class MemcachedAdapter |
10
|
|
|
* @package NilPortugues\Cache\Adapter |
11
|
|
|
*/ |
12
|
|
|
class MemcachedAdapter extends Adapter implements CacheAdapter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Memcached |
16
|
|
|
*/ |
17
|
|
|
protected $memcached; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $persistentId |
21
|
|
|
* @param array $connections |
22
|
|
|
* @param CacheAdapter $next |
23
|
|
|
*/ |
24
|
|
|
public function __construct($persistentId, array $connections, CacheAdapter $next = null) |
25
|
|
|
{ |
26
|
|
|
$this->memcached = $this->getMemcachedClient($persistentId, \array_unique(\array_values($connections))); |
27
|
|
|
$this->nextAdapter = (InMemoryAdapter::getInstance() === $next) ? null : $next; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $persistentId |
32
|
|
|
* @param array $connections |
33
|
|
|
* |
34
|
|
|
* @codeCoverageIgnore |
35
|
|
|
* @return Memcached |
36
|
|
|
*/ |
37
|
|
|
protected function getMemcachedClient($persistentId, array $connections) |
38
|
|
|
{ |
39
|
|
|
return new Memcached($persistentId, $connections); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get a value identified by $key. |
44
|
|
|
* |
45
|
|
|
* @param string $key |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function get($key) |
50
|
|
|
{ |
51
|
|
|
$this->hit = false; |
52
|
|
|
|
53
|
|
|
$inMemoryValue = InMemoryAdapter::getInstance()->get($key); |
54
|
|
|
|
55
|
|
|
if (InMemoryAdapter::getInstance()->isHit()) { |
56
|
|
|
$this->hit = true; |
57
|
|
|
return $inMemoryValue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$value = $this->memcached->get($key); |
61
|
|
|
|
62
|
|
|
if ($value) { |
63
|
|
|
$this->hit = true; |
64
|
|
|
InMemoryAdapter::getInstance()->set($key, $value, 0); |
65
|
|
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return (null !== $this->nextAdapter) ? $this->nextAdapter->get($key) : null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set a value identified by $key and with an optional $ttl. |
73
|
|
|
* |
74
|
|
|
* @param string $key |
75
|
|
|
* @param mixed $value |
76
|
|
|
* @param int $ttl |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function set($key, $value, $ttl = 0) |
81
|
|
|
{ |
82
|
|
|
$ttl = $this->fromDefaultTtl($ttl); |
83
|
|
|
|
84
|
|
|
if ($ttl >= 0) { |
85
|
|
|
$this->memcached->set($key, $value); |
86
|
|
|
|
87
|
|
|
if ($ttl > 0) { |
88
|
|
|
$this->memcached->touch($key, \time() + $ttl); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->setChain($key, $value, $ttl); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Delete a value identified by $key. |
99
|
|
|
* |
100
|
|
|
* @param string $key |
101
|
|
|
*/ |
102
|
|
|
public function delete($key) |
103
|
|
|
{ |
104
|
|
|
$this->memcached->delete($key); |
105
|
|
|
$this->deleteChain($key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks the availability of the cache service. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function isAvailable() |
114
|
|
|
{ |
115
|
|
|
$stats = $this->memcached->getStats(); |
116
|
|
|
return false === empty($stats); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Clears all expired values from cache. |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function clear() |
125
|
|
|
{ |
126
|
|
|
$this->clearChain(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Clears all values from the cache. |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function drop() |
135
|
|
|
{ |
136
|
|
|
$this->memcached->flush(); |
137
|
|
|
$this->dropChain(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|