01Install
Install from npm into your project.
BASH
pnpm add @lov3kaizen/agentsea-cache02Example
TS
import { SemanticCache, MemoryCacheStore, ExactMatchStrategy } from '@lov3kaizen/agentsea-cache';
const cache = new SemanticCache(
{ defaultTTL: 3600, matchStrategy: 'exact' },
new MemoryCacheStore({ type: 'memory', maxEntries: 10000 }),
new ExactMatchStrategy(),
);
const response = await cache.wrap(
{ model: 'gpt-5.5', messages: [{ role: 'user', content: 'What is the capital of France?' }] },
async (request) => await openai.chat.completions.create(request), // your LLM call
);
console.log('Cached:', response._cache?.hit);03Key API
SemanticCache.wrap(request, fn, options?)Returns a cached response if one matches, otherwise calls fn and stores the result.
SemanticCache.get / set / delete / clearReads and writes cache entries directly.
SemanticCache.invalidateByTags(tags)Removes entries that carry the given tags.
SemanticCache.getStats() / getAnalytics()Returns cache statistics and analytics.
MemoryCacheStore / RedisStoreConfig / TieredStoreConfigStorage backends for entries: in memory, Redis, or tiered.
04Caveats
- Semantic matching needs @lov3kaizen/agentsea-embeddings, installed as an optional peer.
- The Redis store needs a running Redis server. The Pinecone store needs an API key and an index.
- The default match strategy is hybrid. The first example sets exact explicitly.
- The package does not say where its pricing data comes from. Check savings figures against your own rates.
05Matching strategies
The strategy decides how a new request is matched against stored entries. ExactMatchStrategy uses a hash of the request. SemanticMatchStrategy compares embeddings and needs a SimilarityEngine. HybridMatchStrategy tries exact first, then semantic.
TS
import { SemanticCache, MemoryCacheStore, HybridMatchStrategy, SimilarityEngine } from '@lov3kaizen/agentsea-cache';
import { createEmbeddingManager, createOpenAIProvider } from '@lov3kaizen/agentsea-embeddings';
const embeddings = createEmbeddingManager({ defaultProvider: 'openai', defaultModel: 'text-embedding-3-small' });
embeddings.registerModel(
createOpenAIProvider({ type: 'openai', apiKey: process.env.OPENAI_API_KEY!, model: 'text-embedding-3-small' }),
true,
);
// SimilarityEngine takes an object with embed(text) returning the vector.
const similarity = new SimilarityEngine({
provider: { embed: async (text) => (await embeddings.embed(text)).vector },
cacheEmbeddings: true,
});
const cache = new SemanticCache(
{ defaultTTL: 3600, similarityThreshold: 0.92, matchStrategy: 'hybrid' },
new MemoryCacheStore({ type: 'memory' }),
new HybridMatchStrategy(),
similarity,
);When a semantic match is found, response._cache reports the hit and the similarity score.
06Wrap options
Each wrap call takes options that override the cache defaults for that call only.
ttlSeconds to keep this entry. Overrides defaultTTL.
tagsLabels for grouped invalidation with invalidateByTags.
namespaceStores the entry under a namespace.
skipCacheCalls the function and does not read or write the cache.
forceRefreshSkips the lookup, calls the function and stores the new response.
TS
const response = await cache.wrap(request, llmCall, { ttl: 1800, tags: ['support', 'faq'] });07Invalidation
TS
// Remove entries whose keys match a pattern
await cache.invalidateByPattern(/^user:123:/);
// Remove all entries tagged 'faq'
await cache.invalidateByTags(['faq']);
// Delete a single key, or clear everything
await cache.delete(key);
await cache.clear();invalidateByPattern takes a regular expression, not a string glob.