composer package updates
[openemr.git] / vendor / symfony / http-foundation / Session / Storage / Handler / MemcachedSessionHandler.php
blob2c45face4da09a11f2cce237205ced8660681a89
1 <?php
3 /*
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;
14 /**
15 * Memcached based session storage handler based on the Memcached class
16 * provided by the PHP memcached extension.
18 * @see http://php.net/memcached
20 * @author Drak <drak@zikula.org>
22 class MemcachedSessionHandler implements \SessionHandlerInterface
24 private $memcached;
26 /**
27 * @var int Time to live in seconds
29 private $ttl;
31 /**
32 * @var string Key prefix for shared environments
34 private $prefix;
36 /**
37 * Constructor.
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
43 * @param \Memcached $memcached A \Memcached instance
44 * @param array $options An associative array of Memcached options
46 * @throws \InvalidArgumentException When unsupported options are passed
48 public function __construct(\Memcached $memcached, array $options = array())
50 $this->memcached = $memcached;
52 if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
53 throw new \InvalidArgumentException(sprintf(
54 'The following options are not supported "%s"', implode(', ', $diff)
55 ));
58 $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
59 $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
62 /**
63 * {@inheritdoc}
65 public function open($savePath, $sessionName)
67 return true;
70 /**
71 * {@inheritdoc}
73 public function close()
75 return true;
78 /**
79 * {@inheritdoc}
81 public function read($sessionId)
83 return $this->memcached->get($this->prefix.$sessionId) ?: '';
86 /**
87 * {@inheritdoc}
89 public function write($sessionId, $data)
91 return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
94 /**
95 * {@inheritdoc}
97 public function destroy($sessionId)
99 $result = $this->memcached->delete($this->prefix.$sessionId);
101 return $result || \Memcached::RES_NOTFOUND == $this->memcached->getResultCode();
105 * {@inheritdoc}
107 public function gc($maxlifetime)
109 // not required here because memcached will auto expire the records anyhow.
110 return true;
114 * Return a Memcached instance.
116 * @return \Memcached
118 protected function getMemcached()
120 return $this->memcached;