|
1
|
|
|
<?php /** MicroMemcachedDriver */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Cache\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class MemcachedDriver |
|
9
|
|
|
* |
|
10
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
11
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
12
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
|
13
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
|
14
|
|
|
* @package Micro |
|
15
|
|
|
* @subpackage Cache\Driver |
|
16
|
|
|
* @version 1.0 |
|
17
|
|
|
* @since 1.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
class MemcachedDriver extends CacheDriver |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var \Memcache|\Memcached $driver driver memcache(d) */ |
|
22
|
|
|
protected $driver; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor |
|
27
|
|
|
* |
|
28
|
|
|
* @access public |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $config config array |
|
31
|
|
|
* |
|
32
|
|
|
* @result void |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(array $config = []) |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct($config); |
|
38
|
|
|
|
|
39
|
|
|
if (empty($config['type']) || !$this->check()) { |
|
40
|
|
|
throw new Exception('Memcache(d) not installed or not select type'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
switch (strtolower($config['type'])) { |
|
44
|
|
|
case 'memcached': |
|
45
|
|
|
$this->driver = new \Memcached; |
|
46
|
|
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case 'memcache': |
|
49
|
|
|
$this->driver = new \Memcache; |
|
50
|
|
|
break; |
|
51
|
|
|
|
|
52
|
|
|
default: |
|
53
|
|
|
throw new Exception('Selected type not valid in the driver'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($config['servers'])) { |
|
57
|
|
|
$this->driver->addServers($config['servers']); |
|
|
|
|
|
|
58
|
|
|
} elseif ($config['server']) { |
|
59
|
|
|
$conf = $config['server']; |
|
60
|
|
|
$server = [ |
|
61
|
|
|
'hostname' => !empty($conf['hostname']) ? $conf['hostname'] : '127.0.0.1', |
|
62
|
|
|
'port' => !empty($conf['port']) ? $conf['port'] : 11211, |
|
63
|
|
|
'weight' => !empty($conf['weight']) ? $conf['weight'] : 1 |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
if (get_class($this->driver) === 'Memcached') { |
|
67
|
|
|
$this->driver->addServer($server['hostname'], $server['port'], $server['weight']); |
|
68
|
|
|
} else { |
|
69
|
|
|
$this->driver->addServer($server['hostname'], $server['port'], true, $server['weight']); |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
throw new Exception('Server(s) not configured'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function check() |
|
80
|
|
|
{ |
|
81
|
|
|
return (!extension_loaded('memcached') && !extension_loaded('memcache')) ?: true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Destructor |
|
86
|
|
|
* |
|
87
|
|
|
* @access public |
|
88
|
|
|
* @result void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __destruct() |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->driver) { |
|
93
|
|
|
$this->driver->close(); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
|
|
public function get($name) |
|
101
|
|
|
{ |
|
102
|
|
|
$data = $this->driver->get($name); |
|
103
|
|
|
|
|
104
|
|
|
return is_array($data) ? $data[0] : $data; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @inheritdoc |
|
109
|
|
|
*/ |
|
110
|
|
|
public function set($name, $value, $duration = 0) |
|
111
|
|
|
{ |
|
112
|
|
|
switch (get_class($this->driver)) { |
|
113
|
|
|
case 'Memcached': |
|
114
|
|
|
return $this->driver->set($name, $value, $duration); |
|
115
|
|
|
|
|
116
|
|
|
case 'Memcache': |
|
117
|
|
|
return $this->driver->set($name, $value, 0, $duration); |
|
118
|
|
|
|
|
119
|
|
|
default: |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
|
|
public function delete($name) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->driver->delete($name); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @inheritdoc |
|
134
|
|
|
*/ |
|
135
|
|
|
public function clean() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->driver->flush(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritdoc |
|
142
|
|
|
*/ |
|
143
|
|
|
public function info() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->driver->getStats(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @inheritdoc |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getMeta($id) |
|
152
|
|
|
{ |
|
153
|
|
|
$stored = $this->driver->get($id); |
|
154
|
|
|
if (count($stored) !== 3) { |
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
list($data, $time, $ttl) = $stored; |
|
159
|
|
|
|
|
160
|
|
|
return ['expire' => $time + $ttl, 'mtime' => $time, 'data' => $data]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
*/ |
|
166
|
|
|
public function increment($name, $offset = 1) |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->driver->increment($name, $offset); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @inheritdoc |
|
173
|
|
|
*/ |
|
174
|
|
|
public function decrement($name, $offset = 1) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->driver->decrement($name, $offset); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: