/
CacheHandler.php
71 lines (57 loc) · 1.53 KB
/
CacheHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
}
}