[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

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

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2020 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   * Redis Cache Handler
  13   */
  14  class redisCacheHandler implements CacheHandlerInterface
  15  {
  16      /**
  17       * The redis server resource
  18       *
  19       * @var Redis
  20       */
  21      public $redis;
  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          // Check if extension is loaded
  35          if(!extension_loaded("Redis"))
  36          {
  37              // Throw our super awesome cache loading error
  38              $mybb->trigger_generic_error("redis_load_error");
  39              die;
  40          }
  41      }
  42  
  43      /**
  44       * Connect and initialize this handler.
  45       *
  46       * @return boolean True if successful, false on failure
  47       */
  48  	function connect()
  49      {
  50          global $mybb, $error_handler;
  51  
  52          $this->redis = new Redis;
  53  
  54          if(!$mybb->config['redis']['host'])
  55          {
  56              $message = "Please configure the redis settings in inc/config.php before attempting to use this cache handler";
  57              $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
  58              die;
  59          }
  60          if($mybb->config['redis']['port'])
  61          {
  62              $ret = $this->redis->pconnect($mybb->config['redis']['host'], $mybb->config['redis']['port']);
  63          }
  64          else
  65          {
  66              $ret = $this->redis->pconnect($mybb->config['redis']['host']);
  67          }
  68  
  69  
  70          if(!$ret)
  71          {
  72              $message = "Unable to connect to the redis server on {$mybb->config['redis']['host']}:{$mybb->config['redis']['port']}. Are you sure it is running?";
  73              $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
  74              die;
  75          }
  76  
  77          // Set a unique identifier for all queries in case other forums are using the same redis server
  78          $this->unique_id = md5(MYBB_ROOT);
  79  
  80          return true;
  81      }
  82  
  83      /**
  84       * Retrieve an item from the cache.
  85       *
  86       * @param string $name The name of the cache
  87       * @return mixed Cache data if successful, false if failure
  88       */
  89  	function fetch($name)
  90      {
  91          $data = $this->redis->get($this->unique_id."_".$name);
  92  
  93          if($data === false)
  94          {
  95              return false;
  96          }
  97  
  98          // use native_unserialize() over my_unserialize() for performance reasons
  99          return native_unserialize($data);
 100      }
 101  
 102      /**
 103       * Write an item to the cache.
 104       *
 105       * @param string $name The name of the cache
 106       * @param mixed $contents The data to write to the cache item
 107       * @return boolean True on success, false on failure
 108       */
 109  	function put($name, $contents)
 110      {
 111          return $this->redis->set($this->unique_id."_".$name, serialize($contents));
 112      }
 113  
 114      /**
 115       * Delete a cache
 116       *
 117       * @param string $name The name of the cache
 118       * @return boolean True on success, false on failure
 119       */
 120  	function delete($name)
 121      {
 122          return $this->redis->del($this->unique_id."_".$name);
 123      }
 124  
 125      /**
 126       * Disconnect from the cache
 127       */
 128  	function disconnect()
 129      {
 130          @$this->redis->close();
 131      }
 132  
 133      /**
 134       * @param string $name
 135       *
 136       * @return string
 137       */
 138  	function size_of($name='')
 139      {
 140          global $lang;
 141  
 142          return $lang->na;
 143      }
 144  }
 145  


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