Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions packages/cache/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import type { Tool } from "ai";
import { createCacheBackend } from "./backends/factory";
import { LRUCacheStore } from "./cache-store";
import type { CachedTool, CacheOptions, CacheStats, CacheStore } from "./types";
import type { CachedTool, CacheOptions, CacheStats, CacheStore, CacheEntry } from "./types";

/**
* Helper function to set cache entry with TTL support
* Uses setWithTTL if available (Redis), otherwise falls back to regular set
*/
async function setCacheEntryWithTTL<T>(
store: CacheStore<T>,
key: string,
entry: CacheEntry<T>,
ttlMs: number,
): Promise<void> {
if (store.setWithTTL && ttlMs > 0) {
const ttlSeconds = Math.floor(ttlMs / 1000);
await store.setWithTTL(key, entry, ttlSeconds);
} else {
await store.set(key, entry);
}
}

/**
* Default cache key generator - stable and deterministic
Expand Down Expand Up @@ -237,11 +255,11 @@ function createStreamingCachedTool<T extends Tool>(
};

if (shouldCache(params, completeResult)) {
await cacheStore.set(key, {
await setCacheEntryWithTTL(cacheStore, key, {
result: completeResult,
timestamp: now,
key,
});
}, ttl);
if (debug) {
const cacheItems =
typeof cacheStore.size === "function"
Expand Down Expand Up @@ -491,11 +509,11 @@ export function cached<T extends Tool>(
};

if (shouldCache(params, completeResult)) {
await cacheStore.set(key, {
await setCacheEntryWithTTL(cacheStore, key, {
result: completeResult,
timestamp: now,
key,
});
}, effectiveTTL);
log(
`[Cache] STORED streaming result with ${capturedMessages.length} messages`,
);
Expand All @@ -508,11 +526,11 @@ export function cached<T extends Tool>(

// Regular tool
if (shouldCache(params, result)) {
await cacheStore.set(key, {
await setCacheEntryWithTTL(cacheStore, key, {
result,
timestamp: now,
key,
});
}, effectiveTTL);
log(`[Cache] STORED result`);
}

Expand Down Expand Up @@ -543,11 +561,11 @@ export function cached<T extends Tool>(
const result = await target.execute?.(params, executionOptions);

if (shouldCache(params, result)) {
await cacheStore.set(key, {
await setCacheEntryWithTTL(cacheStore, key, {
result,
timestamp: now,
key,
});
}, effectiveTTL);
log(`[Cache] STORED result`);
}

Expand Down Expand Up @@ -636,6 +654,7 @@ export function createCached(
});

return createCachedFunction(lruStore, {
ttl: options.ttl,
debug: options.debug || false,
cacheKey: options.cacheKey,
onHit: options.onHit,
Expand All @@ -654,6 +673,7 @@ export function createCached(
});

return createCachedFunction(redisStore, {
ttl: options.ttl,
debug: options.debug || false,
cacheKey: options.cacheKey,
onHit: options.onHit,
Expand Down
6 changes: 6 additions & 0 deletions packages/cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export { cached, createCached, cacheTools } from "./cache";
export type {
CacheOptions,
CachedTool,
CacheStore,
CacheEntry,
} from "./types";

export { createCacheBackend } from "./backends/factory";
export type { CacheBackendConfig } from "./backends/factory";
export { LRUCacheStore, SimpleCacheStore, RedisCacheStore, MemoryCacheStore } from "./backends/index";

// Re-export useful types from ai package
export type { Tool } from "ai";
3 changes: 3 additions & 0 deletions packages/cache/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export interface CacheStore<T = any> {
/** Set cache entry */
set(key: string, entry: CacheEntry<T>): void | Promise<void>;

/** Set cache entry with TTL (time-to-live) in seconds */
setWithTTL?(key: string, entry: CacheEntry<T>, ttlSeconds: number): void | Promise<void>;

/** Delete cache entry */
delete(key: string): boolean | Promise<boolean>;

Expand Down