|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Session\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
8
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
|
9
|
|
|
use Spiral\Cache\CacheStorageProviderInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class CacheHandler implements \SessionHandlerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private readonly CacheInterface $cache; |
|
14
|
|
|
|
|
15
|
7 |
|
public function __construct( |
|
16
|
|
|
CacheStorageProviderInterface $storageProvider, |
|
17
|
|
|
private readonly ?string $storage = null, |
|
18
|
|
|
private readonly int $ttl = 86400, |
|
19
|
|
|
private readonly string $prefix = 'session:' |
|
20
|
|
|
) { |
|
21
|
7 |
|
$this->cache = $storageProvider->storage($this->storage); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
public function close(): bool |
|
25
|
|
|
{ |
|
26
|
1 |
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @throws InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function destroy(string $id): bool |
|
33
|
|
|
{ |
|
34
|
1 |
|
$this->cache->delete($this->getKey($id)); |
|
35
|
|
|
|
|
36
|
1 |
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function gc(int $max_lifetime): int|false |
|
40
|
|
|
{ |
|
41
|
1 |
|
return 0; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function open(string $path, string $name): bool |
|
45
|
|
|
{ |
|
46
|
1 |
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @throws InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function read(string $id): string|false |
|
53
|
|
|
{ |
|
54
|
2 |
|
$result = $this->cache->get($this->getKey($id)); |
|
55
|
|
|
|
|
56
|
2 |
|
return (string) $result; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @throws InvalidArgumentException |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function write(string $id, string $data): bool |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->cache->set($this->getKey($id), $data, $this->ttl); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
private function getKey(string $id): string |
|
68
|
|
|
{ |
|
69
|
4 |
|
return $this->prefix . $id; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|