Skip to content

Commit

Permalink
Added session kv cache driver
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Nov 21, 2022
1 parent a362e5a commit 34ddeeb
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 7 deletions.
6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -29,11 +29,13 @@
"require": {
"php": ">=8.1",
"spiral/core": "^3.4",
"spiral/files": "^3.4"
"spiral/files": "^3.4",
"spiral/cache": "^3.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5.20",
"vimeo/psalm": "^4.27"
"vimeo/psalm": "^4.27",
"mockery/mockery": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions phpunit.xml
Expand Up @@ -16,11 +16,11 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
Expand Down
71 changes: 71 additions & 0 deletions src/Handler/CacheHandler.php
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Spiral\Session\Handler;

use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Spiral\Cache\CacheStorageProviderInterface;

final class CacheHandler implements \SessionHandlerInterface
{
private readonly CacheInterface $cache;

public function __construct(
CacheStorageProviderInterface $storageProvider,
private readonly ?string $storage = null,
private readonly int $ttl = 86400,
private readonly string $prefix = 'session:'
) {
$this->cache = $storageProvider->storage($this->storage);
}

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

/**
* @throws InvalidArgumentException
*/
public function destroy(string $id): bool
{
$this->cache->delete($this->getKey($id));

return true;
}

public function gc(int $max_lifetime): int|false
{
return 0;
}

public function open(string $path, string $name): bool
{
return true;
}

/**
* @throws InvalidArgumentException
*/
public function read(string $id): string|false
{
$result = $this->cache->get($this->getKey($id));

return (string) $result;
}

/**
* @throws InvalidArgumentException
*/
public function write(string $id, string $data): bool
{
return $this->cache->set($this->getKey($id), $data, $this->ttl);
}

private function getKey(string $id): string
{
return $this->prefix . $id;
}
}
73 changes: 73 additions & 0 deletions tests/CacheHandlerTest.php
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Session;

use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
use Spiral\Cache\CacheStorageProviderInterface;
use Spiral\Session\Handler\CacheHandler;
use Mockery as m;

final class CacheHandlerTest extends TestCase
{
private CacheHandler $handler;
private m\MockInterface|CacheInterface $cache;

protected function setUp(): void
{
parent::setUp();

$storage = m::mock(CacheStorageProviderInterface::class);

$storage->shouldReceive('storage')->once()->andReturn($this->cache = m::mock(CacheInterface::class));

$this->handler = new CacheHandler(
$storage
);
}

public function testClose(): void
{
$this->assertTrue($this->handler->close());
}

public function testDestroy(): void
{
$this->cache->shouldReceive('delete')->with('session:foo')->andReturn(true);

$this->assertTrue($this->handler->destroy('foo'));
}

public function testGc(): void
{
$this->assertSame(0, $this->handler->gc(100));
}

public function testOpen(): void
{
$this->assertTrue($this->handler->open('root', 'test'));
}

public function testRead(): void
{
$this->cache->shouldReceive('get')->with('session:foo')->andReturn('bar');

$this->assertSame('bar', $this->handler->read('foo'));
}

public function testReadExpired(): void
{
$this->cache->shouldReceive('get')->with('session:foo')->andReturn(null);

$this->assertSame('', $this->handler->read('foo'));
}

public function testWrite(): void
{
$this->cache->shouldReceive('set')->with('session:foo', 'bar', 86400)->andReturn(true);

$this->assertTrue($this->handler->write('foo', 'bar'));
}
}

0 comments on commit 34ddeeb

Please sign in to comment.