[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/phpbb/cache/driver/ -> memcached.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_MEMCACHED_PORT'))
  17  {
  18      define('PHPBB_ACM_MEMCACHED_PORT', 11211);
  19  }
  20  
  21  if (!defined('PHPBB_ACM_MEMCACHED_COMPRESS'))
  22  {
  23      define('PHPBB_ACM_MEMCACHED_COMPRESS', true);
  24  }
  25  
  26  if (!defined('PHPBB_ACM_MEMCACHED_HOST'))
  27  {
  28      define('PHPBB_ACM_MEMCACHED_HOST', 'localhost');
  29  }
  30  
  31  if (!defined('PHPBB_ACM_MEMCACHED'))
  32  {
  33      //can define multiple servers with host1/port1,host2/port2 format
  34      define('PHPBB_ACM_MEMCACHED', PHPBB_ACM_MEMCACHED_HOST . '/' . PHPBB_ACM_MEMCACHED_PORT);
  35  }
  36  
  37  /**
  38  * ACM for Memcached
  39  */
  40  class memcached extends \phpbb\cache\driver\memory
  41  {
  42      /** @var string Extension to use */
  43      protected $extension = 'memcached';
  44  
  45      /** @var \Memcached Memcached class */
  46      protected $memcached;
  47  
  48      /** @var int Flags */
  49      protected $flags = 0;
  50  
  51      /**
  52       * Memcached constructor
  53       *
  54       * @param string $memcached_servers Memcached servers string (optional)
  55       */
  56  	public function __construct($memcached_servers = '')
  57      {
  58          // Call the parent constructor
  59          parent::__construct();
  60  
  61          $memcached_servers = $memcached_servers ?: PHPBB_ACM_MEMCACHED;
  62  
  63          $this->memcached = new \Memcached();
  64          $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
  65          // Memcached defaults to using compression, disable if we don't want
  66          // to use it
  67          if (!PHPBB_ACM_MEMCACHED_COMPRESS)
  68          {
  69              $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
  70          }
  71  
  72          $server_list = [];
  73          foreach (explode(',', $memcached_servers) as $u)
  74          {
  75              if (preg_match('#(.*)/(\d+)#', $u, $parts))
  76              {
  77                  $server_list[] = [trim($parts[1]), (int) trim($parts[2])];
  78              }
  79          }
  80  
  81          $this->memcached->addServers($server_list);
  82  
  83          if (empty($server_list) || empty($this->memcached->getStats()))
  84          {
  85              trigger_error('Could not connect to memcached server(s).');
  86          }
  87      }
  88  
  89      /**
  90      * {@inheritDoc}
  91      */
  92  	public function unload()
  93      {
  94          parent::unload();
  95  
  96          unset($this->memcached);
  97      }
  98  
  99      /**
 100      * {@inheritDoc}
 101      */
 102  	public function purge()
 103      {
 104          $this->memcached->flush();
 105  
 106          parent::purge();
 107      }
 108  
 109      /**
 110      * Fetch an item from the cache
 111      *
 112      * @param string $var Cache key
 113      *
 114      * @return mixed Cached data
 115      */
 116  	protected function _read($var)
 117      {
 118          return $this->memcached->get($this->key_prefix . $var);
 119      }
 120  
 121      /**
 122      * Store data in the cache
 123      *
 124      * @param string $var Cache key
 125      * @param mixed $data Data to store
 126      * @param int $ttl Time-to-live of cached data
 127      * @return bool True if the operation succeeded
 128      */
 129  	protected function _write($var, $data, $ttl = 2592000)
 130      {
 131          if (!$this->memcached->replace($this->key_prefix . $var, $data, $ttl))
 132          {
 133              return $this->memcached->set($this->key_prefix . $var, $data, $ttl);
 134          }
 135          return true;
 136      }
 137  
 138      /**
 139      * Remove an item from the cache
 140      *
 141      * @param string $var Cache key
 142      * @return bool True if the operation succeeded
 143      */
 144  	protected function _delete($var)
 145      {
 146          return $this->memcached->delete($this->key_prefix . $var);
 147      }
 148  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1