-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcachedStore.php
More file actions
193 lines (167 loc) · 4.52 KB
/
MemcachedStore.php
File metadata and controls
193 lines (167 loc) · 4.52 KB
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
<?php
/**
* Memcached Store
*
* @package SugiPHP.Cache
* @author Plamen Popov <tzappa@gmail.com>
* @license http://opensource.org/licenses/mit-license.php (MIT License)
*/
namespace SugiPHP\Cache;
use Memcached;
class MemcachedStore implements StoreInterface, IncrementorInterface
{
/**
* Memcached instance
*/
protected $memcached;
/**
* Fix bug in memcached PHP module
* @see https://bugs.php.net/bug.php?id=51434
* @var boolean
*/
public $bug51434fix = true;
/**
* Creates a Memcached store
*
* @param Memcached $memcached
*/
public function __construct(Memcached $memcached)
{
$this->memcached = $memcached;
}
/**
* Creates MemcachedStore instance
*
* @param array $config Server Configurations
*
* @return MemcachedStore
*/
public static function factory(array $config = array())
{
$memcached = new Memcached();
// empty config
if (empty($config)) {
$host = "127.0.0.1";
$port = 11211;
$memcached->addServer($host, $port);
} elseif (empty($config[0])) {
// only one server
$host = empty($config["host"]) ? "127.0.0.1" : $config["host"];
$port = empty($config["port"]) ? 11211 : $config["port"];
$weight = empty($config["weight"]) ? 1 : $config["weight"];
$memcached->addServer($host, $port, $weight);
} else {
// multiple servers
$memcached->addServers($config);
}
// The code using a store should work no matter if the store is running or not
// Check is the memcache store is working with checkRunning() method
return new MemcachedStore($memcached);
}
/**
* {@inheritdoc}
*/
public function add($key, $value, $ttl = 0)
{
return $this->memcached->add($key, $value, $ttl);
}
/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = 0)
{
return $this->memcached->set($key, $value, $ttl);
}
/**
* {@inheritdoc}
*/
public function get($key)
{
$result = $this->memcached->get($key);
if (($result === false) && ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND)) {
return null;
}
return $result;
}
/**
* {@inheritdoc}
*/
public function has($key)
{
$result = $this->memcached->get($key);
if (($result === false) && ($this->memcached->getResultCode() === Memcached::RES_NOTFOUND)) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function delete($key)
{
$this->memcached->delete($key);
}
/**
* {@inheritdoc}
*/
public function flush()
{
$this->memcached->flush();
}
/**
* {@inheritdoc}
*/
public function inc($key, $step = 1)
{
// due to a bug in memcached PHP module
// https://bugs.php.net/bug.php?id=51434
// we'll check if the $key has a non numeric value
if ($this->bug51434fix) {
$value = $this->get($key);
if (is_null($value) || !is_numeric($value)) {
return false;
}
}
$val = $this->memcached->increment($key, $step);
// on some servers it will return 0 instead of FALSE
return ($val) ? $val : false;
}
/**
* {@inheritdoc}
*/
public function dec($key, $step = 1)
{
// due to a bug in memcached PHP module
// https://bugs.php.net/bug.php?id=51434
// we'll check if the $key has a non numeric value
if ($this->bug51434fix) {
$value = $this->get($key);
if (is_null($value) || !is_numeric($value)) {
return false;
}
}
$val = $this->memcached->decrement($key, $step);
// on some servers it will return 0 instead of FALSE
return ($val) ? $val : false;
}
/**
* Checks is the memcache server is running
*
* @return boolean
*/
public function checkRunning()
{
$servers = $this->memcached->getVersion();
// this happens when no servers were added
if (!$servers) {
return false;
}
// at least one server should be running
foreach ($servers as $server) {
if ($server != "255.255.255") {
return true;
}
}
return false;
}
}