@vizamodo/edge-cache-core 0.3.32 → 0.3.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/runtime/cache.js +0 -11
- package/dist/runtime/edge-cache.js +36 -6
- package/package.json +1 -1
package/dist/runtime/cache.js
CHANGED
|
@@ -21,27 +21,18 @@ export async function getCachedOrFetch(key, fetcher, options) {
|
|
|
21
21
|
// Use DedupePromiseCache as L1. On forceRefresh we bypass by deleting first.
|
|
22
22
|
if (forceRefresh)
|
|
23
23
|
l1.delete(key);
|
|
24
|
-
if (forceRefresh) {
|
|
25
|
-
console.debug("[cache] forceRefresh → bypass L1");
|
|
26
|
-
}
|
|
27
24
|
return l1.getOrCreate(key, async () => {
|
|
28
|
-
console.debug("[cache] L1 MISS → entering factory", { key });
|
|
29
25
|
// ── L2: edge cache (skip on forceRefresh) ─────────────────────────────
|
|
30
26
|
if (!forceRefresh) {
|
|
31
|
-
console.debug("[cache] checking L2 (edge cache)", { key });
|
|
32
27
|
const edgeValue = await getEdgeCache(key).catch(() => null);
|
|
33
28
|
if (edgeValue !== null && edgeValue !== undefined) {
|
|
34
|
-
console.debug("[cache] L2 HIT", { key });
|
|
35
29
|
// Populate L1 for next request; edge TTL is not observable,
|
|
36
30
|
// so use caller-supplied ttlSec as an upper bound (may slightly over-cache).
|
|
37
31
|
return { value: edgeValue, ttlMs: ttlSec * 1000 };
|
|
38
32
|
}
|
|
39
33
|
}
|
|
40
|
-
console.debug("[cache] L2 MISS → calling fetcher", { key });
|
|
41
34
|
// ── L3: fetcher ───────────────────────────────────────────────────────
|
|
42
|
-
console.debug("[cache] L3 FETCH start", { key });
|
|
43
35
|
const raw = await fetcher();
|
|
44
|
-
console.debug("[cache] L3 FETCH done", { key });
|
|
45
36
|
let unwrapped;
|
|
46
37
|
let edgeTtlSec = ttlSec;
|
|
47
38
|
if (isWrapped(raw)) {
|
|
@@ -63,12 +54,10 @@ export async function getCachedOrFetch(key, fetcher, options) {
|
|
|
63
54
|
}
|
|
64
55
|
// Write to edge cache (fire-and-forget — failure must not affect return value).
|
|
65
56
|
if (edgeTtlSec > 0) {
|
|
66
|
-
console.debug("[cache] writing to L2", { key, ttl: edgeTtlSec });
|
|
67
57
|
setEdgeCache(key, unwrapped, edgeTtlSec).catch(() => { });
|
|
68
58
|
}
|
|
69
59
|
// ttlMs for L1 = same window as edge TTL so both layers expire together.
|
|
70
60
|
const l1TtlMs = edgeTtlSec * 1000;
|
|
71
|
-
console.debug("[cache] writing to L1", { key, ttlMs: l1TtlMs });
|
|
72
61
|
return { value: unwrapped, ttlMs: l1TtlMs };
|
|
73
62
|
});
|
|
74
63
|
}
|
|
@@ -11,23 +11,49 @@ export async function getEdgeCache(key) {
|
|
|
11
11
|
const req = new Request(CACHE_KEY_PREFIX + normalizeKey(key));
|
|
12
12
|
console.debug("[edge-cache] GET", { key, url: req.url });
|
|
13
13
|
const res = await cache.match(req);
|
|
14
|
+
console.debug("[edge-cache] MATCH RESULT", {
|
|
15
|
+
key,
|
|
16
|
+
hasResponse: !!res,
|
|
17
|
+
status: res?.status,
|
|
18
|
+
headers: res ? Object.fromEntries(res.headers.entries()) : null,
|
|
19
|
+
});
|
|
14
20
|
if (!res) {
|
|
15
21
|
console.debug("[edge-cache] MISS", { key });
|
|
16
22
|
return null;
|
|
17
23
|
}
|
|
18
|
-
console.debug("[edge-cache] HIT", {
|
|
24
|
+
console.debug("[edge-cache] HIT", {
|
|
25
|
+
key,
|
|
26
|
+
status: res.status,
|
|
27
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
28
|
+
});
|
|
19
29
|
try {
|
|
20
30
|
const data = await res.clone().text();
|
|
21
|
-
console.debug("[edge-cache] HIT RAW", {
|
|
22
|
-
|
|
31
|
+
console.debug("[edge-cache] HIT RAW", {
|
|
32
|
+
key,
|
|
33
|
+
size: data.length,
|
|
34
|
+
preview: data.slice(0, 200), // avoid huge logs
|
|
35
|
+
});
|
|
36
|
+
let parsed;
|
|
37
|
+
try {
|
|
38
|
+
parsed = JSON.parse(data);
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
console.error("[edge-cache] JSON PARSE FAILED", { key, err, dataPreview: data.slice(0, 200) });
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
console.debug("[edge-cache] PARSED OK", {
|
|
45
|
+
key,
|
|
46
|
+
type: typeof parsed,
|
|
47
|
+
});
|
|
48
|
+
return parsed;
|
|
23
49
|
}
|
|
24
50
|
catch {
|
|
25
51
|
// corrupted cache entry → ignore
|
|
26
52
|
return null;
|
|
27
53
|
}
|
|
28
54
|
}
|
|
29
|
-
catch {
|
|
30
|
-
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.error("[edge-cache] GET FAILED", { key, err });
|
|
31
57
|
return null;
|
|
32
58
|
}
|
|
33
59
|
}
|
|
@@ -49,8 +75,12 @@ export async function setEdgeCache(key, value, ttlSec) {
|
|
|
49
75
|
});
|
|
50
76
|
try {
|
|
51
77
|
console.debug("[edge-cache] SET BODY SIZE", { key, size: body.length });
|
|
78
|
+
const t0 = Date.now();
|
|
52
79
|
await cache.put(req, res);
|
|
53
|
-
|
|
80
|
+
// Force event loop flush to ensure write completes before worker exits
|
|
81
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
82
|
+
const t1 = Date.now();
|
|
83
|
+
console.debug("[edge-cache] SET OK", { key, durationMs: t1 - t0 });
|
|
54
84
|
}
|
|
55
85
|
catch (err) {
|
|
56
86
|
console.error("[edge-cache] SET FAILED", { key, err });
|