|
| 1 | +# Caching |
| 2 | + |
| 3 | +`@mcp-framework/docs` includes a built-in caching layer that reduces HTTP requests to your documentation site. All source adapters use caching automatically. |
| 4 | + |
| 5 | +## Default Cache |
| 6 | + |
| 7 | +The default `MemoryCache` is an in-memory LRU (Least Recently Used) cache with TTL (Time-To-Live) expiry. |
| 8 | + |
| 9 | +### What Gets Cached |
| 10 | + |
| 11 | +| Content | Cache Key | Default TTL | |
| 12 | +|---------|-----------|-------------| |
| 13 | +| `llms.txt` content | `index:{baseUrl}` | `refreshInterval` | |
| 14 | +| `llms-full.txt` content | `full:{baseUrl}` | `refreshInterval` | |
| 15 | +| Individual page content | `page:{slug}` | `refreshInterval` | |
| 16 | +| Search results | `search:{query}:{section}:{limit}` | `refreshInterval` | |
| 17 | +| Parsed section tree | `sections:{baseUrl}` | `refreshInterval` | |
| 18 | + |
| 19 | +### Configuration |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { MemoryCache, LlmsTxtSource } from "@mcp-framework/docs"; |
| 23 | + |
| 24 | +const cache = new MemoryCache({ |
| 25 | + maxEntries: 200, // Default: 100 |
| 26 | + ttlMs: 600_000, // Default: 300_000 (5 minutes) |
| 27 | +}); |
| 28 | + |
| 29 | +const source = new LlmsTxtSource({ |
| 30 | + baseUrl: "https://docs.example.com", |
| 31 | + cache, |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +The `refreshInterval` option on source adapters sets the TTL for the default cache. If you provide a custom cache, the `ttlMs` on the cache takes precedence. |
| 36 | + |
| 37 | +## Cache Behavior |
| 38 | + |
| 39 | +- **Lazy expiry** — Expired entries are cleaned on access, not via a background timer. This avoids interval leaks. |
| 40 | +- **LRU eviction** — When `maxEntries` is reached, the oldest entry is evicted to make room for new ones. |
| 41 | +- **Per-entry TTL** — Each `set()` call can override the default TTL. |
| 42 | +- **Overwrite resets TTL** — Storing the same key again resets the expiry timer. |
| 43 | + |
| 44 | +## Cache Interface |
| 45 | + |
| 46 | +Implement this interface for custom backends (Redis, SQLite, etc.): |
| 47 | + |
| 48 | +```typescript |
| 49 | +interface Cache { |
| 50 | + get<T>(key: string): Promise<T | null>; |
| 51 | + set<T>(key: string, value: T, ttlMs?: number): Promise<void>; |
| 52 | + delete(key: string): Promise<void>; |
| 53 | + clear(): Promise<void>; |
| 54 | + stats(): { hits: number; misses: number; size: number }; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Example: Redis Cache |
| 59 | + |
| 60 | +```typescript |
| 61 | +import { Cache, CacheStats } from "@mcp-framework/docs"; |
| 62 | +import { createClient } from "redis"; |
| 63 | + |
| 64 | +class RedisCache implements Cache { |
| 65 | + private client; |
| 66 | + private defaultTtl: number; |
| 67 | + private _hits = 0; |
| 68 | + private _misses = 0; |
| 69 | + |
| 70 | + constructor(redisUrl: string, ttlMs = 300_000) { |
| 71 | + this.client = createClient({ url: redisUrl }); |
| 72 | + this.defaultTtl = ttlMs; |
| 73 | + } |
| 74 | + |
| 75 | + async get<T>(key: string): Promise<T | null> { |
| 76 | + const value = await this.client.get(`docs:${key}`); |
| 77 | + if (!value) { |
| 78 | + this._misses++; |
| 79 | + return null; |
| 80 | + } |
| 81 | + this._hits++; |
| 82 | + return JSON.parse(value); |
| 83 | + } |
| 84 | + |
| 85 | + async set<T>(key: string, value: T, ttlMs?: number): Promise<void> { |
| 86 | + const ttl = Math.ceil((ttlMs ?? this.defaultTtl) / 1000); |
| 87 | + await this.client.set(`docs:${key}`, JSON.stringify(value), { EX: ttl }); |
| 88 | + } |
| 89 | + |
| 90 | + async delete(key: string): Promise<void> { |
| 91 | + await this.client.del(`docs:${key}`); |
| 92 | + } |
| 93 | + |
| 94 | + async clear(): Promise<void> { |
| 95 | + // Careful: only clear docs: keys |
| 96 | + const keys = await this.client.keys("docs:*"); |
| 97 | + if (keys.length > 0) await this.client.del(keys); |
| 98 | + this._hits = 0; |
| 99 | + this._misses = 0; |
| 100 | + } |
| 101 | + |
| 102 | + stats(): CacheStats { |
| 103 | + return { hits: this._hits, misses: this._misses, size: -1 }; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Cache Invalidation |
| 109 | + |
| 110 | +In v1, cache invalidation is **TTL-based only**. When the TTL expires, the next request triggers a fresh fetch. There is no webhook-based or push-based invalidation. |
| 111 | + |
| 112 | +For near-real-time updates, set a shorter `refreshInterval`: |
| 113 | + |
| 114 | +```typescript |
| 115 | +const source = new FumadocsRemoteSource({ |
| 116 | + baseUrl: "https://docs.example.com", |
| 117 | + refreshInterval: 60_000, // Re-fetch every minute |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +For less frequently changing docs, increase it: |
| 122 | + |
| 123 | +```typescript |
| 124 | +const source = new FumadocsRemoteSource({ |
| 125 | + baseUrl: "https://docs.example.com", |
| 126 | + refreshInterval: 3_600_000, // Re-fetch every hour |
| 127 | +}); |
| 128 | +``` |
0 commit comments