Automatically generated installer lang files
[moodle.git] / lib / adodb / adodb-memcache.lib.inc.php
blob7f110e7493c600c6bd0325d53b872a64926b8776
1 <?php
2 /**
3 * Memory caching.
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
7 * @package ADOdb
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
37 var $library = false;
39 //-----------------------------
40 // memcache specific variables
42 var $hosts; // array of hosts
43 var $port = 11211;
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;
66 } else {
67 $err = 'Neither the Memcache nor Memcached PECL extensions were found!';
68 return false;
71 if (!is_array($this->hosts)) $this->hosts = array($this->hosts);
73 $failcnt = 0;
74 foreach($this->hosts as $host) {
75 if (!@$memcache->addServer($host,$this->port)) {
76 $failcnt += 1;
79 if ($failcnt == sizeof($this->hosts)) {
80 $err = 'Can\'t connect to any memcache server';
81 return false;
83 $this->_connected = true;
84 $this->_memcache = $memcache;
85 return true;
88 // returns true or false. true if successful save
89 function writecache($filename, $contents, $debug, $secs2cache)
91 if (!$this->_connected) {
92 $err = '';
93 if (!$this->connect($err) && $debug) ADOConnection::outp($err);
95 if (!$this->_memcache) return false;
97 $failed=false;
98 switch ($this->library) {
99 case 'Memcache':
100 if (!$this->_memcache->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED : 0, $secs2cache)) {
101 $failed=true;
103 break;
104 case 'Memcached':
105 if (!$this->_memcache->set($filename, $contents, $secs2cache)) {
106 $failed=true;
108 break;
109 default:
110 $failed=true;
111 break;
114 if($failed) {
115 if ($debug) ADOConnection::outp(" Failed to save data at the memcache server!<br>\n");
116 return false;
119 return true;
122 // returns a recordset
123 function readcache($filename, &$err, $secs2cache, $rsClass)
125 $false = false;
126 if (!$this->_connected) $this->connect($err);
127 if (!$this->_memcache) return $false;
129 $rs = $this->_memcache->get($filename);
130 if (!$rs) {
131 $err = 'Item with such key doesn\'t exist on the memcache server.';
132 return $false;
135 // hack, should actually use _csv2rs
136 $rs = explode("\n", $rs);
137 unset($rs[0]);
138 $rs = join("\n", $rs);
139 $rs = unserialize($rs);
140 if (! is_object($rs)) {
141 $err = 'Unable to unserialize $rs';
142 return $false;
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());
147 if ($tdiff <= 2) {
148 switch($tdiff) {
149 case 2:
150 if ((rand() & 15) == 0) {
151 $err = "Timeout 2";
152 return $false;
154 break;
155 case 1:
156 if ((rand() & 3) == 0) {
157 $err = "Timeout 1";
158 return $false;
160 break;
161 default:
162 $err = "Timeout 0";
163 return $false;
166 return $rs;
169 function flushall($debug=false)
171 if (!$this->_connected) {
172 $err = '';
173 if (!$this->connect($err) && $debug) ADOConnection::outp($err);
175 if (!$this->_memcache) return false;
177 $del = $this->_memcache->flush();
179 if ($debug)
180 if (!$del) ADOConnection::outp("flushall: failed!<br>\n");
181 else ADOConnection::outp("flushall: succeeded!<br>\n");
183 return $del;
186 function flushcache($filename, $debug=false)
188 if (!$this->_connected) {
189 $err = '';
190 if (!$this->connect($err) && $debug) ADOConnection::outp($err);
192 if (!$this->_memcache) return false;
194 $del = $this->_memcache->delete($filename);
196 if ($debug)
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");
200 return $del;
203 // not used for memcache
204 function createdir($dir, $hash)
206 return true;