lru-cache TTL eviction and size limits: working versions (tested 2026-10-15)
Cache computed values with lru-cache, covering max entries, TTL expiry via fake timers, and getOrSet style populate helpers offline.
passed3 passed · 0 failed2026-10-15 16:01 UTC- Status
- passed
- Last passed
- 2026-10-15 16:01 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
lru-cache@11.5.3@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("lru-cache-ttl") (install), or see them on GitHub.
package.json
{
"name": "lru-cache-ttl",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {"lru-cache":"11.5.3"},
"devDependencies": {"@types/node":"26.6.5","tsx":"4.23.15"}
}
src/cache.ts
import { LRUCache } from "lru-cache";
export type User = { id: string; name: string };
export function createUserCache(max = 100, ttlMs = 60_000) {
const cache = new LRUCache<string, User>({
max,
ttl: ttlMs,
// Use Date so node:test fake timers control expiry
perf: { now: () => Date.now() },
});
return {
get(id: string) {
return cache.get(id);
},
set(user: User) {
cache.set(user.id, user);
return user;
},
async getOrFetch(id: string, fetch: (id: string) => Promise<User>) {
const hit = cache.get(id);
if (hit) return hit;
const user = await fetch(id);
cache.set(id, user);
return user;
},
size() {
return cache.size;
},
clear() {
cache.clear();
},
};
}
test/cache.test.ts
import { test, mock } from "node:test";
import assert from "node:assert/strict";
import { createUserCache } from "../src/cache.ts";
test("evicts least recently used when full", () => {
const cache = createUserCache(2, 60_000);
cache.set({ id: "a", name: "Ada" });
cache.set({ id: "b", name: "Bob" });
cache.get("a");
cache.set({ id: "c", name: "Cid" });
assert.equal(cache.get("b"), undefined);
assert.equal(cache.get("a")?.name, "Ada");
assert.equal(cache.size(), 2);
});
test("expires entries after TTL with fake timers", async () => {
mock.timers.enable({ apis: ["Date", "setTimeout"], now: 1_000 });
const cache = createUserCache(10, 5_000);
cache.set({ id: "a", name: "Ada" });
assert.equal(cache.get("a")?.name, "Ada");
mock.timers.tick(6_000);
assert.equal(cache.get("a"), undefined);
mock.timers.reset();
});
test("getOrFetch populates once", async () => {
const cache = createUserCache();
let calls = 0;
const fetch = async (id: string) => {
calls++;
return { id, name: "Ada" };
};
assert.equal((await cache.getOrFetch("a", fetch)).name, "Ada");
assert.equal((await cache.getOrFetch("a", fetch)).name, "Ada");
assert.equal(calls, 1);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.