-
Notifications
You must be signed in to change notification settings - Fork 52
feat(cache): add CACHE_MIN_FREQUENCY to gate LRU admission #1121
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
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,11 @@ const CACHE_TTL_RESOLUTION = parseInt( | |||||||||||||||||||||||||||||
| const STALE_TTL_PERIOD = parseInt( | ||||||||||||||||||||||||||||||
| Deno.env.get("STALE_TTL_PERIOD") ?? "30000", | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| // Minimum number of times a request must be made before it is admitted into the LRU cache. | ||||||||||||||||||||||||||||||
| // Protects against one-off/cold requests polluting the cache. Default is 3. | ||||||||||||||||||||||||||||||
| const CACHE_MIN_FREQUENCY = parseInt( | ||||||||||||||||||||||||||||||
| Deno.env.get("CACHE_MIN_FREQUENCY") ?? "3", | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const cacheOptions = (cache: Cache) => ( | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
|
|
@@ -41,6 +46,11 @@ function createLruCacheStorage(cacheStorageInner: CacheStorage): CacheStorage { | |||||||||||||||||||||||||||||
| cacheStorageInner, | ||||||||||||||||||||||||||||||
| (_cacheName, cacheInner, requestURLSHA1) => { | ||||||||||||||||||||||||||||||
| const fileCache = new LRUCache(cacheOptions(cacheInner)); | ||||||||||||||||||||||||||||||
| // Tracks how many times each key has been put before admission into the LRU. | ||||||||||||||||||||||||||||||
| // Bounded to avoid unbounded memory growth from unique one-off keys. | ||||||||||||||||||||||||||||||
| const frequency = new LRUCache<string, number>({ | ||||||||||||||||||||||||||||||
| max: CACHE_MAX_ITEMS * 4, | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+53
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. Clear warm-up counters on explicit
Proposed fix delete: async (
request: RequestInfo | URL,
options?: CacheQueryOptions,
): Promise<boolean> => {
const cacheKey = await requestURLSHA1(request);
cacheInner.delete(cacheKey, options);
+ frequency.delete(cacheKey);
return fileCache.delete(cacheKey);
},📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| return Promise.resolve({ | ||||||||||||||||||||||||||||||
| ...baseCache, | ||||||||||||||||||||||||||||||
| delete: async ( | ||||||||||||||||||||||||||||||
|
|
@@ -89,6 +99,16 @@ function createLruCacheStorage(cacheStorageInner: CacheStorage): CacheStorage { | |||||||||||||||||||||||||||||
| if (!length || length == "0") { | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (CACHE_MIN_FREQUENCY > 1 && !fileCache.has(cacheKey)) { | ||||||||||||||||||||||||||||||
| const count = (frequency.get(cacheKey) ?? 0) + 1; | ||||||||||||||||||||||||||||||
| if (count < CACHE_MIN_FREQUENCY) { | ||||||||||||||||||||||||||||||
| frequency.set(cacheKey, count); | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| frequency.delete(cacheKey); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| fileCache.set(cacheKey, true, { | ||||||||||||||||||||||||||||||
| size: parseInt(length), | ||||||||||||||||||||||||||||||
| ttl, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
Default
CACHE_MIN_FREQUENCYof3is a behavior-breaking default.This changes cache admission from first-hit to third-hit globally. If backward compatibility is expected, default should remain
1and higher values should be opt-in.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents