Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; Chris@0: Chris@14: @trigger_error(sprintf('The class %s is deprecated since Symfony 3.4 and will be removed in 4.0. Use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler instead.', MemcacheSessionHandler::class), E_USER_DEPRECATED); Chris@14: Chris@0: /** Chris@14: * @author Drak Chris@0: * Chris@14: * @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler instead. Chris@0: */ Chris@0: class MemcacheSessionHandler implements \SessionHandlerInterface Chris@0: { Chris@0: private $memcache; Chris@0: Chris@0: /** Chris@0: * @var int Time to live in seconds Chris@0: */ Chris@0: private $ttl; Chris@0: Chris@0: /** Chris@0: * @var string Key prefix for shared environments Chris@0: */ Chris@0: private $prefix; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * List of available options: Chris@0: * * prefix: The prefix to use for the memcache keys in order to avoid collision Chris@0: * * expiretime: The time to live in seconds Chris@0: * Chris@0: * @param \Memcache $memcache A \Memcache instance Chris@0: * @param array $options An associative array of Memcache options Chris@0: * Chris@0: * @throws \InvalidArgumentException When unsupported options are passed Chris@0: */ Chris@17: public function __construct(\Memcache $memcache, array $options = []) Chris@0: { Chris@17: if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) { Chris@17: throw new \InvalidArgumentException(sprintf('The following options are not supported "%s"', implode(', ', $diff))); Chris@0: } Chris@0: Chris@0: $this->memcache = $memcache; Chris@0: $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400; Chris@0: $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function open($savePath, $sessionName) Chris@0: { Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function close() Chris@0: { Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function read($sessionId) Chris@0: { Chris@0: return $this->memcache->get($this->prefix.$sessionId) ?: ''; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function write($sessionId, $data) Chris@0: { Chris@0: return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function destroy($sessionId) Chris@0: { Chris@14: $this->memcache->delete($this->prefix.$sessionId); Chris@14: Chris@14: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function gc($maxlifetime) Chris@0: { Chris@0: // not required here because memcache will auto expire the records anyhow. Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return a Memcache instance. Chris@0: * Chris@0: * @return \Memcache Chris@0: */ Chris@0: protected function getMemcache() Chris@0: { Chris@0: return $this->memcache; Chris@0: } Chris@0: }