<?php
namespace App\Extensions;
class MemcacheSessionHandler implements \SessionHandlerInterface
{
/**
* @var \Memcache Memcache driver.
*/
private $memcache;
/**
* @var int Time to live in seconds
*/
private $ttl;
/**
* Constructor.
*
* @param \Memcache $memcache A \Memcache instance
* @param array $options An associative array of Memcache options
*
*/
public function __construct(\Memcache $memcache, array $options = array())
{
$this->memcache = $memcache;
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
}
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return $this->memcache->close();
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
return $this->memcache->get($sessionId);
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
return $this->memcache->set($sessionId, $data, 0, time() + $this->ttl);
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
return $this->memcache->delete($sessionId);
}
/**
* {@inheritdoc}
*/
public function gc($maxlifetime)
{
// not required here because memcache will auto expire the records anyhow.
return true;
}
/**
* Return a Memcache instance
*
* @return \Memcache
*/
protected function getMemcache()
{
return $this->memcache;
}
}