memcached.php
6.2 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Caching engine - Memcache |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
+-----------------------------------------------------------------------+
*/
/**
* Interface implementation class for accessing Memcached cache
*
* @package Framework
* @subpackage Cache
*/
class rcube_cache_memcached extends rcube_cache
{
/**
* Instance of memcached handler
*
* @var Memcached
*/
protected static $memcache;
/**
* {@inheritdoc}
*/
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
{
parent::__construct($userid, $prefix, $ttl, $packed, $indexed);
$this->type = 'memcache';
$this->debug = rcube::get_instance()->config->get('memcache_debug');
// Maximum TTL is 30 days, bigger values are treated by Memcached
// as unix timestamp which is not what we want
if ($this->ttl > 60*60*24*30) {
$this->ttl = 60*60*24*30;
}
self::engine();
}
/**
* Get global handle for memcache access
*
* @return object Memcache
*/
public static function engine()
{
if (self::$memcache !== null) {
return self::$memcache;
}
// no memcache support in PHP
if (!class_exists('Memcached')) {
self::$memcache = false;
rcube::raise_error([
'code' => 604, 'type' => 'memcache', 'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to find Memcached. Make sure php-memcached is installed"
],
true, true);
}
// add all configured hosts to pool
$rcube = rcube::get_instance();
$pconnect = $rcube->config->get('memcache_pconnect', true);
$timeout = $rcube->config->get('memcache_timeout', 1);
$retry_interval = $rcube->config->get('memcache_retry_interval', 15);
$hosts = $rcube->config->get('memcache_hosts');
$persistent_id = $pconnect ? ('rc' . md5(serialize($hosts))) : null;
self::$memcache = new Memcached($persistent_id);
self::$memcache->setOptions([
Memcached::OPT_CONNECT_TIMEOUT => $timeout * 1000,
Memcached::OPT_RETRY_TIMEOUT => $timeout,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
Memcached::OPT_COMPRESSION => true,
]);
if (!$pconnect || !count(self::$memcache->getServerList())) {
foreach ((array) $hosts as $host) {
if (substr($host, 0, 7) != 'unix://') {
list($host, $port) = explode(':', $host);
if (!$port) $port = 11211;
}
else {
$host = substr($host, 7);
$port = 0;
}
self::$memcache->addServer($host, $port);
}
}
// test connection
$result = self::$memcache->increment('__CONNECTIONTEST__');
if ($result === false && ($res_code = self::$memcache->getResultCode()) !== Memcached::RES_NOTFOUND) {
self::$memcache = false;
rcube::raise_error([
'code' => 604, 'type' => 'memcache', 'line' => __LINE__, 'file' => __FILE__,
'message' => "Memcache connection failure (code: $res_code)."
],
true, false);
}
return self::$memcache;
}
/**
* Remove cache records older than ttl
*/
public function expunge()
{
// No need for GC, entries are expunged automatically
}
/**
* Remove expired records of all caches
*/
public static function gc()
{
// No need for GC, entries are expunged automatically
}
/**
* Reads cache entry.
*
* @param string $key Cache internal key name
*
* @return mixed Cached value
*/
protected function get_item($key)
{
if (!self::$memcache) {
return false;
}
$data = self::$memcache->get($key);
if ($this->debug) {
$this->debug('get', $key, $data);
}
return $data;
}
/**
* Adds entry into the cache.
*
* @param string $key Cache internal key name
* @param mixed $data Serialized cache data
*
* @param bool True on success, False on failure
*/
protected function add_item($key, $data)
{
if (!self::$memcache) {
return false;
}
$result = self::$memcache->set($key, $data, $this->ttl);
if ($this->debug) {
$this->debug('set', $key, $data, $result);
}
return $result;
}
/**
* Deletes entry from the cache
*
* @param string $key Cache internal key name
*
* @param bool True on success, False on failure
*/
protected function delete_item($key)
{
if (!self::$memcache) {
return false;
}
// #1488592: use 2nd argument
$result = self::$memcache->delete($key, 0);
if ($this->debug) {
$this->debug('delete', $key, null, $result);
}
return $result;
}
}