-
Notifications
You must be signed in to change notification settings - Fork 52
feat(cache): in-memory tier, size limits, sharding, observability #1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e6d5b28
8c4e5d1
9e3c534
7ba9241
0010914
23b5ce1
b3696ff
60d5f50
ad86a9b
7b2e290
9ed8346
464e1f0
093066e
ff09a48
84560bf
2c062bd
ac7edcd
7f6bbac
9f84eee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,45 @@ import { | |
| const FILE_SYSTEM_CACHE_DIRECTORY = | ||
| Deno.env.get("FILE_SYSTEM_CACHE_DIRECTORY") ?? "/tmp/deco_cache"; | ||
|
|
||
| const CACHE_MAX_ENTRY_SIZE = parseInt( | ||
| Deno.env.get("CACHE_MAX_ENTRY_SIZE") ?? "2097152", // 2 MB | ||
| ) || 2097152; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Using Prompt for AI agents |
||
|
|
||
| // Warn when write rate exceeds this many writes per minute. | ||
| // High write rates usually indicate bots, missing cache keys, or very short TTLs. | ||
| const CACHE_WRITE_RATE_WARN = parseInt( | ||
| Deno.env.get("CACHE_WRITE_RATE_WARN") ?? "500", | ||
| ) || 500; | ||
|
|
||
| // --- Write rate tracking --- | ||
| let writeCount = 0; | ||
| let writeWindowStart = Date.now(); | ||
|
|
||
| function trackWriteRate(key: string) { | ||
| const now = Date.now(); | ||
| if (now - writeWindowStart > 60_000) { | ||
| writeWindowStart = now; | ||
| writeCount = 0; | ||
| } | ||
| writeCount++; | ||
| if (writeCount === CACHE_WRITE_RATE_WARN) { | ||
| logger.warn( | ||
| `fs_cache: high write rate — ${writeCount} writes in the last minute. ` + | ||
| `Latest key: ${key}. ` + | ||
| `Consider increasing CACHE_MAX_AGE_S or reviewing loader cacheKey functions. ` + | ||
| `Adjust threshold with CACHE_WRITE_RATE_WARN (current: ${CACHE_WRITE_RATE_WARN}/min).`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const initializedShardDirs = new Set<string>(); | ||
|
|
||
| function shardedPath(cacheDir: string, key: string): string { | ||
| const l1 = key.substring(0, 2); | ||
| const l2 = key.substring(2, 4); | ||
| return `${cacheDir}/${l1}/${l2}/${key}`; | ||
| } | ||
|
|
||
| // Reuse TextEncoder instance to avoid repeated instantiation | ||
| const textEncoder = new TextEncoder(); | ||
|
|
||
|
|
@@ -106,7 +145,7 @@ function createFileSystemCache(): CacheStorage { | |
| if ( | ||
| FILE_SYSTEM_CACHE_DIRECTORY && !existsSync(FILE_SYSTEM_CACHE_DIRECTORY) | ||
| ) { | ||
| await Deno.mkdirSync(FILE_SYSTEM_CACHE_DIRECTORY, { recursive: true }); | ||
| await Deno.mkdir(FILE_SYSTEM_CACHE_DIRECTORY, { recursive: true }); | ||
| } | ||
| isCacheInitialized = true; | ||
| } catch (err) { | ||
|
|
@@ -118,11 +157,25 @@ function createFileSystemCache(): CacheStorage { | |
| key: string, | ||
| responseArray: Uint8Array, | ||
| ) { | ||
| if (responseArray.length > CACHE_MAX_ENTRY_SIZE) { | ||
| // Evict any existing entry so stale data doesn't stay pinned on disk. | ||
| deleteFile(key).catch(() => {}); | ||
| return; | ||
| } | ||
| if (!isCacheInitialized) { | ||
| await assertCacheDirectory(); | ||
| } | ||
| const filePath = `${FILE_SYSTEM_CACHE_DIRECTORY}/${key}`; | ||
|
|
||
| trackWriteRate(key); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Write-rate warnings are skipped whenever cache writes use Prompt for AI agents |
||
| const filePath = shardedPath(FILE_SYSTEM_CACHE_DIRECTORY, key); | ||
| const dir = filePath.substring(0, filePath.lastIndexOf("/")); | ||
| if (!initializedShardDirs.has(dir)) { | ||
| try { | ||
| await Deno.mkdir(dir, { recursive: true }); | ||
| initializedShardDirs.add(dir); | ||
| } catch { | ||
| // transient failure — don't mark initialized so next write retries mkdir | ||
| } | ||
| } | ||
| await Deno.writeFile(filePath, responseArray); | ||
| return; | ||
| } | ||
|
|
@@ -132,8 +185,12 @@ function createFileSystemCache(): CacheStorage { | |
| await assertCacheDirectory(); | ||
| } | ||
| try { | ||
| const filePath = `${FILE_SYSTEM_CACHE_DIRECTORY}/${key}`; | ||
| const filePath = shardedPath(FILE_SYSTEM_CACHE_DIRECTORY, key); | ||
| const fileContent = await Deno.readFile(filePath); | ||
| if (fileContent.length > CACHE_MAX_ENTRY_SIZE) { | ||
| Deno.remove(filePath).catch(() => {}); | ||
| return null; | ||
| } | ||
| return fileContent; | ||
| } catch (_err) { | ||
| const err = _err as { code?: string }; | ||
|
|
@@ -151,7 +208,7 @@ function createFileSystemCache(): CacheStorage { | |
| await assertCacheDirectory(); | ||
| } | ||
| try { | ||
| const filePath = `${FILE_SYSTEM_CACHE_DIRECTORY}/${key}`; | ||
| const filePath = shardedPath(FILE_SYSTEM_CACHE_DIRECTORY, key); | ||
| await Deno.remove(filePath); | ||
| return true; | ||
| } catch (err) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: The
|| 2097152fallback makes an explicitCACHE_MAX_ENTRY_SIZE=0impossible, because0is coerced to the default 2MB.Prompt for AI agents