Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
RedisSessionHandler added for Symfony Session
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Sep 19, 2017
1 parent 10fe47b commit 3b0c564
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"autoload": {
"psr-4": {
"Selami\\Factories\\": "src/"
}
},
"file": [
"src/Adapters/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

namespace Selami\Factories\Adapters\Symfony\Component\HttpFoundation\Session\Storage\Handler;

use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Redis;

class RedisSessionHandler implements \SessionHandlerInterface
{
private $redis;
private $ttl;
private $prefix;

public function __construct(Redis $redis, array $options = array())
{
$this->redis = $redis;
if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) {
throw new \InvalidArgumentException(sprintf(
'The following options are not supported "%s"', implode(', ', $diff)
));
}
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
$this->prefix = $options['prefix'] ?? 'selami';
}

public function open($savePath, $sessionName) : bool
{
return true;
}

public function close() : bool
{
return true;
}

public function read($sessionId) : string
{
return $this->redis->get($this->prefix.$sessionId) ?: '';
}

public function write($sessionId, $data) : bool
{
return $this->redis->set($this->prefix.$sessionId, $data, time() + $this->ttl);
}

public function destroy($sessionId)
{
return $this->redis->delete($this->prefix.$sessionId);
}

public function gc($maxlifetime) : bool
{
// not required here because redis will auto expire the records anyhow.
return true;
}

}
24 changes: 24 additions & 0 deletions src/SymfonySessionRedisFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Selami\Factories;

use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;
use Symfony\Component\HttpFoundation\Session\Session;
use Memcached;

class SymfonySessionRedisFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) : Session
{
ini_set('session.use_cookies', '1');
ini_set('session.use_only_cookies', '1');
ini_set('session.cookie_httponly', '1');
ini_set('session.name', 'SELAMISESSID');
$storage = new NativeSessionStorage(array(), new MemcachedSessionHandler($container->get(Memcached::class)));
return new Session($storage);
}
}

0 comments on commit 3b0c564

Please sign in to comment.