Skip to content

Commit 818e46f

Browse files
authored
Fix LRU Cache removing files from file cache (#861)
* Fix LRU Cache removing files from file cache * Update tests to reflect changes
1 parent 9991ffb commit 818e46f

4 files changed

Lines changed: 9 additions & 43 deletions

File tree

‎blocks/loader.ts‎

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// deno-lint-ignore-file no-explicit-any
22
import JsonViewer from "../components/JsonViewer.tsx";
33
import { RequestContext } from "../deco.ts";
4-
import { ValueType, weakcache } from "../deps.ts";
4+
import { ValueType } from "../deps.ts";
55
import type { Block, BlockModule, InstanceOf } from "../engine/block.ts";
66
import { FieldResolver } from "../engine/core/resolver.ts";
77
import { singleFlight } from "../engine/core/utils.ts";
@@ -117,11 +117,6 @@ export const wrapCaughtErrors = async <
117117
}
118118
};
119119

120-
export const LOADER_CACHE_START_TRESHOLD =
121-
Deno.env.get("LOADER_CACHE_START_TRESHOLD") ?? 0;
122-
123-
export const LOADER_CACHE_SIZE = Deno.env.get("LOADER_CACHE_SIZE") ?? 1_024_000;
124-
125120
const stats = {
126121
cache: meter.createCounter("loader_cache", {
127122
unit: "1",
@@ -154,8 +149,6 @@ const inFuture = (maybeDate: string) => {
154149

155150
const noop = () => "";
156151

157-
let countCache = null as (weakcache.WeakLRUCache<any, any> | null);
158-
159152
/**
160153
* Wraps the loader written by the user by adding support for:
161154
* 1. Caching
@@ -229,28 +222,6 @@ const wrapLoader = (
229222
ctx.vary?.push(loader, cacheKeyValue);
230223
RequestContext?.signal?.throwIfAborted();
231224

232-
if (countCache === null) {
233-
countCache = new weakcache.WeakLRUCache({
234-
cacheSize: +LOADER_CACHE_SIZE,
235-
});
236-
}
237-
238-
const cc = countCache.getValue(cacheKeyValue) ?? { count: 0 };
239-
240-
if (cc.count === 0) {
241-
cc.count = 1;
242-
countCache.setValue(cacheKeyValue, cc);
243-
} else {
244-
cc.count += 1;
245-
}
246-
247-
if (cc.count < LOADER_CACHE_START_TRESHOLD) {
248-
status = "bypass";
249-
stats.cache.add(1, { status, loader });
250-
251-
return await handler(props, req, ctx);
252-
}
253-
254225
const cache = maybeCache;
255226

256227
const timing = ctx.monitoring?.timings.start("loader-hash");

‎runtime/caches/fileSystem.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,10 @@ function createFileSystemCache(): CacheStorage {
147147
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Cache/delete) */
148148
delete: async (
149149
request: RequestInfo | URL,
150-
options?: CacheQueryOptions,
150+
_options?: CacheQueryOptions,
151151
): Promise<boolean> => {
152-
assertNoOptions(options);
153-
154-
const deleteResponse = await deleteFile(
155-
await requestURLSHA1(request),
156-
);
152+
const cacheKey = await requestURLSHA1(request);
153+
const deleteResponse = await deleteFile(cacheKey);
157154
return deleteResponse;
158155
},
159156
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Cache/keys) */

‎runtime/caches/lrucache.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
createBaseCacheStorage,
77
} from "./utils.ts";
88

9+
// keep compatible with old variable name
910
const CACHE_MAX_SIZE = parseInt(
10-
Deno.env.get("CACHE_MAX_SIZE") ?? "1073824",
11+
Deno.env.get("CACHE_MAX_SIZE") ?? Deno.env.get("MAX_CACHE_SIZE") ??
12+
"1073741824",
1113
); // 1 GB max size of cache
1214
const CACHE_TTL_AUTOPURGE = Deno.env.get("CACHE_TTL_AUTOPURGE") !== "false"; // automatically delete expired items
1315
const CACHE_ALLOW_STALE = Deno.env.get("CACHE_ALLOW_STALE") !== "false"; // automatically allow stale
@@ -51,10 +53,6 @@ function createLruCacheStorage(cacheStorageInner: CacheStorage): CacheStorage {
5153
if (fileCache.has(cacheKey)) {
5254
fileCache.get(cacheKey);
5355
const result = cacheInner.match(cacheKey);
54-
if (!result) {
55-
// innercache miss
56-
fileCache.delete(cacheKey);
57-
}
5856
return result;
5957
}
6058
return undefined;

‎runtime/caches/mod.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert, assertEquals, assertNotEquals } from "@std/assert";
22
import { caches as lruCache } from "./lrucache.ts";
33
import { caches as headersCache } from "./headerscache.ts";
44

5-
const MAX_CACHE_SIZE = 1073824;
5+
const MAX_CACHE_SIZE = 1073741824;
66

77
const NOT_IMPLEMENTED = () => {
88
throw new Error("Not Implemented");
@@ -52,7 +52,7 @@ const baseTest = async (cacheStorageUT: CacheStorage) => {
5252
const cache = await headersCache(lruCache(cacheStorageUT)).open(CACHE_NAME);
5353
const response = () =>
5454
new Response("Hello, World!", {
55-
headers: { "Content-length": `${MAX_CACHE_SIZE / 2}` },
55+
headers: { "Content-length": `${MAX_CACHE_SIZE / 2 }` },
5656
});
5757
for (let i = 0; i < 5; i++) {
5858
const request = createRequest(i);

0 commit comments

Comments
 (0)