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 * MemcacheSessionHandler.
17 * @author Drak <drak@zikula.org>
19 class MemcacheSessionHandler implements \SessionHandlerInterface
22 * @var \Memcache Memcache driver
27 * @var int Time to live in seconds
32 * @var string Key prefix for shared environments
39 * List of available options:
40 * * prefix: The prefix to use for the memcache keys in order to avoid collision
41 * * expiretime: The time to live in seconds
43 * @param \Memcache $memcache A \Memcache instance
44 * @param array $options An associative array of Memcache options
46 * @throws \InvalidArgumentException When unsupported options are passed
48 public function __construct(\Memcache $memcache, array $options = array())
50 if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
51 throw new \InvalidArgumentException(sprintf(
52 'The following options are not supported "%s"', implode(', ', $diff)
56 $this->memcache = $memcache;
57 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
58 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
64 public function open($savePath, $sessionName)
72 public function close()
80 public function read($sessionId)
82 return $this->memcache->get($this->prefix.$sessionId) ?: '';
88 public function write($sessionId, $data)
90 return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl);
96 public function destroy($sessionId)
98 return $this->memcache->delete($this->prefix.$sessionId);
104 public function gc($maxlifetime)
106 // not required here because memcache will auto expire the records anyhow.
111 * Return a Memcache instance.
115 protected function getMemcache()
117 return $this->memcache;