[ Index ]

PHP Cross Reference of MyBB 1.8.27

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          return unserialize($data);
  98      }
  99  
 100      /**
 101       * Write an item to the cache.
 102       *
 103       * @param string $name The name of the cache
 104       * @param mixed $contents The data to write to the cache item
 105       * @return boolean True on success, false on failure
 106       */
 107  	function put($name, $contents)
 108      {
 109          return $this->redis->set($this->unique_id."_".$name, serialize($contents));
 110      }
 111  
 112      /**
 113       * Delete a cache
 114       *
 115       * @param string $name The name of the cache
 116       * @return boolean True on success, false on failure
 117       */
 118  	function delete($name)
 119      {
 120          return $this->redis->del($this->unique_id."_".$name);
 121      }
 122  
 123      /**
 124       * Disconnect from the cache
 125       */
 126  	function disconnect()
 127      {
 128          @$this->redis->close();
 129      }
 130  
 131      /**
 132       * @param string $name
 133       *
 134       * @return string
 135       */
 136  	function size_of($name='')
 137      {
 138          global $lang;
 139  
 140          return $lang->na;
 141      }
 142  }
 143  


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