| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // security - hide paths |
|---|
| 4 | if (!defined('ADODB_DIR')) die(); |
|---|
| 5 | |
|---|
| 6 | global $ADODB_INCLUDED_MEMCACHE; |
|---|
| 7 | $ADODB_INCLUDED_MEMCACHE = 1; |
|---|
| 8 | |
|---|
| 9 | global $ADODB_INCLUDED_CSV; |
|---|
| 10 | if (empty($ADODB_INCLUDED_CSV)) include(ADODB_DIR.'/adodb-csvlib.inc.php'); |
|---|
| 11 | |
|---|
| 12 | /* |
|---|
| 13 | |
|---|
| 14 | v4.992 10 Nov 2009 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved. |
|---|
| 15 | Released under both BSD license and Lesser GPL library license. |
|---|
| 16 | Whenever there is any discrepancy between the two licenses, |
|---|
| 17 | the BSD license will take precedence. See License.txt. |
|---|
| 18 | Set tabs to 4 for best viewing. |
|---|
| 19 | |
|---|
| 20 | Latest version is available at http://adodb.sourceforge.net |
|---|
| 21 | |
|---|
| 22 | Usage: |
|---|
| 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 |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 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 | } |
|---|
| 65 | |
|---|
| 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 | |
|---|
| 83 | return true; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // returns true or false. true if successful save |
|---|
| 87 | function writecache($filename, $contents,$debug, $secs2cache) |
|---|
| 88 | { |
|---|
| 89 | if (!$this->_connected) { |
|---|
| 90 | $err = ''; |
|---|
| 91 | if (!$this->connect($err) && $debug) ADOConnection::outp($err); |
|---|
| 92 | } |
|---|
| 93 | if (!$this->_memcache) return false; |
|---|
| 94 | |
|---|
| 95 | if (!$this->_memcache->set($filename, $contents, $this->compress, $secs2cache)) { |
|---|
| 96 | if ($debug) ADOConnection::outp(" Failed to save data at the memcached server!<br>\n"); |
|---|
| 97 | return false; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | return true; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // returns a recordset |
|---|
| 104 | function &readcache($filename, &$err, $secs2cache, $rsClass) |
|---|
| 105 | { |
|---|
| 106 | $false = false; |
|---|
| 107 | if (!$this->_connected) $this->connect($err); |
|---|
| 108 | if (!$this->_memcache) return $false; |
|---|
| 109 | |
|---|
| 110 | $rs = $this->_memcache->get($filename); |
|---|
| 111 | if (!$rs) { |
|---|
| 112 | $err = 'Item with such key doesn\'t exists on the memcached server.'; |
|---|
| 113 | return $false; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | // hack, should actually use _csv2rs |
|---|
| 117 | $rs = explode("\n", $rs); |
|---|
| 118 | unset($rs[0]); |
|---|
| 119 | $rs = join("\n", $rs); |
|---|
| 120 | $rs = unserialize($rs); |
|---|
| 121 | if (! is_object($rs)) { |
|---|
| 122 | $err = 'Unable to unserialize $rs'; |
|---|
| 123 | return $false; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if ($rs->timeCreated == 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere |
|---|
| 127 | $tdiff = intval($rs->timeCreated+$secs2cache - time()); |
|---|
| 128 | if ($tdiff <= 2) { |
|---|
| 129 | switch($tdiff) { |
|---|
| 130 | case 2: |
|---|
| 131 | if ((rand() & 15) == 0) { |
|---|
| 132 | $err = "Timeout 2"; |
|---|
| 133 | return $false; |
|---|
| 134 | } |
|---|
| 135 | break; |
|---|
| 136 | case 1: |
|---|
| 137 | if ((rand() & 3) == 0) { |
|---|
| 138 | $err = "Timeout 1"; |
|---|
| 139 | return $false; |
|---|
| 140 | } |
|---|
| 141 | break; |
|---|
| 142 | default: |
|---|
| 143 | $err = "Timeout 0"; |
|---|
| 144 | return $false; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | return $rs; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | function flushall($debug=false) |
|---|
| 151 | { |
|---|
| 152 | if (!$this->_connected) { |
|---|
| 153 | $err = ''; |
|---|
| 154 | if (!$this->connect($err) && $debug) ADOConnection::outp($err); |
|---|
| 155 | } |
|---|
| 156 | if (!$this->_memcache) return false; |
|---|
| 157 | |
|---|
| 158 | $del = $this->_memcache->flush(); |
|---|
| 159 | |
|---|
| 160 | if ($debug) |
|---|
| 161 | if (!$del) ADOConnection::outp("flushall: failed!<br>\n"); |
|---|
| 162 | else ADOConnection::outp("flushall: succeeded!<br>\n"); |
|---|
| 163 | |
|---|
| 164 | return $del; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | function flushcache($filename, $debug=false) |
|---|
| 168 | { |
|---|
| 169 | if (!$this->_connected) { |
|---|
| 170 | $err = ''; |
|---|
| 171 | if (!$this->connect($err) && $debug) ADOConnection::outp($err); |
|---|
| 172 | } |
|---|
| 173 | if (!$this->_memcache) return false; |
|---|
| 174 | |
|---|
| 175 | $del = $this->_memcache->delete($filename); |
|---|
| 176 | |
|---|
| 177 | if ($debug) |
|---|
| 178 | if (!$del) ADOConnection::outp("flushcache: $key entry doesn't exist on memcached server!<br>\n"); |
|---|
| 179 | else ADOConnection::outp("flushcache: $key entry flushed from memcached server!<br>\n"); |
|---|
| 180 | |
|---|
| 181 | return $del; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | // not used for memcache |
|---|
| 185 | function createdir($dir, $hash) |
|---|
| 186 | { |
|---|
| 187 | return true; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | ?> |
|---|