[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/cache/driver/ -> redis.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\cache\driver;
  15  
  16  if (!defined('PHPBB_ACM_REDIS_PORT'))
  17  {
  18      define('PHPBB_ACM_REDIS_PORT', 6379);
  19  }
  20  
  21  if (!defined('PHPBB_ACM_REDIS_HOST'))
  22  {
  23      define('PHPBB_ACM_REDIS_HOST', 'localhost');
  24  }
  25  
  26  /**
  27  * ACM for Redis
  28  *
  29  * Compatible with the php extension phpredis available
  30  * at https://github.com/nicolasff/phpredis
  31  *
  32  */
  33  class redis extends \phpbb\cache\driver\memory
  34  {
  35      var $extension = 'redis';
  36  
  37      var $redis;
  38  
  39      /**
  40      * Creates a redis cache driver.
  41      *
  42      * The following global constants affect operation:
  43      *
  44      * PHPBB_ACM_REDIS_HOST
  45      * PHPBB_ACM_REDIS_PORT
  46      * PHPBB_ACM_REDIS_PASSWORD
  47      * PHPBB_ACM_REDIS_DB
  48      *
  49      * There are no publicly documented constructor parameters.
  50      */
  51  	function __construct()
  52      {
  53          // Call the parent constructor
  54          parent::__construct();
  55  
  56          $this->redis = new \Redis();
  57  
  58          $args = func_get_args();
  59          if (!empty($args))
  60          {
  61              $ok = call_user_func_array(array($this->redis, 'connect'), $args);
  62          }
  63          else
  64          {
  65              $ok = $this->redis->connect(PHPBB_ACM_REDIS_HOST, PHPBB_ACM_REDIS_PORT);
  66          }
  67  
  68          if (!$ok)
  69          {
  70              trigger_error('Could not connect to redis server');
  71          }
  72  
  73          if (defined('PHPBB_ACM_REDIS_PASSWORD'))
  74          {
  75              if (!$this->redis->auth(PHPBB_ACM_REDIS_PASSWORD))
  76              {
  77                  global $acm_type;
  78  
  79                  trigger_error("Incorrect password for the ACM module $acm_type.", E_USER_ERROR);
  80              }
  81          }
  82  
  83          $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
  84          $this->redis->setOption(\Redis::OPT_PREFIX, $this->key_prefix);
  85  
  86          if (defined('PHPBB_ACM_REDIS_DB'))
  87          {
  88              if (!$this->redis->select(PHPBB_ACM_REDIS_DB))
  89              {
  90                  global $acm_type;
  91  
  92                  trigger_error("Incorrect database for the ACM module $acm_type.", E_USER_ERROR);
  93              }
  94          }
  95      }
  96  
  97      /**
  98      * {@inheritDoc}
  99      */
 100  	function unload()
 101      {
 102          parent::unload();
 103  
 104          $this->redis->close();
 105      }
 106  
 107      /**
 108      * {@inheritDoc}
 109      */
 110  	function purge()
 111      {
 112          $this->redis->flushDB();
 113  
 114          parent::purge();
 115      }
 116  
 117      /**
 118      * Fetch an item from the cache
 119      *
 120      * @access protected
 121      * @param string $var Cache key
 122      * @return mixed Cached data
 123      */
 124  	function _read($var)
 125      {
 126          return $this->redis->get($var);
 127      }
 128  
 129      /**
 130      * Store data in the cache
 131      *
 132      * @access protected
 133      * @param string $var Cache key
 134      * @param mixed $data Data to store
 135      * @param int $ttl Time-to-live of cached data
 136      * @return bool True if the operation succeeded
 137      */
 138  	function _write($var, $data, $ttl = 2592000)
 139      {
 140          if ($ttl == 0)
 141          {
 142              return $this->redis->set($var, $data);
 143          }
 144          return $this->redis->setex($var, $ttl, $data);
 145      }
 146  
 147      /**
 148      * Remove an item from the cache
 149      *
 150      * @access protected
 151      * @param string $var Cache key
 152      * @return bool True if the operation succeeded
 153      */
 154  	function _delete($var)
 155      {
 156          if ($this->redis->delete($var) > 0)
 157          {
 158              return true;
 159          }
 160          return false;
 161      }
 162  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1