[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/adodb/ -> adodb-memcache.lib.inc.php (source)

   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  /* 
  10  
  11    V4.90 8 June 2006  (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
  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    
  17    Latest version is available at http://adodb.sourceforge.net
  18    
  19  */
  20  
  21      function &getmemcache($key,&$err, $timeout=0, $host, $port)
  22      {
  23          $false = false;
  24          $err = false;
  25  
  26          if (!function_exists('memcache_pconnect')) {
  27              $err = 'Memcache module PECL extension not found!';
  28              return $false;
  29          }
  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;
  35          }
  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;
  41          }
  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;
  61              }
  62          }
  63          return $rs;
  64      }
  65  
  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  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7