@vizamodo/edge-cache-core 0.3.26
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/runtime/cache.d.ts +6 -0
- package/dist/runtime/cache.js +22 -0
- package/dist/runtime/edge-cache.d.ts +8 -0
- package/dist/runtime/edge-cache.js +46 -0
- package/package.json +27 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./runtime/cache";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./runtime/cache";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getEdgeCache, setEdgeCache } from "./edge-cache";
|
|
2
|
+
const memory = new Map();
|
|
3
|
+
export async function getCachedOrFetch(key, fetcher, options) {
|
|
4
|
+
// L1: memory (skip if forceRefresh)
|
|
5
|
+
if (!options?.forceRefresh && memory.has(key)) {
|
|
6
|
+
return memory.get(key);
|
|
7
|
+
}
|
|
8
|
+
// L2: edge cache (skip if forceRefresh)
|
|
9
|
+
if (!options?.forceRefresh) {
|
|
10
|
+
const edge = await getEdgeCache(key);
|
|
11
|
+
if (edge) {
|
|
12
|
+
memory.set(key, edge);
|
|
13
|
+
return edge;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// L3: fetch
|
|
17
|
+
const value = await fetcher();
|
|
18
|
+
// write back
|
|
19
|
+
memory.set(key, value);
|
|
20
|
+
await setEdgeCache(key, value, options?.ttlSec ?? 3000);
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const CACHE_KEY_PREFIX = "https://edge-cache.internal/";
|
|
2
|
+
/**
|
|
3
|
+
* Generic edge cache GET helper
|
|
4
|
+
*/
|
|
5
|
+
export async function getEdgeCache(key) {
|
|
6
|
+
try {
|
|
7
|
+
const cache = caches.default;
|
|
8
|
+
const req = new Request(CACHE_KEY_PREFIX + key);
|
|
9
|
+
const res = await cache.match(req);
|
|
10
|
+
if (!res)
|
|
11
|
+
return null;
|
|
12
|
+
try {
|
|
13
|
+
return (await res.json());
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// corrupted cache entry → ignore
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// cache API failure should not break runtime
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generic edge cache SET helper
|
|
27
|
+
*/
|
|
28
|
+
export async function setEdgeCache(key, value, ttlSec) {
|
|
29
|
+
if (ttlSec <= 0)
|
|
30
|
+
return;
|
|
31
|
+
const cache = caches.default;
|
|
32
|
+
const req = new Request(CACHE_KEY_PREFIX + key);
|
|
33
|
+
const body = JSON.stringify(value);
|
|
34
|
+
const res = new Response(body, {
|
|
35
|
+
headers: {
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
"Cache-Control": `max-age=${ttlSec}`,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
try {
|
|
41
|
+
await cache.put(req, res);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// cache write failure is non-fatal
|
|
45
|
+
}
|
|
46
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vizamodo/edge-cache-core",
|
|
3
|
+
"version": "0.3.26",
|
|
4
|
+
"description": "Edge cache primitives for Cloudflare Workers (L1 memory + L2 edge cache)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.json",
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"clean": "rm -rf dist",
|
|
18
|
+
"prepublishOnly": "npm run build",
|
|
19
|
+
"release:prod": "rm -rf dist && npx npm-check-updates -u && npm install && git add package.json package-lock.json && git commit -m 'chore(deps): auto update dependencies before release' || echo 'No changes' && node versioning.js && npm publish --tag latest --access public && git push",
|
|
20
|
+
"release:full": "rm -rf dist && npx npm-check-updates -u && npm install && git add package.json package-lock.json && git commit -m 'chore(deps): auto update dependencies before release' || echo 'No changes' && node versioning.js && npm login && npm publish --tag latest --access public && git push"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@vitest/coverage-v8": "^4.1.0",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vitest": "^4.1.0"
|
|
26
|
+
}
|
|
27
|
+
}
|