Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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_once (ADODB_DIR.'/adodb-csvlib.inc.php');
  11  
  12  /*
  13  
  14    @version   v5.21.0  2021-02-27
  15    @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  16    @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
  17    Released under both BSD license and Lesser GPL library license.
  18    Whenever there is any discrepancy between the two licenses,
  19    the BSD license will take precedence. See License.txt.
  20    Set tabs to 4 for best viewing.
  21  
  22    Latest version is available at https://adodb.org/
  23  
  24  Usage:
  25  
  26  $db = NewADOConnection($driver);
  27  $db->memCache = true; /// should we use memCache instead of caching in files
  28  $db->memCacheHost = array($ip1, $ip2, $ip3);
  29  $db->memCachePort = 11211; /// this is default memCache port
  30  $db->memCacheCompress = false; /// Use 'true' to store the item compressed (uses zlib)
  31                                 /// Note; compression is not supported w/the memcached library
  32  
  33  $db->Connect(...);
  34  $db->CacheExecute($sql);
  35  
  36    Notes; The memcache class is shared by all connections, is created during the first call to Connect/PConnect.
  37           We'll look for both the memcache library (https://pecl.php.net/package/memcache) and the  memcached
  38           library (https://pecl.php.net/package/memcached). If both exist, the memcache library will be used.
  39  
  40    Class instance is stored in $ADODB_CACHE
  41  */
  42  
  43  	 class ADODB_Cache_MemCache {
  44  	 	 var $createdir = false; // create caching directory structure?
  45  
  46  	 	 // $library will be populated with the proper library on connect
  47  	 	 // and is used later when there are differences in specific calls
  48  	 	 // between memcache and memcached
  49  	 	 var $library = false;
  50  
  51  	 	 //-----------------------------
  52  	 	 // memcache specific variables
  53  
  54  	 	 var $hosts;	 // array of hosts
  55  	 	 var $port = 11211;
  56  	 	 var $compress = false; // memcache compression with zlib
  57  
  58  	 	 var $_connected = false;
  59  	 	 var $_memcache = false;
  60  
  61  		function __construct(&$obj)
  62  	 	 {
  63  	 	 	 $this->hosts = $obj->memCacheHost;
  64  	 	 	 $this->port = $obj->memCachePort;
  65  	 	 	 $this->compress = $obj->memCacheCompress;
  66  	 	 }
  67  
  68  	 	 // implement as lazy connection. The connection only occurs on CacheExecute call
  69  		function connect(&$err)
  70  	 	 {
  71  	 	 	 // do we have memcache or memcached?
  72  	 	 	 if (class_exists('Memcache')) {
  73  	 	 	 	 $this->library='Memcache';
  74  	 	 	 	 $memcache = new MemCache;
  75  	 	 	 } elseif (class_exists('Memcached')) {
  76  	 	 	 	 $this->library='Memcached';
  77  	 	 	 	 $memcache = new MemCached;
  78  	 	 	 } else {
  79  	 	 	 	 $err = 'Neither the Memcache nor Memcached PECL extensions were found!';
  80  	 	 	 	 return false;
  81  	 	 	 }
  82  
  83  	 	 	 if (!is_array($this->hosts)) $this->hosts = array($this->hosts);
  84  
  85  	 	 	 $failcnt = 0;
  86  	 	 	 foreach($this->hosts as $host) {
  87  	 	 	 	 if (!@$memcache->addServer($host,$this->port)) {
  88  	 	 	 	 	 $failcnt += 1;
  89  	 	 	 	 }
  90  	 	 	 }
  91  	 	 	 if ($failcnt == sizeof($this->hosts)) {
  92  	 	 	 	 $err = 'Can\'t connect to any memcache server';
  93  	 	 	 	 return false;
  94  	 	 	 }
  95  	 	 	 $this->_connected = true;
  96  	 	 	 $this->_memcache = $memcache;
  97  	 	 	 return true;
  98  	 	 }
  99  
 100  	 	 // returns true or false. true if successful save
 101  		function writecache($filename, $contents, $debug, $secs2cache)
 102  	 	 {
 103  	 	 	 if (!$this->_connected) {
 104  	 	 	 	 $err = '';
 105  	 	 	 	 if (!$this->connect($err) && $debug) ADOConnection::outp($err);
 106  	 	 	 }
 107  	 	 	 if (!$this->_memcache) return false;
 108  
 109  	 	 	 $failed=false;
 110  	 	 	 switch ($this->library) {
 111  	 	 	 	 case 'Memcache':
 112  	 	 	 	 	 if (!$this->_memcache->set($filename, $contents, $this->compress ? MEMCACHE_COMPRESSED : 0, $secs2cache)) {
 113  	 	 	 	 	 	 $failed=true;
 114  	 	 	 	 	 }
 115  	 	 	 	 	 break;
 116  	 	 	 	 case 'Memcached':
 117  	 	 	 	 	 if (!$this->_memcache->set($filename, $contents, $secs2cache)) {
 118  	 	 	 	 	 	 $failed=true;
 119  	 	 	 	 	 }
 120  	 	 	 	 	 break;
 121  	 	 	 	 default:
 122  	 	 	 	 	 $failed=true;
 123  	 	 	 	 	 break;
 124  	 	 	 }
 125  
 126  	 	 	 if($failed) {
 127  	 	 	 	 if ($debug) ADOConnection::outp(" Failed to save data at the memcache server!<br>\n");
 128  	 	 	 	 return false;
 129  	 	 	 }
 130  
 131  	 	 	 return true;
 132  	 	 }
 133  
 134  	 	 // returns a recordset
 135  		function readcache($filename, &$err, $secs2cache, $rsClass)
 136  	 	 {
 137  	 	 	 $false = false;
 138  	 	 	 if (!$this->_connected) $this->connect($err);
 139  	 	 	 if (!$this->_memcache) return $false;
 140  
 141  	 	 	 $rs = $this->_memcache->get($filename);
 142  	 	 	 if (!$rs) {
 143  	 	 	 	 $err = 'Item with such key doesn\'t exist on the memcache server.';
 144  	 	 	 	 return $false;
 145  	 	 	 }
 146  
 147  	 	 	 // hack, should actually use _csv2rs
 148  	 	 	 $rs = explode("\n", $rs);
 149              unset($rs[0]);
 150              $rs = join("\n", $rs);
 151   	 	 	 $rs = unserialize($rs);
 152  	 	 	 if (! is_object($rs)) {
 153  	 	 	 	 $err = 'Unable to unserialize $rs';
 154  	 	 	 	 return $false;
 155  	 	 	 }
 156  	 	 	 if ($rs->timeCreated == 0) return $rs; // apparently have been reports that timeCreated was set to 0 somewhere
 157  
 158  	 	 	 $tdiff = intval($rs->timeCreated+$secs2cache - time());
 159  	 	 	 if ($tdiff <= 2) {
 160  	 	 	 	 switch($tdiff) {
 161  	 	 	 	 	 case 2:
 162  	 	 	 	 	 	 if ((rand() & 15) == 0) {
 163  	 	 	 	 	 	 	 $err = "Timeout 2";
 164  	 	 	 	 	 	 	 return $false;
 165  	 	 	 	 	 	 }
 166  	 	 	 	 	 	 break;
 167  	 	 	 	 	 case 1:
 168  	 	 	 	 	 	 if ((rand() & 3) == 0) {
 169  	 	 	 	 	 	 	 $err = "Timeout 1";
 170  	 	 	 	 	 	 	 return $false;
 171  	 	 	 	 	 	 }
 172  	 	 	 	 	 	 break;
 173  	 	 	 	 	 default:
 174  	 	 	 	 	 	 $err = "Timeout 0";
 175  	 	 	 	 	 	 return $false;
 176  	 	 	 	 }
 177  	 	 	 }
 178  	 	 	 return $rs;
 179  	 	 }
 180  
 181  		function flushall($debug=false)
 182  	 	 {
 183  	 	 	 if (!$this->_connected) {
 184  	 	 	 	 $err = '';
 185  	 	 	 	 if (!$this->connect($err) && $debug) ADOConnection::outp($err);
 186  	 	 	 }
 187  	 	 	 if (!$this->_memcache) return false;
 188  
 189  	 	 	 $del = $this->_memcache->flush();
 190  
 191  	 	 	 if ($debug)
 192  	 	 	 	 if (!$del) ADOConnection::outp("flushall: failed!<br>\n");
 193  	 	 	 	 else ADOConnection::outp("flushall: succeeded!<br>\n");
 194  
 195  	 	 	 return $del;
 196  	 	 }
 197  
 198  		function flushcache($filename, $debug=false)
 199  	 	 {
 200  	 	 	 if (!$this->_connected) {
 201    	 	 	 	 $err = '';
 202    	 	 	 	 if (!$this->connect($err) && $debug) ADOConnection::outp($err);
 203  	 	 	 }
 204  	 	 	 if (!$this->_memcache) return false;
 205  
 206  	 	 	 $del = $this->_memcache->delete($filename);
 207  
 208  	 	 	 if ($debug)
 209  	 	 	 	 if (!$del) ADOConnection::outp("flushcache: $key entry doesn't exist on memcache server!<br>\n");
 210  	 	 	 	 else ADOConnection::outp("flushcache: $key entry flushed from memcache server!<br>\n");
 211  
 212  	 	 	 return $del;
 213  	 	 }
 214  
 215  	 	 // not used for memcache
 216  		function createdir($dir, $hash)
 217  	 	 {
 218  	 	 	 return true;
 219  	 	 }
 220  	 }