3 // security - hide paths
4 if (!defined('ADODB_DIR')) die();
6 global $ADODB_INCLUDED_MEMCACHE;
7 $ADODB_INCLUDED_MEMCACHE = 1;
9 global $ADODB_INCLUDED_CSV;
10 if (empty($ADODB_INCLUDED_CSV)) include_once(ADODB_DIR
.'/adodb-csvlib.inc.php');
14 @version v5.20.9 21-Dec-2016
15 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
16 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
17 Released under both BSD license and Lesser GPL library license.
18 Whenever there is any discrepancy between the two licenses,
19 the BSD license will take precedence. See License.txt.
20 Set tabs to 4 for best viewing.
22 Latest version is available at http://adodb.sourceforge.net
26 $db = NewADOConnection($driver);
27 $db->memCache = true; /// should we use memCache instead of caching in files
28 $db->memCacheHost = array($ip1, $ip2, $ip3);
29 $db->memCachePort = 11211; /// this is default memCache port
30 $db->memCacheCompress = false; /// Use 'true' to store the item compressed (uses zlib)
33 $db->CacheExecute($sql);
35 Note the memcache class is shared by all connections, is created during the first call to Connect/PConnect.
37 Class instance is stored in $ADODB_CACHE
40 class ADODB_Cache_MemCache
{
41 var $createdir = false; // create caching directory structure?
43 //-----------------------------
44 // memcache specific variables
46 var $hosts; // array of hosts
48 var $compress = false; // memcache compression with zlib
50 var $_connected = false;
51 var $_memcache = false;
53 function __construct(&$obj)
55 $this->hosts
= $obj->memCacheHost
;
56 $this->port
= $obj->memCachePort
;
57 $this->compress
= $obj->memCacheCompress
;
60 // implement as lazy connection. The connection only occurs on CacheExecute call
61 function connect(&$err)
63 if (!function_exists('memcache_pconnect')) {
64 $err = 'Memcache module PECL extension not found!';
68 $memcache = new MemCache
;
70 if (!is_array($this->hosts
)) $this->hosts
= array($this->hosts
);
73 foreach($this->hosts
as $host) {
74 if (!@$memcache->addServer($host,$this->port
,true)) {
78 if ($failcnt == sizeof($this->hosts
)) {
79 $err = 'Can\'t connect to any memcache server';
82 $this->_connected
= true;
83 $this->_memcache
= $memcache;
87 // returns true or false. true if successful save
88 function writecache($filename, $contents, $debug, $secs2cache)
90 if (!$this->_connected
) {
92 if (!$this->connect($err) && $debug) ADOConnection
::outp($err);
94 if (!$this->_memcache
) return false;
96 if (!$this->_memcache
->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED
: 0, $secs2cache)) {
97 if ($debug) ADOConnection
::outp(" Failed to save data at the memcached server!<br>\n");
104 // returns a recordset
105 function readcache($filename, &$err, $secs2cache, $rsClass)
108 if (!$this->_connected
) $this->connect($err);
109 if (!$this->_memcache
) return $false;
111 $rs = $this->_memcache
->get($filename);
113 $err = 'Item with such key doesn\'t exists on the memcached server.';
117 // hack, should actually use _csv2rs
118 $rs = explode("\n", $rs);
120 $rs = join("\n", $rs);
121 $rs = unserialize($rs);
122 if (! is_object($rs)) {
123 $err = 'Unable to unserialize $rs';
126 if ($rs->timeCreated
== 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere
128 $tdiff = intval($rs->timeCreated+
$secs2cache - time());
132 if ((rand() & 15) == 0) {
138 if ((rand() & 3) == 0) {
151 function flushall($debug=false)
153 if (!$this->_connected
) {
155 if (!$this->connect($err) && $debug) ADOConnection
::outp($err);
157 if (!$this->_memcache
) return false;
159 $del = $this->_memcache
->flush();
162 if (!$del) ADOConnection
::outp("flushall: failed!<br>\n");
163 else ADOConnection
::outp("flushall: succeeded!<br>\n");
168 function flushcache($filename, $debug=false)
170 if (!$this->_connected
) {
172 if (!$this->connect($err) && $debug) ADOConnection
::outp($err);
174 if (!$this->_memcache
) return false;
176 $del = $this->_memcache
->delete($filename);
179 if (!$del) ADOConnection
::outp("flushcache: $key entry doesn't exist on memcached server!<br>\n");
180 else ADOConnection
::outp("flushcache: $key entry flushed from memcached server!<br>\n");
185 // not used for memcache
186 function createdir($dir, $hash)