-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathPsr16SessionStore.php
More file actions
79 lines (69 loc) · 1.81 KB
/
Copy pathPsr16SessionStore.php
File metadata and controls
79 lines (69 loc) · 1.81 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
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Server\Session;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Uid\Uuid;
/**
* PSR-16 compliant cache-based session store.
*
* This implementation uses any PSR-16 compliant cache as the storage backend
* for session data. Each session is stored with a prefixed key using the session ID.
*
* @author luoyue <1569097443@qq.com>
*/
class Psr16SessionStore implements SessionStoreInterface
{
public function __construct(
private readonly CacheInterface $cache,
private readonly string $prefix = 'mcp-',
private readonly int $ttl = 3600,
) {
}
public function exists(Uuid $id): bool
{
try {
return $this->cache->has($this->getKey($id));
} catch (\Throwable) {
return false;
}
}
public function read(Uuid $id): string|false
{
try {
return $this->cache->get($this->getKey($id), false);
} catch (\Throwable) {
return false;
}
}
public function write(Uuid $id, string $data): bool
{
try {
return $this->cache->set($this->getKey($id), $data, $this->ttl);
} catch (\Throwable) {
return false;
}
}
public function destroy(Uuid $id): bool
{
try {
return $this->cache->delete($this->getKey($id));
} catch (\Throwable) {
return false;
}
}
public function gc(): array
{
return [];
}
private function getKey(Uuid $id): string
{
return $this->prefix.$id;
}
}