5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
8 * @link https://adodb.org Project's web site and documentation
9 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
11 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
12 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
13 * any later version. This means you can use it in proprietary products.
14 * See the LICENSE.md file distributed with this source code for details.
15 * @license BSD-3-Clause
16 * @license LGPL-2.1-or-later
18 * @copyright 2000-2013 John Lim
19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
22 // security - hide paths
23 if (!defined('ADODB_DIR')) die();
25 global $ADODB_INCLUDED_MEMCACHE;
26 $ADODB_INCLUDED_MEMCACHE = 1;
28 global $ADODB_INCLUDED_CSV;
29 if (empty($ADODB_INCLUDED_CSV)) include_once(ADODB_DIR
.'/adodb-csvlib.inc.php');
31 class ADODB_Cache_MemCache
{
32 var $createdir = false; // create caching directory structure?
34 // $library will be populated with the proper library on connect
35 // and is used later when there are differences in specific calls
36 // between memcache and memcached
39 //-----------------------------
40 // memcache specific variables
42 var $hosts; // array of hosts
44 var $compress = false; // memcache compression with zlib
46 var $_connected = false;
47 var $_memcache = false;
49 function __construct(&$obj)
51 $this->hosts
= $obj->memCacheHost
;
52 $this->port
= $obj->memCachePort
;
53 $this->compress
= $obj->memCacheCompress
;
56 // implement as lazy connection. The connection only occurs on CacheExecute call
57 function connect(&$err)
59 // do we have memcache or memcached?
60 if (class_exists('Memcache')) {
61 $this->library
='Memcache';
62 $memcache = new MemCache
;
63 } elseif (class_exists('Memcached')) {
64 $this->library
='Memcached';
65 $memcache = new MemCached
;
67 $err = 'Neither the Memcache nor Memcached PECL extensions were found!';
71 if (!is_array($this->hosts
)) $this->hosts
= array($this->hosts
);
74 foreach($this->hosts
as $host) {
75 if (!@$memcache->addServer($host,$this->port
)) {
79 if ($failcnt == sizeof($this->hosts
)) {
80 $err = 'Can\'t connect to any memcache server';
83 $this->_connected
= true;
84 $this->_memcache
= $memcache;
88 // returns true or false. true if successful save
89 function writecache($filename, $contents, $debug, $secs2cache)
91 if (!$this->_connected
) {
93 if (!$this->connect($err) && $debug) ADOConnection
::outp($err);
95 if (!$this->_memcache
) return false;
98 switch ($this->library
) {
100 if (!$this->_memcache
->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED
: 0, $secs2cache)) {
105 if (!$this->_memcache
->set($filename, $contents, $secs2cache)) {
115 if ($debug) ADOConnection
::outp(" Failed to save data at the memcache server!<br>\n");
122 // returns a recordset
123 function readcache($filename, &$err, $secs2cache, $rsClass)
126 if (!$this->_connected
) $this->connect($err);
127 if (!$this->_memcache
) return $false;
129 $rs = $this->_memcache
->get($filename);
131 $err = 'Item with such key doesn\'t exist on the memcache server.';
135 // hack, should actually use _csv2rs
136 $rs = explode("\n", $rs);
138 $rs = join("\n", $rs);
139 $rs = unserialize($rs);
140 if (! is_object($rs)) {
141 $err = 'Unable to unserialize $rs';
144 if ($rs->timeCreated
== 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere
146 $tdiff = intval($rs->timeCreated+
$secs2cache - time());
150 if ((rand() & 15) == 0) {
156 if ((rand() & 3) == 0) {
169 function flushall($debug=false)
171 if (!$this->_connected
) {
173 if (!$this->connect($err) && $debug) ADOConnection
::outp($err);
175 if (!$this->_memcache
) return false;
177 $del = $this->_memcache
->flush();
180 if (!$del) ADOConnection
::outp("flushall: failed!<br>\n");
181 else ADOConnection
::outp("flushall: succeeded!<br>\n");
186 function flushcache($filename, $debug=false)
188 if (!$this->_connected
) {
190 if (!$this->connect($err) && $debug) ADOConnection
::outp($err);
192 if (!$this->_memcache
) return false;
194 $del = $this->_memcache
->delete($filename);
197 if (!$del) ADOConnection
::outp("flushcache: $key entry doesn't exist on memcache server!<br>\n");
198 else ADOConnection
::outp("flushcache: $key entry flushed from memcache server!<br>\n");
203 // not used for memcache
204 function createdir($dir, $hash)