|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // security - hide paths 00004 if (!defined('ADODB_DIR')) die(); 00005 00006 global $ADODB_INCLUDED_MEMCACHE; 00007 $ADODB_INCLUDED_MEMCACHE = 1; 00008 00009 global $ADODB_INCLUDED_CSV; 00010 if (empty($ADODB_INCLUDED_CSV)) include(ADODB_DIR.'/adodb-csvlib.inc.php'); 00011 00012 /* 00013 00014 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved. 00015 Released under both BSD license and Lesser GPL library license. 00016 Whenever there is any discrepancy between the two licenses, 00017 the BSD license will take precedence. See License.txt. 00018 Set tabs to 4 for best viewing. 00019 00020 Latest version is available at http://adodb.sourceforge.net 00021 00022 Usage: 00023 00024 $db = NewADOConnection($driver); 00025 $db->memCache = true; /// should we use memCache instead of caching in files 00026 $db->memCacheHost = array($ip1, $ip2, $ip3); 00027 $db->memCachePort = 11211; /// this is default memCache port 00028 $db->memCacheCompress = false; /// Use 'true' to store the item compressed (uses zlib) 00029 00030 $db->Connect(...); 00031 $db->CacheExecute($sql); 00032 00033 Note the memcache class is shared by all connections, is created during the first call to Connect/PConnect. 00034 00035 Class instance is stored in $ADODB_CACHE 00036 */ 00037 00038 class ADODB_Cache_MemCache { 00039 var $createdir = false; // create caching directory structure? 00040 00041 //----------------------------- 00042 // memcache specific variables 00043 00044 var $hosts; // array of hosts 00045 var $port = 11211; 00046 var $compress = false; // memcache compression with zlib 00047 00048 var $_connected = false; 00049 var $_memcache = false; 00050 00051 function ADODB_Cache_MemCache(&$obj) 00052 { 00053 $this->hosts = $obj->memCacheHost; 00054 $this->port = $obj->memCachePort; 00055 $this->compress = $obj->memCacheCompress; 00056 } 00057 00058 // implement as lazy connection. The connection only occurs on CacheExecute call 00059 function connect(&$err) 00060 { 00061 if (!function_exists('memcache_pconnect')) { 00062 $err = 'Memcache module PECL extension not found!'; 00063 return false; 00064 } 00065 00066 $memcache = new MemCache; 00067 00068 if (!is_array($this->hosts)) $this->hosts = array($this->hosts); 00069 00070 $failcnt = 0; 00071 foreach($this->hosts as $host) { 00072 if (!@$memcache->addServer($host,$this->port,true)) { 00073 $failcnt += 1; 00074 } 00075 } 00076 if ($failcnt == sizeof($this->hosts)) { 00077 $err = 'Can\'t connect to any memcache server'; 00078 return false; 00079 } 00080 $this->_connected = true; 00081 $this->_memcache = $memcache; 00082 return true; 00083 } 00084 00085 // returns true or false. true if successful save 00086 function writecache($filename, $contents, $debug, $secs2cache) 00087 { 00088 if (!$this->_connected) { 00089 $err = ''; 00090 if (!$this->connect($err) && $debug) ADOConnection::outp($err); 00091 } 00092 if (!$this->_memcache) return false; 00093 00094 if (!$this->_memcache->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED : 0, $secs2cache)) { 00095 if ($debug) ADOConnection::outp(" Failed to save data at the memcached server!<br>\n"); 00096 return false; 00097 } 00098 00099 return true; 00100 } 00101 00102 // returns a recordset 00103 function readcache($filename, &$err, $secs2cache, $rsClass) 00104 { 00105 $false = false; 00106 if (!$this->_connected) $this->connect($err); 00107 if (!$this->_memcache) return $false; 00108 00109 $rs = $this->_memcache->get($filename); 00110 if (!$rs) { 00111 $err = 'Item with such key doesn\'t exists on the memcached server.'; 00112 return $false; 00113 } 00114 00115 // hack, should actually use _csv2rs 00116 $rs = explode("\n", $rs); 00117 unset($rs[0]); 00118 $rs = join("\n", $rs); 00119 $rs = unserialize($rs); 00120 if (! is_object($rs)) { 00121 $err = 'Unable to unserialize $rs'; 00122 return $false; 00123 } 00124 if ($rs->timeCreated == 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere 00125 00126 $tdiff = intval($rs->timeCreated+$secs2cache - time()); 00127 if ($tdiff <= 2) { 00128 switch($tdiff) { 00129 case 2: 00130 if ((rand() & 15) == 0) { 00131 $err = "Timeout 2"; 00132 return $false; 00133 } 00134 break; 00135 case 1: 00136 if ((rand() & 3) == 0) { 00137 $err = "Timeout 1"; 00138 return $false; 00139 } 00140 break; 00141 default: 00142 $err = "Timeout 0"; 00143 return $false; 00144 } 00145 } 00146 return $rs; 00147 } 00148 00149 function flushall($debug=false) 00150 { 00151 if (!$this->_connected) { 00152 $err = ''; 00153 if (!$this->connect($err) && $debug) ADOConnection::outp($err); 00154 } 00155 if (!$this->_memcache) return false; 00156 00157 $del = $this->_memcache->flush(); 00158 00159 if ($debug) 00160 if (!$del) ADOConnection::outp("flushall: failed!<br>\n"); 00161 else ADOConnection::outp("flushall: succeeded!<br>\n"); 00162 00163 return $del; 00164 } 00165 00166 function flushcache($filename, $debug=false) 00167 { 00168 if (!$this->_connected) { 00169 $err = ''; 00170 if (!$this->connect($err) && $debug) ADOConnection::outp($err); 00171 } 00172 if (!$this->_memcache) return false; 00173 00174 $del = $this->_memcache->delete($filename); 00175 00176 if ($debug) 00177 if (!$del) ADOConnection::outp("flushcache: $key entry doesn't exist on memcached server!<br>\n"); 00178 else ADOConnection::outp("flushcache: $key entry flushed from memcached server!<br>\n"); 00179 00180 return $del; 00181 } 00182 00183 // not used for memcache 00184 function createdir($dir, $hash) 00185 { 00186 return true; 00187 } 00188 } 00189 00190 ?>