-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpFastCacheSessionHandler.php
More file actions
107 lines (94 loc) · 2.4 KB
/
Copy pathPhpFastCacheSessionHandler.php
File metadata and controls
107 lines (94 loc) · 2.4 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Detain\SessionSamurai;
use Psr\Cache\CacheItemPoolInterface;
class PhpFastCacheSessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
private CacheItemPoolInterface $cache;
public function __construct(CacheItemPoolInterface $cache = null)
{
if ($cache === null) {
$cacheConfig = new \Phpfastcache\Config\ConfigurationOption([
'path' => sys_get_temp_dir(),
'itemDetailedDate' => true,
]);
$cache = \Phpfastcache\CacheManager::getInstance('files', $cacheConfig);
}
$this->cache = $cache;
}
/**
* {@inheritdoc}
*/
public function open(string $savePath, string $sessionName): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function read(string $sessionId): string
{
$item = $this->cache->getItem($sessionId);
if (!$item->isHit()) {
return '';
}
return (string) $item->get();
}
/**
* {@inheritdoc}
*/
public function write(string $sessionId, string $data): bool
{
$item = $this->cache->getItem($sessionId);
$item->set($data);
$item->expiresAfter((int) ini_get('session.gc_maxlifetime'));
$this->cache->save($item);
return true;
}
/**
* {@inheritdoc}
*/
public function destroy(string $sessionId): bool
{
$this->cache->deleteItem($sessionId);
return true;
}
/**
* {@inheritdoc}
*/
public function gc(int $maxlifetime): int|false
{
return 0;
}
/**
* {@inheritdoc}
*/
public function validateId(string $sessionId): bool
{
return $this->cache->getItem($sessionId)->isHit();
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $sessionId, string $data): bool
{
$item = $this->cache->getItem($sessionId);
$item->expiresAfter((int) ini_get('session.gc_maxlifetime'));
$this->cache->save($item);
return true;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
return bin2hex(random_bytes(32));
}
}