|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 4/6/15 |
|
5
|
|
|
* Time: 6:31 PM |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Cache\Adapter\Memcached; |
|
12
|
|
|
|
|
13
|
|
|
use \Memcached as MemcachedDriver; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Memcached |
|
17
|
|
|
* @package NilPortugues\Cache\Adapter\Memcached |
|
18
|
|
|
*/ |
|
19
|
|
|
class Memcached implements MemcachedClient |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Memcached |
|
23
|
|
|
*/ |
|
24
|
|
|
private $memcached; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $persistentId |
|
28
|
|
|
* @param array $connections |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($persistentId, array $connections) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->isMemcachedExtensionAvailable(); |
|
33
|
|
|
|
|
34
|
|
|
$this->memcached = new MemcachedDriver($persistentId); |
|
35
|
|
|
$this->memcached->addServers($connections); |
|
36
|
|
|
|
|
37
|
|
|
$this->memcached->setOption( |
|
38
|
|
|
MemcachedDriver::OPT_SERIALIZER, |
|
39
|
|
|
(\defined(MemcachedDriver::HAVE_IGBINARY) && MemcachedDriver::HAVE_IGBINARY) |
|
40
|
|
|
? MemcachedDriver::SERIALIZER_IGBINARY : MemcachedDriver::SERIALIZER_PHP |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$this->memcached->setOption(MemcachedDriver::OPT_DISTRIBUTION, MemcachedDriver::DISTRIBUTION_CONSISTENT); |
|
44
|
|
|
$this->memcached->setOption(MemcachedDriver::OPT_LIBKETAMA_COMPATIBLE, true); |
|
45
|
|
|
$this->memcached->setOption(MemcachedDriver::OPT_BINARY_PROTOCOL, true); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
* @codeCoverageIgnore |
|
52
|
|
|
*/ |
|
53
|
|
|
private function isMemcachedExtensionAvailable() |
|
54
|
|
|
{ |
|
55
|
|
|
if (false === \class_exists('\Memcached')) { |
|
56
|
|
|
throw new \Exception('Memcached extension for PHP is not installed on the system.'); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $key |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get($key) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->memcached->get($key); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $key |
|
72
|
|
|
* @param $value |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function set($key, $value) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->memcached->set($key, $value); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $key |
|
83
|
|
|
* @param $expiration |
|
84
|
|
|
*/ |
|
85
|
|
|
public function touch($key, $expiration) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->memcached->touch($key, $expiration); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $key |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function delete($key) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->memcached->delete($key); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getStats() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->memcached->getStats(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function flush() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->memcached->flush(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|