|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace KLP\KlpMcpServer\Transports\SseAdapters; |
| 4 | + |
| 5 | +use Psr\Cache\CacheItemPoolInterface; |
| 6 | +use Psr\Cache\InvalidArgumentException; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | + |
| 9 | +class CachePoolAdapter implements SseAdapterInterface |
| 10 | +{ |
| 11 | + private const DEFAULT_PREFIX = 'mcp_sse_'; |
| 12 | + |
| 13 | + private const DEFAULT_MESSAGE_TTL = 100; |
| 14 | + |
| 15 | + /** |
| 16 | + * Key prefix for SSE messages |
| 17 | + */ |
| 18 | + private string $keyPrefix; |
| 19 | + |
| 20 | + /** |
| 21 | + * Message expiration time in seconds |
| 22 | + */ |
| 23 | + private int $messageTtl; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly array $config, |
| 27 | + private readonly CacheItemPoolInterface $cache, |
| 28 | + private readonly ?LoggerInterface $logger |
| 29 | + ) { |
| 30 | + $this->keyPrefix = $this->config['prefix'] ?? self::DEFAULT_PREFIX; |
| 31 | + $this->messageTtl = (int) ($this->config['ttl'] ?? self::DEFAULT_MESSAGE_TTL); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Get the Redis key for a client's message queue |
| 36 | + * |
| 37 | + * @param string $clientId The client ID |
| 38 | + * @return string The Redis key |
| 39 | + */ |
| 40 | + private function generateQueueKey(string $clientId): string |
| 41 | + { |
| 42 | + return "$this->keyPrefix|client|$clientId"; |
| 43 | + } |
| 44 | + /** |
| 45 | + * @inheritDoc |
| 46 | + */ |
| 47 | + public function pushMessage(string $clientId, string $message): void |
| 48 | + { |
| 49 | + try { |
| 50 | + $cacheItem = $this->cache->getItem($this->generateQueueKey($clientId)); |
| 51 | + |
| 52 | + $messages = $cacheItem->isHit() ? $cacheItem->get() : [] ; |
| 53 | + $messages[] = $message; |
| 54 | + $cacheItem->set($messages); |
| 55 | + $cacheItem->expiresAfter($this->messageTtl); |
| 56 | + $this->cache->save($cacheItem); |
| 57 | + } catch (InvalidArgumentException $e) { |
| 58 | + $this->logger?->error('Failed to add message to cache: '.$e->getMessage()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @inheritDoc |
| 64 | + */ |
| 65 | + public function removeAllMessages(string $clientId): void |
| 66 | + { |
| 67 | + try { |
| 68 | + $this->cache->deleteItem($this->generateQueueKey($clientId)); |
| 69 | + } catch (InvalidArgumentException $e) { |
| 70 | + $this->logger?->error('Failed to remove messages from cache: '.$e->getMessage()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @inheritDoc |
| 76 | + */ |
| 77 | + public function receiveMessages(string $clientId): array |
| 78 | + { |
| 79 | + return $this->cache->getItem($this->generateQueueKey($clientId))->get() ?? []; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @inheritDoc |
| 84 | + */ |
| 85 | + public function popMessage(string $clientId): ?string |
| 86 | + { |
| 87 | + try { |
| 88 | + $cacheItem = $this->cache->getItem($this->generateQueueKey($clientId)); |
| 89 | + $messages = $cacheItem->get() ?? []; |
| 90 | + $message = array_shift($messages); |
| 91 | + $cacheItem->set($messages); |
| 92 | + $cacheItem->expiresAfter($this->messageTtl); |
| 93 | + $this->cache->save($cacheItem); |
| 94 | + |
| 95 | + return $message; |
| 96 | + } catch (InvalidArgumentException $e) { |
| 97 | + $this->logger?->error('Failed to pop message from cache: '.$e->getMessage()); |
| 98 | + |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @inheritDoc |
| 105 | + */ |
| 106 | + public function hasMessages(string $clientId): bool |
| 107 | + { |
| 108 | + return $this->cache->getItem($this->generateQueueKey($clientId))->isHit(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @inheritDoc |
| 113 | + */ |
| 114 | + public function getMessageCount(string $clientId): int |
| 115 | + { |
| 116 | + return count($this->cache->getItem($this->generateQueueKey($clientId))->get() ?? []); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @inheritDoc |
| 121 | + */ |
| 122 | + public function storeLastPongResponseTimestamp(string $clientId, ?int $timestamp = null): void |
| 123 | + { |
| 124 | + $cacheItem = $this->cache->getItem($this->generateQueueKey($clientId).'|last_pong'); |
| 125 | + $cacheItem->set($timestamp ?? time()); |
| 126 | + $cacheItem->expiresAfter($this->messageTtl); |
| 127 | + $this->cache->save($cacheItem); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @inheritDoc |
| 132 | + */ |
| 133 | + public function getLastPongResponseTimestamp(string $clientId): ?int |
| 134 | + { |
| 135 | + return $this->cache->getItem($this->generateQueueKey($clientId).'|last_pong')->get(); |
| 136 | + } |
| 137 | +} |
0 commit comments