4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
15 * MemcachedSessionHandler.
17 * Memcached based session storage handler based on the Memcached class
18 * provided by the PHP memcached extension.
20 * @see http://php.net/memcached
22 * @author Drak <drak@zikula.org>
24 class MemcachedSessionHandler implements \SessionHandlerInterface
27 * @var \Memcached Memcached driver
32 * @var int Time to live in seconds
37 * @var string Key prefix for shared environments
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
48 * @param \Memcached $memcached A \Memcached instance
49 * @param array $options An associative array of Memcached options
51 * @throws \InvalidArgumentException When unsupported options are passed
53 public function __construct(\Memcached $memcached, array $options = array())
55 $this->memcached = $memcached;
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)
63 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
64 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
70 public function open($savePath, $sessionName)
78 public function close()
86 public function read($sessionId)
88 return $this->memcached->get($this->prefix.$sessionId) ?: '';
94 public function write($sessionId, $data)
96 return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
102 public function destroy($sessionId)
104 return $this->memcached->delete($this->prefix.$sessionId);
110 public function gc($maxlifetime)
112 // not required here because memcached will auto expire the records anyhow.
117 * Return a Memcached instance.
121 protected function getMemcached()
123 return $this->memcached;