| 8 | const REDIS_KEY_PREFIX = 'mcp:tools:' |
| 9 | |
| 10 | export class RedisMcpCache implements McpCacheStorageAdapter { |
| 11 | constructor(private redis: Redis) {} |
| 12 | |
| 13 | private getKey(key: string): string { |
| 14 | return `${REDIS_KEY_PREFIX}${key}` |
| 15 | } |
| 16 | |
| 17 | async get(key: string): Promise<McpCacheEntry | null> { |
| 18 | try { |
| 19 | const redisKey = this.getKey(key) |
| 20 | const data = await this.redis.get(redisKey) |
| 21 | |
| 22 | if (!data) { |
| 23 | return null |
| 24 | } |
| 25 | |
| 26 | try { |
| 27 | return JSON.parse(data) as McpCacheEntry |
| 28 | } catch { |
| 29 | // Corrupted data - delete and treat as miss |
| 30 | logger.warn('Corrupted cache entry, deleting:', redisKey) |
| 31 | await this.redis.del(redisKey) |
| 32 | return null |
| 33 | } |
| 34 | } catch (error) { |
| 35 | logger.error('Redis cache get error:', error) |
| 36 | throw error |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | async set(key: string, tools: McpTool[], ttlMs: number): Promise<void> { |
| 41 | try { |
| 42 | const now = Date.now() |
| 43 | const entry: McpCacheEntry = { |
| 44 | tools, |
| 45 | expiry: now + ttlMs, |
| 46 | } |
| 47 | |
| 48 | await this.redis.set(this.getKey(key), JSON.stringify(entry), 'PX', ttlMs) |
| 49 | } catch (error) { |
| 50 | logger.error('Redis cache set error:', error) |
| 51 | throw error |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | async delete(key: string): Promise<void> { |
| 56 | try { |
| 57 | await this.redis.del(this.getKey(key)) |
| 58 | } catch (error) { |
| 59 | logger.error('Redis cache delete error:', error) |
| 60 | throw error |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | async clear(): Promise<void> { |
| 65 | try { |
| 66 | let cursor = '0' |
| 67 | let deletedCount = 0 |
nothing calls this directly
no outgoing calls
no test coverage detected