claude-code-best/packages/mcp-client/src/cache.ts
James Feng 39ba9a56fd feat: pull in Vite build system + complete packages/builtin-tools/
- Add Vite as alternative build (vite.config.ts, scripts/vite-plugin-*)
- Add dab04af7 RSS fix (distRoot, ripgrep path, post-build)
- Pull in packages/builtin-tools/ (+354 files)
- Pull in packages/agent-tools/, mcp-client/, acp-link/, weixin/
- Copy defines.ts from upstream for Vite compatibility
- Update package.json with Vite scripts and deps
2026-06-01 18:18:03 +08:00

59 lines
1.5 KiB
TypeScript

// LRU memoization cache for MCP tool discovery
// Adapted from src/utils/memoize.ts — only memoizeWithLRU needed
import { LRUCache } from 'lru-cache'
type LRUMemoizedFunction<Args extends unknown[], Result> = {
(...args: Args): Result
cache: {
clear: () => void
size: () => number
delete: (key: string) => boolean
get: (key: string) => Result | undefined
has: (key: string) => boolean
}
}
/**
* Creates a memoized function with LRU eviction policy.
* Prevents unbounded memory growth by evicting least recently used entries.
*
* @param f The function to memoize
* @param cacheFn Key generation function
* @param maxCacheSize Maximum cache entries (default 100)
*/
export function memoizeWithLRU<
Args extends unknown[],
Result extends NonNullable<unknown>,
>(
f: (...args: Args) => Result,
cacheFn: (...args: Args) => string,
maxCacheSize: number = 100,
): LRUMemoizedFunction<Args, Result> {
const cache = new LRUCache<string, Result>({
max: maxCacheSize,
})
const memoized = (...args: Args): Result => {
const key = cacheFn(...args)
const cached = cache.get(key)
if (cached !== undefined) {
return cached
}
const result = f(...args)
cache.set(key, result)
return result
}
memoized.cache = {
clear: () => cache.clear(),
size: () => cache.size,
delete: (key: string) => cache.delete(key),
get: (key: string) => cache.peek(key),
has: (key: string) => cache.has(key),
}
return memoized
}