- Timestamp:
- 09/26/13 15:41:49 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/adodb-memcache.lib.inc.php
r34 r8222 7 7 $ADODB_INCLUDED_MEMCACHE = 1; 8 8 9 global $ADODB_INCLUDED_CSV; 10 if (empty($ADODB_INCLUDED_CSV)) include(ADODB_DIR.'/adodb-csvlib.inc.php'); 11 9 12 /* 10 13 11 V 4.90 8 June 2006 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.14 V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved. 12 15 Released under both BSD license and Lesser GPL library license. 13 16 Whenever there is any discrepancy between the two licenses, … … 16 19 17 20 Latest version is available at http://adodb.sourceforge.net 21 22 Usage: 18 23 24 $db = NewADOConnection($driver); 25 $db->memCache = true; /// should we use memCache instead of caching in files 26 $db->memCacheHost = array($ip1, $ip2, $ip3); 27 $db->memCachePort = 11211; /// this is default memCache port 28 $db->memCacheCompress = false; /// Use 'true' to store the item compressed (uses zlib) 29 30 $db->Connect(...); 31 $db->CacheExecute($sql); 32 33 Note the memcache class is shared by all connections, is created during the first call to Connect/PConnect. 34 35 Class instance is stored in $ADODB_CACHE 19 36 */ 20 37 21 function &getmemcache($key,&$err, $timeout=0, $host, $port) 22 { 23 $false = false; 24 $err = false; 38 class ADODB_Cache_MemCache { 39 var $createdir = false; // create caching directory structure? 40 41 //----------------------------- 42 // memcache specific variables 43 44 var $hosts; // array of hosts 45 var $port = 11211; 46 var $compress = false; // memcache compression with zlib 47 48 var $_connected = false; 49 var $_memcache = false; 50 51 function ADODB_Cache_MemCache(&$obj) 52 { 53 $this->hosts = $obj->memCacheHost; 54 $this->port = $obj->memCachePort; 55 $this->compress = $obj->memCacheCompress; 56 } 57 58 // implement as lazy connection. The connection only occurs on CacheExecute call 59 function connect(&$err) 60 { 61 if (!function_exists('memcache_pconnect')) { 62 $err = 'Memcache module PECL extension not found!'; 63 return false; 64 } 25 65 26 if (!function_exists('memcache_pconnect')) { 27 $err = 'Memcache module PECL extension not found!'; 28 return $false; 66 $memcache = new MemCache; 67 68 if (!is_array($this->hosts)) $this->hosts = array($this->hosts); 69 70 $failcnt = 0; 71 foreach($this->hosts as $host) { 72 if (!@$memcache->addServer($host,$this->port,true)) { 73 $failcnt += 1; 74 } 75 } 76 if ($failcnt == sizeof($this->hosts)) { 77 $err = 'Can\'t connect to any memcache server'; 78 return false; 79 } 80 $this->_connected = true; 81 $this->_memcache = $memcache; 82 return true; 29 83 } 30 31 $memcache = new Memcache; 32 if (!@$memcache->pconnect($host, $port)) { 33 $err = 'Can\'t connect to memcache server on: '.$host.':'.$port; 34 return $false; 84 85 // returns true or false. true if successful save 86 function writecache($filename, $contents, $debug, $secs2cache) 87 { 88 if (!$this->_connected) { 89 $err = ''; 90 if (!$this->connect($err) && $debug) ADOConnection::outp($err); 91 } 92 if (!$this->_memcache) return false; 93 94 if (!$this->_memcache->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED : 0, $secs2cache)) { 95 if ($debug) ADOConnection::outp(" Failed to save data at the memcached server!<br>\n"); 96 return false; 97 } 98 99 return true; 35 100 } 36 37 $rs = $memcache->get($key); 38 if (!$rs) { 39 $err = 'Item with such key doesn\'t exists on the memcached server.'; 40 return $false; 101 102 // returns a recordset 103 function readcache($filename, &$err, $secs2cache, $rsClass) 104 { 105 $false = false; 106 if (!$this->_connected) $this->connect($err); 107 if (!$this->_memcache) return $false; 108 109 $rs = $this->_memcache->get($filename); 110 if (!$rs) { 111 $err = 'Item with such key doesn\'t exists on the memcached server.'; 112 return $false; 113 } 114 115 // hack, should actually use _csv2rs 116 $rs = explode("\n", $rs); 117 unset($rs[0]); 118 $rs = join("\n", $rs); 119 $rs = unserialize($rs); 120 if (! is_object($rs)) { 121 $err = 'Unable to unserialize $rs'; 122 return $false; 123 } 124 if ($rs->timeCreated == 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere 125 126 $tdiff = intval($rs->timeCreated+$secs2cache - time()); 127 if ($tdiff <= 2) { 128 switch($tdiff) { 129 case 2: 130 if ((rand() & 15) == 0) { 131 $err = "Timeout 2"; 132 return $false; 133 } 134 break; 135 case 1: 136 if ((rand() & 3) == 0) { 137 $err = "Timeout 1"; 138 return $false; 139 } 140 break; 141 default: 142 $err = "Timeout 0"; 143 return $false; 144 } 145 } 146 return $rs; 41 147 } 42 43 $tdiff = intval($rs->timeCreated+$timeout - time()); 44 if ($tdiff <= 2) { 45 switch($tdiff) { 46 case 2: 47 if ((rand() & 15) == 0) { 48 $err = "Timeout 2"; 49 return $false; 50 } 51 break; 52 case 1: 53 if ((rand() & 3) == 0) { 54 $err = "Timeout 1"; 55 return $false; 56 } 57 break; 58 default: 59 $err = "Timeout 0"; 60 return $false; 148 149 function flushall($debug=false) 150 { 151 if (!$this->_connected) { 152 $err = ''; 153 if (!$this->connect($err) && $debug) ADOConnection::outp($err); 61 154 } 155 if (!$this->_memcache) return false; 156 157 $del = $this->_memcache->flush(); 158 159 if ($debug) 160 if (!$del) ADOConnection::outp("flushall: failed!<br>\n"); 161 else ADOConnection::outp("flushall: succeeded!<br>\n"); 162 163 return $del; 62 164 } 63 return $rs; 165 166 function flushcache($filename, $debug=false) 167 { 168 if (!$this->_connected) { 169 $err = ''; 170 if (!$this->connect($err) && $debug) ADOConnection::outp($err); 171 } 172 if (!$this->_memcache) return false; 173 174 $del = $this->_memcache->delete($filename); 175 176 if ($debug) 177 if (!$del) ADOConnection::outp("flushcache: $key entry doesn't exist on memcached server!<br>\n"); 178 else ADOConnection::outp("flushcache: $key entry flushed from memcached server!<br>\n"); 179 180 return $del; 181 } 182 183 // not used for memcache 184 function createdir($dir, $hash) 185 { 186 return true; 187 } 64 188 } 65 189 66 function putmemcache($key, $rs, $host, $port, $compress, $debug=false)67 {68 $false = false;69 $true = true;70 71 if (!function_exists('memcache_pconnect')) {72 if ($debug) ADOConnection::outp(" Memcache module PECL extension not found!<br>\n");73 return $false;74 }75 76 $memcache = new Memcache;77 if (!@$memcache->pconnect($host, $port)) {78 if ($debug) ADOConnection::outp(" Can't connect to memcache server on: $host:$port<br>\n");79 return $false;80 }81 82 $rs->timeCreated = time();83 if (!$memcache->set($key, $rs, $compress, 0)) {84 if ($debug) ADOConnection::outp(" Failed to save data at the memcached server!<br>\n");85 return $false;86 }87 return $true;88 }89 90 function flushmemcache($key=false, $host, $port, $debug=false)91 {92 if (!function_exists('memcache_pconnect')) {93 if ($debug) ADOConnection::outp(" Memcache module PECL extension not found!<br>\n");94 return;95 }96 97 $memcache = new Memcache;98 if (!@$memcache->pconnect($host, $port)) {99 if ($debug) ADOConnection::outp(" Can't connect to memcache server on: $host:$port<br>\n");100 return;101 }102 103 if ($key) {104 if (!$memcache->delete($key)) {105 if ($debug) ADOConnection::outp("CacheFlush: $key entery doesn't exist on memcached server!<br>\n");106 } else {107 if ($debug) ADOConnection::outp("CacheFlush: $key entery flushed from memcached server!<br>\n");108 }109 } else {110 if (!$memcache->flush()) {111 if ($debug) ADOConnection::outp("CacheFlush: Failure flushing all enteries from memcached server!<br>\n");112 } else {113 if ($debug) ADOConnection::outp("CacheFlush: All enteries flushed from memcached server!<br>\n");114 }115 }116 return;117 }118 190 ?>
Note: See TracChangeset
for help on using the changeset viewer.