Rename Psr16StoreSession to Psr16SessionStore for consistency (#236)
This commit is contained in:
parent
77d6433e4b
commit
9621039e54
4 changed files with 10 additions and 5 deletions
|
|
@ -2,6 +2,11 @@
|
|||
|
||||
All notable changes to `mcp/sdk` will be documented in this file.
|
||||
|
||||
0.4.0
|
||||
-----
|
||||
|
||||
* Rename `Mcp\Server\Session\Psr16StoreSession` to `Mcp\Server\Session\Psr16SessionStore`
|
||||
|
||||
0.3.0
|
||||
-----
|
||||
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ By default, the SDK uses in-memory sessions. You can configure different session
|
|||
```php
|
||||
use Mcp\Server\Session\FileSessionStore;
|
||||
use Mcp\Server\Session\InMemorySessionStore;
|
||||
use Mcp\Server\Session\Psr16StoreSession;
|
||||
use Mcp\Server\Session\Psr16SessionStore;
|
||||
use Symfony\Component\Cache\Psr16Cache;
|
||||
use Symfony\Component\Cache\Adapter\RedisAdapter;
|
||||
|
||||
|
|
@ -220,7 +220,7 @@ $redisAdapter = new RedisAdapter(
|
|||
);
|
||||
|
||||
$server = Server::builder()
|
||||
->setSession(new Psr16StoreSession(
|
||||
->setSession(new Psr16SessionStore(
|
||||
cache: new Psr16Cache($redisAdapter),
|
||||
prefix: 'mcp-',
|
||||
ttl: 3600
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ Configure session storage and lifecycle. By default, the SDK uses `InMemorySessi
|
|||
```php
|
||||
use Mcp\Server\Session\FileSessionStore;
|
||||
use Mcp\Server\Session\InMemorySessionStore;
|
||||
use Mcp\Server\Session\Psr16StoreSession;
|
||||
use Mcp\Server\Session\Psr16SessionStore;
|
||||
use Symfony\Component\Cache\Psr16Cache;
|
||||
use Symfony\Component\Cache\Adapter\RedisAdapter;
|
||||
|
||||
|
|
@ -178,7 +178,7 @@ $redisAdapter = new RedisAdapter(
|
|||
);
|
||||
|
||||
$server = Server::builder()
|
||||
->setSession(new Psr16StoreSession(
|
||||
->setSession(new Psr16SessionStore(
|
||||
cache: new Psr16Cache($redisAdapter),
|
||||
prefix: 'mcp-',
|
||||
ttl: 3600
|
||||
|
|
|
|||
81
src/Server/Session/Psr16SessionStore.php
Normal file
81
src/Server/Session/Psr16SessionStore.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue