adodb-memcache.lib.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // security - hide paths
  3. if (!defined('ADODB_DIR')) die();
  4. global $ADODB_INCLUDED_MEMCACHE;
  5. $ADODB_INCLUDED_MEMCACHE = 1;
  6. global $ADODB_INCLUDED_CSV;
  7. if (empty($ADODB_INCLUDED_CSV)) include_once(ADODB_DIR.'/adodb-csvlib.inc.php');
  8. /*
  9. @version v5.20.17 31-Mar-2020
  10. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  11. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  12. Released under both BSD license and Lesser GPL library license.
  13. Whenever there is any discrepancy between the two licenses,
  14. the BSD license will take precedence. See License.txt.
  15. Set tabs to 4 for best viewing.
  16. Latest version is available at http://adodb.org/
  17. Usage:
  18. $db = NewADOConnection($driver);
  19. $db->memCache = true; /// should we use memCache instead of caching in files
  20. $db->memCacheHost = array($ip1, $ip2, $ip3);
  21. $db->memCachePort = 11211; /// this is default memCache port
  22. $db->memCacheCompress = false; /// Use 'true' to store the item compressed (uses zlib)
  23. $db->Connect(...);
  24. $db->CacheExecute($sql);
  25. Note the memcache class is shared by all connections, is created during the first call to Connect/PConnect.
  26. Class instance is stored in $ADODB_CACHE
  27. */
  28. class ADODB_Cache_MemCache {
  29. var $createdir = false; // create caching directory structure?
  30. //-----------------------------
  31. // memcache specific variables
  32. var $hosts; // array of hosts
  33. var $port = 11211;
  34. var $compress = false; // memcache compression with zlib
  35. var $_connected = false;
  36. var $_memcache = false;
  37. function __construct(&$obj)
  38. {
  39. $this->hosts = $obj->memCacheHost;
  40. $this->port = $obj->memCachePort;
  41. $this->compress = $obj->memCacheCompress;
  42. }
  43. // implement as lazy connection. The connection only occurs on CacheExecute call
  44. function connect(&$err)
  45. {
  46. if (!function_exists('memcache_pconnect')) {
  47. $err = 'Memcache module PECL extension not found!';
  48. return false;
  49. }
  50. $memcache = new MemCache;
  51. if (!is_array($this->hosts)) $this->hosts = array($this->hosts);
  52. $failcnt = 0;
  53. foreach($this->hosts as $host) {
  54. if (!@$memcache->addServer($host,$this->port,true)) {
  55. $failcnt += 1;
  56. }
  57. }
  58. if ($failcnt == sizeof($this->hosts)) {
  59. $err = 'Can\'t connect to any memcache server';
  60. return false;
  61. }
  62. $this->_connected = true;
  63. $this->_memcache = $memcache;
  64. return true;
  65. }
  66. // returns true or false. true if successful save
  67. function writecache($filename, $contents, $debug, $secs2cache)
  68. {
  69. if (!$this->_connected) {
  70. $err = '';
  71. if (!$this->connect($err) && $debug) ADOConnection::outp($err);
  72. }
  73. if (!$this->_memcache) return false;
  74. if (!$this->_memcache->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED : 0, $secs2cache)) {
  75. if ($debug) ADOConnection::outp(" Failed to save data at the memcached server!<br>\n");
  76. return false;
  77. }
  78. return true;
  79. }
  80. // returns a recordset
  81. function readcache($filename, &$err, $secs2cache, $rsClass)
  82. {
  83. $false = false;
  84. if (!$this->_connected) $this->connect($err);
  85. if (!$this->_memcache) return $false;
  86. $rs = $this->_memcache->get($filename);
  87. if (!$rs) {
  88. $err = 'Item with such key doesn\'t exists on the memcached server.';
  89. return $false;
  90. }
  91. // hack, should actually use _csv2rs
  92. $rs = explode("\n", $rs);
  93. unset($rs[0]);
  94. $rs = join("\n", $rs);
  95. $rs = unserialize($rs);
  96. if (! is_object($rs)) {
  97. $err = 'Unable to unserialize $rs';
  98. return $false;
  99. }
  100. if ($rs->timeCreated == 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere
  101. $tdiff = intval($rs->timeCreated+$secs2cache - time());
  102. if ($tdiff <= 2) {
  103. switch($tdiff) {
  104. case 2:
  105. if ((rand() & 15) == 0) {
  106. $err = "Timeout 2";
  107. return $false;
  108. }
  109. break;
  110. case 1:
  111. if ((rand() & 3) == 0) {
  112. $err = "Timeout 1";
  113. return $false;
  114. }
  115. break;
  116. default:
  117. $err = "Timeout 0";
  118. return $false;
  119. }
  120. }
  121. return $rs;
  122. }
  123. function flushall($debug=false)
  124. {
  125. if (!$this->_connected) {
  126. $err = '';
  127. if (!$this->connect($err) && $debug) ADOConnection::outp($err);
  128. }
  129. if (!$this->_memcache) return false;
  130. $del = $this->_memcache->flush();
  131. if ($debug)
  132. if (!$del) ADOConnection::outp("flushall: failed!<br>\n");
  133. else ADOConnection::outp("flushall: succeeded!<br>\n");
  134. return $del;
  135. }
  136. function flushcache($filename, $debug=false)
  137. {
  138. if (!$this->_connected) {
  139. $err = '';
  140. if (!$this->connect($err) && $debug) ADOConnection::outp($err);
  141. }
  142. if (!$this->_memcache) return false;
  143. $del = $this->_memcache->delete($filename);
  144. if ($debug)
  145. if (!$del) ADOConnection::outp("flushcache: $key entry doesn't exist on memcached server!<br>\n");
  146. else ADOConnection::outp("flushcache: $key entry flushed from memcached server!<br>\n");
  147. return $del;
  148. }
  149. // not used for memcache
  150. function createdir($dir, $hash)
  151. {
  152. return true;
  153. }
  154. }