Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

  • MemcachedSessionHandler
  • MemcacheSessionHandler
  • MongoDbSessionHandler
  • NativeFileSessionHandler
  • NativeSessionHandler
  • NullSessionHandler
  • PdoSessionHandler
  • WriteCheckSessionHandler
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * This file is part of the Symfony package.
  5:  *
  6:  * (c) Fabien Potencier <[email protected]>
  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:  * MemcachedSessionHandler.
 16:  *
 17:  * Memcached based session storage handler based on the Memcached class
 18:  * provided by the PHP memcached extension.
 19:  *
 20:  * @see http://php.net/memcached
 21:  *
 22:  * @author Drak <[email protected]>
 23:  */
 24: class MemcachedSessionHandler implements \SessionHandlerInterface
 25: {
 26:     /**
 27:      * @var \Memcached Memcached driver
 28:      */
 29:     private $memcached;
 30: 
 31:     /**
 32:      * @var int Time to live in seconds
 33:      */
 34:     private $ttl;
 35: 
 36:     /**
 37:      * @var string Key prefix for shared environments
 38:      */
 39:     private $prefix;
 40: 
 41:     /**
 42:      * Constructor.
 43:      *
 44:      * List of available options:
 45:      *  * prefix: The prefix to use for the memcached keys in order to avoid collision
 46:      *  * expiretime: The time to live in seconds
 47:      *
 48:      * @param \Memcached $memcached A \Memcached instance
 49:      * @param array      $options   An associative array of Memcached options
 50:      *
 51:      * @throws \InvalidArgumentException When unsupported options are passed
 52:      */
 53:     public function __construct(\Memcached $memcached, array $options = array())
 54:     {
 55:         $this->memcached = $memcached;
 56: 
 57:         if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
 58:             throw new \InvalidArgumentException(sprintf(
 59:                 'The following options are not supported "%s"', implode(', ', $diff)
 60:             ));
 61:         }
 62: 
 63:         $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
 64:         $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
 65:     }
 66: 
 67:     /**
 68:      * {@inheritdoc}
 69:      */
 70:     public function open($savePath, $sessionName)
 71:     {
 72:         return true;
 73:     }
 74: 
 75:     /**
 76:      * {@inheritdoc}
 77:      */
 78:     public function close()
 79:     {
 80:         return true;
 81:     }
 82: 
 83:     /**
 84:      * {@inheritdoc}
 85:      */
 86:     public function read($sessionId)
 87:     {
 88:         return $this->memcached->get($this->prefix.$sessionId) ?: '';
 89:     }
 90: 
 91:     /**
 92:      * {@inheritdoc}
 93:      */
 94:     public function write($sessionId, $data)
 95:     {
 96:         return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
 97:     }
 98: 
 99:     /**
100:      * {@inheritdoc}
101:      */
102:     public function destroy($sessionId)
103:     {
104:         $result = $this->memcached->delete($this->prefix.$sessionId);
105: 
106:         return $result || $this->memcached->getResultCode() == \Memcached::RES_NOTFOUND;
107:     }
108: 
109:     /**
110:      * {@inheritdoc}
111:      */
112:     public function gc($maxlifetime)
113:     {
114:         // not required here because memcached will auto expire the records anyhow.
115:         return true;
116:     }
117: 
118:     /**
119:      * Return a Memcached instance.
120:      *
121:      * @return \Memcached
122:      */
123:     protected function getMemcached()
124:     {
125:         return $this->memcached;
126:     }
127: }
128: 
Ratchet API documentation generated by ApiGen 2.8.0