-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memcache.php
249 lines (216 loc) · 6.58 KB
/
Memcache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/*
* This file is part of the Stash package.
*
* (c) Robert Hafner <tedivm@tedivm.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Stash\Driver;
use Stash;
use Stash\Exception\RuntimeException;
use Stash\Driver\Sub\Memcache as SubMemcache;
use Stash\Driver\Sub\Memcached as SubMemcached;
/**
* Memcache is a wrapper around the popular memcache server. Memcache supports both memcache php
* extensions and allows access to all of their options as well as all Stash features (including hierarchical caching).
*
* @package Stash
* @author Robert Hafner <tedivm@tedivm.com>
*/
class Memcache extends AbstractDriver
{
/**
* Memcache subdriver used by this class.
*
* @var SubMemcache|SubMemcached
*/
protected $memcache;
/**
* Cache of calculated keys.
*
* @var array
*/
protected $keyCache = array();
/**
* Timestamp of last time the key cache was updated.
*
* @var int timestamp
*/
protected $keyCacheTime = 0;
/**
* Limit
*
* @var int seconds
*/
protected $keyCacheTimeLimit = 1;
/**
* {@inheritdoc}
*/
public function getDefaultOptions()
{
return array(
'keycache_limit' => 1,
);
}
/**
*
* * servers - An array of servers, with each server represented by its own array (array(host, port, [weight])). If
* not passed the default is array('127.0.0.1', 11211).
*
* * extension - Which php extension to use, either 'memcache' or 'memcache'. Defaults to memcache with memcache
* as a fallback.
*
* * Options can be passed to the "memcache" driver by adding them to the options array. The memcache extension
* defined options using constants, ie Memcached::OPT_*. By passing in the * portion ('compression' for
* Memcached::OPT_COMPRESSION) and its respective option. Please see the php manual for the specific options
* (http://us2.php.net/manual/en/memcache.constants.php)
*
*
* @param array $options
*
* @throws RuntimeException
*/
public function setOptions(array $options = array())
{
$options += $this->getDefaultOptions();
if (!isset($options['servers'])) {
$options['servers'] = array('127.0.0.1', 11211);
}
$servers = $this->normalizeServerConfig($options['servers']);
if (!isset($options['extension'])) {
$options['extension'] = 'any';
}
$this->keyCacheTimeLimit = (int) $options['keycache_limit'];
$extension = strtolower($options['extension']);
if (class_exists('Memcached', false) && $extension != 'memcache') {
$this->memcache = new SubMemcached($servers, $options);
} elseif (class_exists('Memcache', false) && $extension != 'memcached') {
$this->memcache = new SubMemcache($servers);
} else {
throw new RuntimeException('No memcache extension available.');
}
}
protected function normalizeServerConfig($servers)
{
if (is_scalar($servers[0])) {
$servers = array($servers);
}
$normalizedServers = array();
foreach ($servers as $server) {
$host = '127.0.0.1';
if (isset($server['host'])) {
$host = $server['host'];
} elseif (isset($server[0])) {
$host = $server[0];
}
$port = '11211';
if (isset($server['port'])) {
$port = $server['port'];
} elseif (isset($server[1])) {
$port = $server[1];
}
$weight = null;
if (isset($server['weight'])) {
$weight = $server['weight'];
} elseif (isset($server[2])) {
$weight = $server[2];
}
$normalizedServers[] = array($host, $port, $weight);
}
return $normalizedServers;
}
/**
* {@inheritdoc}
*/
public function getData($key)
{
return $this->memcache->get($this->makeKeyString($key));
}
/**
* {@inheritdoc}
*/
public function storeData($key, $data, $expiration)
{
return $this->memcache->set($this->makeKeyString($key), $data, $expiration);
}
/**
* {@inheritdoc}
*/
public function clear($key = null)
{
$this->keyCache = array();
if (is_null($key)) {
$this->memcache->flush();
} else {
$keyString = $this->makeKeyString($key, true);
$this->memcache->inc($keyString);
$this->keyCache = array();
$this->makeKeyString($key);
}
$this->keyCache = array();
return true;
}
/**
* {@inheritdoc}
*/
public function purge()
{
return true;
}
/**
* Turns a key array into a key string. This includes running the indexing functions used to manage the memcached
* hierarchical storage.
*
* When requested the actual path, rather than a normalized value, is returned.
*
* @param array $key
* @param bool $path
* @return string
*/
protected function makeKeyString($key, $path = false)
{
$key = \Stash\Utilities::normalizeKeys($key);
$keyString = ':cache:::';
$pathKey = ':pathdb::';
$time = microtime(true);
if (($time - $this->keyCacheTime) >= $this->keyCacheTimeLimit) {
$this->keyCacheTime = $time;
$this->keyCache = array();
}
foreach ($key as $name) {
//a. cache:::name
//b. cache:::name0:::sub
$keyString .= $name;
//a. :pathdb::cache:::name
//b. :pathdb::cache:::name0:::sub
$pathKey = ':pathdb::' . $keyString;
$pathKey = md5($pathKey);
if (isset($this->keyCache[$pathKey])) {
$index = $this->keyCache[$pathKey];
} else {
$index = $this->memcache->cas($pathKey, 0);
$this->keyCache[$pathKey] = $index;
}
//a. cache:::name0:::
//b. cache:::name0:::sub1:::
$keyString .= '_' . $index . ':::';
}
return $path ? $pathKey : md5($keyString);
}
/**
* {@inheritdoc}
*/
public static function isAvailable()
{
return (SubMemcache::isAvailable() || SubMemcached::isAvailable());
}
/**
* {@inheritdoc}
*/
public function isPersistent()
{
return true;
}
}