forked from ADOdb/ADOdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadodb-memcache.lib.inc.php
431 lines (377 loc) · 9.07 KB
/
adodb-memcache.lib.inc.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
// security - hide paths
if (!defined('ADODB_DIR')) die();
global $ADODB_INCLUDED_MEMCACHE;
$ADODB_INCLUDED_MEMCACHE = 1;
/*
@version v5.22.0-dev Unreleased
@copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
@copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. See License.txt.
Set tabs to 4 for best viewing.
Latest version is available at https://adodb.org/
Class instance is stored in $ADODB_CACHE
*/
class ADODB_Cache_MemCache
{
/*
* Prevents parent class calling non-existant function
*/
public $createdir = false;
/*
* populated with the proper library on connect
* and is used later when there are differences in specific calls
* between memcache and memcached
*/
private $memCacheLibrary = false;
/*
* array of hosts
*/
private $hosts;
/*
* Connection Port, uses default
*/
private $port = 11211;
/*
* memcache compression with zlib
*/
private $compress = false;
/*
* Array of options for memcached only
*/
private $options = false;
/*
* Internal flag indicating successful connection
*/
private $isConnected = false;
/*
* Handle for the Memcache library
*/
private $memcacheLibrary = false
/*
* New server feature controller lists available servers
*/
private $serverControllers = array();
/*
* New server feature controller uses granular
* server controller
*/
private $serverControllerTemplate = array(
'host'=>'',
'port'=>11211,
'weight'=>0,
'key'=>''
);
/*
* An integer index into the libraries
*/
const MCLIB = 1;
const MCLIBD = 2;
/*
* Xrefs the library flag to the actual class name
*/
private $libraries = array(
1=>'Memcache',
2=>'Memcached'
);
/*
* An indicator of which library we are using
*/
private $libraryFlag;
/**
* constructor passes in a ADONewConnection Object
*
* @param $db ADONewConnection object
*
* @return obj
*/
public function __construct(&$db)
{
$this->hosts = $db->memCacheHost;
$this->port = $db->memCachePort;
$this->compress = $db->memCacheCompress;
$this->options = $db->memCacheOptions;
}
/**
* implement as lazy connection. The connection only occurs on CacheExecute call
*
* @param string $err
*
* @return bool success of connecting to a server
*/
public function connect(&$err)
{
/*
* do we have memcache or memcached? see the note
* at adodb.org on memcache
*/
if (class_exists('Memcache'))
$this->libraryFlag = self::MCLIB;
elseif (class_exists('Memcached'))
$this->libraryFlag = self::MCLIB;
else
{
$err = 'Neither the Memcache nor Memcached PECL extensions were found!';
return false;
}
$this->memCacheLibrary = new {$this->libraries[$libraryFlag]};
if (!$this->memcacheLibrary)
{
$err = 'Memcache library failed to initialize';
return false;
}
/*
* Convert simple compression flag for memcached
*/
if ($this->libraryFlag == self::MCLIBD && $this->compress)
{
/*
* Value of Memcached::OPT_COMPRESSION = 2;
*/
$this->options[2] == 1
}
/*
* Are there any options available for memcached
*/
if ($this->libraryFlag == self::MCLIBD && count($this->options) > 0)
{
$optionSuccess = $this->memCacheLibrary->setOptions($this->options);
if (!$optionSuccess)
{
$err = 'Invalid option parameters passed to Memecached';
return false;
}
}
/*
* Have we passed a controller array
*/
if (!is_array($this->hosts))
$this->hosts = array($this->hosts);
if (array_values($this->hosts) == $this->hosts)
{
/*
* Old way, convert to controller
*/
foreach ($this->hosts as $ipAddress)
{
$connector = $this->serverControllerTemplate;
$connector['host'] = $ipAddress;
$connector['port'] = $this->port;
$this->serverControllers[] = $connector;
}
}
else
{
/*
* New way, must validate port, etc
*/
foreach ($this->hosts as $controller)
{
$connector = array_merge($this->serverControllerTemplate,$controller);
if ($this->libraryFlag == self::MCLIB)
{
/*
* Cannot use a key or weight in memcache, simply discard
*/
$connector['key'] = '';
$connector['weight'] = 0;
}
else
$connector['weight'] = $connector['weight'] ? (int)$connector['weight']:0;
$this->serverControllers[] = $connector;
}
}
/*
* Checks for existing connections ( but only for memcached )
*/
if ($this->libraryFlag == self::MCLIBD)
{
$existingServers = $memCache->getServerList();
if (is_array($existingServers))
{
/*
* Use the existing configuration
*/
$this->isConnected = true;
$this->memcacheLibrary = $memcache;
return true;
}
}
$failcnt = 0;
foreach($this->serverControllers as $controller)
{
switch($this->libraryFlag)
{
case self::MCLIB:
if (!@$this->memcacheLibrary->addServer($controller['host'],$controller['port']))
$failcnt++;
break;
default:
if (!@$this->memcacheLibrary->addServer($controller['host'],$controller['port'],$controller['weight']))
$failcnt++;
}
}
if ($failcnt == sizeof($this->serverControllers))
{
$err = 'Can\'t connect to any memcache server';
return false;
}
/*
* A valid memcache connection is available
*/
$this->isConnected = true;
return true;
}
/**
* Writes a cached query to the server
*
* @param string $filename The MD5 of the query to cache
* @param string $contents The query results
* @param bool $debug
* @param int $secs2cache
*
* @return bool true or false. true if successful save
*/
public function writeCache($filename, $contents, $debug, $secs2cache)
{
$err = '';
if (!$this->isConnected && $debug)
{
/*
* Call to writecache before connect, try
* to connect
*/
if (!$this->connect($err))
ADOConnection::outp($err);
}
else if (!$this->isConnected)
$this->connect($err);
if (!$this->memcacheLibrary)
return false;
$failed=false;
switch ($this->libraryFlag)
{
case self::MCLIB:
if (!$this->memcacheLibrary->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED : 0, $secs2cache)) {
$failed=true;
}
break;
case self::MCLIBD:
if (!$this->memcacheLibrary->set($filename, $contents, $secs2cache)) {
$failed=true;
}
break;
default:
$failed=true;
break;
}
if($failed) {
if ($debug) ADOConnection::outp(" Failed to save data at the memcache server!<br>\n");
return false;
}
return true;
}
/**
* Reads a cached query to the server
*
* @param string $filename The MD5 of the query to read
* @param string $err The query results
* @param int $secs2cache
* @param obj $rsClass **UNUSED**
* @return the record or false.
*/
public function readCache($filename, &$err, $secs2cache, $rsClass)
{
if (!$this->isConnected) $this->connect($err);
if (!$this->memcacheLibrary)
return false;
$rs = $this->memcacheLibrary->get($filename);
if (!$rs)
{
$err = 'Item with such key doesn\'t exist on the memcache server.';
return false;
}
// hack, should actually use _csv2rs
$rs = explode("\n", $rs);
unset($rs[0]);
$rs = join("\n", $rs);
$rs = unserialize($rs);
if (! is_object($rs)) {
$err = 'Unable to unserialize $rs';
return $false;
}
if ($rs->timeCreated == 0)
return $rs; // apparently have been reports that timeCreated was set to 0 somewhere
$tdiff = intval($rs->timeCreated+$secs2cache - time());
if ($tdiff <= 2)
{
switch($tdiff)
{
case 2:
if ((rand() & 15) == 0) {
$err = "Timeout 2";
return false;
}
break;
case 1:
if ((rand() & 3) == 0) {
$err = "Timeout 1";
return false;
}
break;
default:
$err = "Timeout 0";
return false;
}
}
return $rs;
}
/**
* Flushes all of the stored memcache data
*
* @param bool $debug
*
* @return int The response from the memcache server
*/
public function flushAll($debug=false)
{
if (!$this->isConnected) {
$err = '';
if (!$this->connect($err) && $debug) ADOConnection::outp($err);
}
if (!$this->memcacheLibrary)
return false;
$del = $this->memcacheLibrary->flush();
if ($debug)
if (!$del)
ADOConnection::outp("flushall: failed!<br>\n");
else
ADOConnection::outp("flushall: succeeded!<br>\n");
return $del;
}
/**
* Flushes the contents of a specified query
*
* @param str $filname The MD5 of the query to flush
* @param bool $debug
*
* @return int The response from the memcache server
*/
public function flushCache($filename, $debug=false)
{
if (!$this->isConnected)
{
$err = '';
if (!$this->connect($err) && $debug) ADOConnection::outp($err);
}
if (!$this->memcacheLibrary)
return false;
$del = $this->memcacheLibrary->delete($filename);
if ($debug)
if (!$del) ADOConnection::outp("flushcache: $key entry doesn't exist on memcache server!<br>\n");
else ADOConnection::outp("flushcache: $key entry flushed from memcache server!<br>\n");
return $del;
}
}