-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemcachedWrapper.php
149 lines (131 loc) · 4.49 KB
/
MemcachedWrapper.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
<?php
/**
* A lightweight wrapper around the PHP Memcached extension with three goals:
*
* - You can specify a prefix to prepend to all keys.
* - You can use it exactly like a regular Memcached object.
* - You can access the cache like an array.
*
* Example:
*
* $cache = new MemcachedWrapper('foo');
* $cache['bar'] = 'x'; // sets 'foobar' to 'x'
* isset($cache['bar']); // returns true
* unset($cache['bar']); // deletes 'foobar'
* $cache->set('bar', 'x') // sets 'foobar' to 'x'
*/
class MemcachedWrapper implements ArrayAccess {
/**
* Memcached methods that take key(s) as arguments, and the argument
* position of those key(s).
*/
protected $keyArgMethods = array(
'add' => 0,
'addByKey' => 1,
'append' => 0,
'appendByKey' => 0,
'cas' => 1,
'casByKey' => 2,
'decrement' => 0,
'delete' => 0,
'deleteByKey' => 1,
'get' => 0,
'getByKey' => 1,
'getDelayed' => 0,
'getDelayedByKey' => 1,
'getMulti' => 0,
'getMultiByKey' => 1,
'increment' => 0,
'prepend' => 0,
'prependByKey' => 1,
'replace' => 0,
'replaceByKey' => 1,
'set' => 0,
'setByKey' => 1,
'setMulti' => 0,
'setMultiByKey' => 1,
);
protected $prefix;
/**
* The underlying Memcached object, which you can access in order to
* override the prefix prepending if you really want.
*/
public $mc;
public function __construct($prefix='', $persistent_id=null) {
$this->prefix = $prefix;
if ($persistent_id !== null) {
$this->mc = new Memcached($persistent_id);
} else {
$this->mc = new Memcached();
}
}
public function __call($name, $args) {
if (!is_callable(array($this->mc, $name))) {
throw new MemcachedWrapperError("Unknown method: $name");
}
// find the position of the argument with key(s), if any
if (isset($this->keyArgMethods[$name])) {
$pos = $this->keyArgMethods[$name];
// prepend prefix to key(s)
if (strpos($name, 'setMulti') !== false) {
$new = array();
foreach ($args[$pos] as $k => $v) {
$new[$this->prefix . $k] = $v;
}
$args[$pos] = $new;
} else if (strpos($name, 'Multi') !== false ||
strpos($name, 'Delayed') !== false)
{
$new = array();
foreach ($args[$pos] as $k) {
$new[] = $this->prefix . $k;
}
$args[$pos] = $new;
} else {
$args[$pos] = $this->prefix . $args[$pos];
}
}
$result = call_user_func_array(array($this->mc, $name), $args);
// process keys in return value if necessary
$prefixLen = strlen($this->prefix);
$process = function ($r) use ($prefixLen) {
$r['key'] = substr($r['key'], $prefixLen);
return $r;
};
if ($name == 'fetch' && is_array($result)) {
return $process($result);
} else if ($name == 'fetchAll' && is_array($result)) {
return array_map($process, $result);
} else if (strpos($name, 'getMulti') === 0 && is_array($result)) {
$new = array();
foreach ($result as $k => $v) {
$new[substr($k, strlen($this->prefix))] = $v;
}
return $new;
} else {
return $result;
}
}
public function offsetExists($offset) {
if ($this->mc->get($this->prefix . $offset)) {
return true;
} else if ($this->mc->getResultCode() != Memcached::RES_NOTFOUND) {
return true;
} else {
return false;
}
}
public function offsetGet($offset) {
return $this->mc->get($this->prefix . $offset);
}
public function offsetSet($offset, $value) {
if ($offset === null) {
throw new MemcachedWrapperError("Tried to set null offset");
}
return $this->mc->set($this->prefix . $offset, $value);
}
public function offsetUnset($offset) {
return $this->mc->delete($this->prefix . $offset);
}
}
class MemcachedWrapperError extends Exception {}