[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/cachehandlers/ -> memcached.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  /**
  12   * Memcached Cache Handler
  13   */
  14  class memcachedCacheHandler implements CacheHandlerInterface
  15  {
  16      /**
  17       * The memcached server resource
  18       *
  19       * @var Memcached
  20       */
  21      public $memcached;
  22  
  23      /**
  24       * Unique identifier representing this copy of MyBB
  25       *
  26       * @var string
  27       */
  28      public $unique_id;
  29  
  30  	function __construct()
  31      {
  32          global $mybb;
  33  
  34          if(!function_exists("memcached_connect"))
  35          {
  36              // Check if our Memcached extension is loaded
  37              if(!extension_loaded("Memcached"))
  38              {
  39                  // Throw our super awesome cache loading error
  40                  $mybb->trigger_generic_error("memcached_load_error");
  41                  die;
  42              }
  43          }
  44      }
  45  
  46      /**
  47       * Connect and initialize this handler.
  48       *
  49       * @return boolean True if successful, false on failure
  50       */
  51  	function connect()
  52      {
  53          global $mybb, $error_handler;
  54  
  55          $this->memcached = new Memcached;
  56  
  57          if($mybb->config['memcache']['host'])
  58          {
  59              $mybb->config['memcache'][0] = $mybb->config['memcache'];
  60              unset($mybb->config['memcache']['host']);
  61              unset($mybb->config['memcache']['port']);
  62          }
  63  
  64          foreach($mybb->config['memcache'] as $memcached)
  65          {
  66              if(!$memcached['host'])
  67              {
  68                  $message = "Please configure the memcache settings in inc/config.php before attempting to use this cache handler";
  69                  $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
  70                  die;
  71              }
  72  
  73              if(!isset($memcached['port']))
  74              {
  75                  $memcached['port'] = "11211";
  76              }
  77  
  78              $this->memcached->addServer($memcached['host'], $memcached['port']);
  79  
  80              if(!$this->memcached)
  81              {
  82                  $message = "Unable to connect to the memcached server on {$memcached['memcache_host']}:{$memcached['memcache_port']}. Are you sure it is running?";
  83                  $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
  84                  die;
  85              }
  86          }
  87  
  88          // Set a unique identifier for all queries in case other forums are using the same memcache server
  89          $this->unique_id = md5(MYBB_ROOT);
  90  
  91          return true;
  92      }
  93  
  94      /**
  95       * Retrieve an item from the cache.
  96       *
  97       * @param string $name The name of the cache
  98       * @return mixed Cache data if successful, false if failure
  99       */
 100  	function fetch($name)
 101      {
 102          $data = $this->memcached->get($this->unique_id."_".$name);
 103  
 104          if($data === false)
 105          {
 106              return false;
 107          }
 108          else
 109          {
 110              return $data;
 111          }
 112      }
 113  
 114      /**
 115       * Write an item to the cache.
 116       *
 117       * @param string $name The name of the cache
 118       * @param mixed $contents The data to write to the cache item
 119       * @return boolean True on success, false on failure
 120       */
 121  	function put($name, $contents)
 122      {
 123          return $this->memcached->set($this->unique_id."_".$name, $contents);
 124      }
 125  
 126      /**
 127       * Delete a cache
 128       *
 129       * @param string $name The name of the cache
 130       * @return boolean True on success, false on failure
 131       */
 132  	function delete($name)
 133      {
 134          return $this->memcached->delete($this->unique_id."_".$name);
 135      }
 136  
 137      /**
 138       * Disconnect from the cache
 139       */
 140  	function disconnect()
 141      {
 142          @$this->memcached->quit();
 143      }
 144  
 145      /**
 146       * @param string $name
 147       *
 148       * @return string
 149       */
 150  	function size_of($name='')
 151      {
 152          global $lang;
 153  
 154          return $lang->na;
 155      }
 156  }
 157  


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref