[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/Session/Storage/Handler/ -> MemcachedSessionHandler.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  13  
  14  /**
  15   * Memcached based session storage handler based on the Memcached class
  16   * provided by the PHP memcached extension.
  17   *
  18   * @see https://php.net/memcached
  19   *
  20   * @author Drak <drak@zikula.org>
  21   */
  22  class MemcachedSessionHandler extends AbstractSessionHandler
  23  {
  24      private $memcached;
  25  
  26      /**
  27       * @var int Time to live in seconds
  28       */
  29      private $ttl;
  30  
  31      /**
  32       * @var string Key prefix for shared environments
  33       */
  34      private $prefix;
  35  
  36      /**
  37       * Constructor.
  38       *
  39       * List of available options:
  40       *  * prefix: The prefix to use for the memcached keys in order to avoid collision
  41       *  * expiretime: The time to live in seconds.
  42       *
  43       * @throws \InvalidArgumentException When unsupported options are passed
  44       */
  45      public function __construct(\Memcached $memcached, array $options = [])
  46      {
  47          $this->memcached = $memcached;
  48  
  49          if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) {
  50              throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
  51          }
  52  
  53          $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
  54          $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
  55      }
  56  
  57      /**
  58       * @return bool
  59       */
  60      public function close()
  61      {
  62          return $this->memcached->quit();
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      protected function doRead($sessionId)
  69      {
  70          return $this->memcached->get($this->prefix.$sessionId) ?: '';
  71      }
  72  
  73      /**
  74       * @return bool
  75       */
  76      public function updateTimestamp($sessionId, $data)
  77      {
  78          $this->memcached->touch($this->prefix.$sessionId, time() + $this->ttl);
  79  
  80          return true;
  81      }
  82  
  83      /**
  84       * {@inheritdoc}
  85       */
  86      protected function doWrite($sessionId, $data)
  87      {
  88          return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
  89      }
  90  
  91      /**
  92       * {@inheritdoc}
  93       */
  94      protected function doDestroy($sessionId)
  95      {
  96          $result = $this->memcached->delete($this->prefix.$sessionId);
  97  
  98          return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
  99      }
 100  
 101      /**
 102       * @return bool
 103       */
 104      public function gc($maxlifetime)
 105      {
 106          // not required here because memcached will auto expire the records anyhow.
 107          return true;
 108      }
 109  
 110      /**
 111       * Return a Memcached instance.
 112       *
 113       * @return \Memcached
 114       */
 115      protected function getMemcached()
 116      {
 117          return $this->memcached;
 118      }
 119  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1