Skip to content

Commit e462783

Browse files
committed
small cache / driver refactor
1 parent 1b4a029 commit e462783

3 files changed

Lines changed: 31 additions & 37 deletions

File tree

modules/snapshot/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const logger = useLogger('comark-docs')
1212
const ASSET_BASE = 'comark-content'
1313

1414
/**
15-
* Writes a build-time content snapshot into the function bundle, stamped with the commit it was
16-
* parsed at. A cold start at that commit hydrates from it instead of walking the content
17-
* repository; a cold start at a later commit reuses every unchanged body from it.
15+
* Writes a build-time snapshot into the function bundle stamped with the commit it was parsed at.
16+
* A cold start at that commit hydrates from it instead of walking the content repository.
17+
* At a later commit it still supplies every unchanged body.
1818
*/
1919
export default defineNuxtModule({
2020
meta: { name: 'comark-docs:snapshot' },
@@ -53,9 +53,8 @@ export default defineNuxtModule({
5353
return
5454
}
5555

56-
// Pinned to the content commit, so the artifact carries `ref`. At runtime an instance pinned
57-
// to the same commit uses it as its index; one pinned to a later commit walks that commit
58-
// for the index and still takes every body whose source text did not change.
56+
// `withRef` stamps the artifact with the commit.
57+
// At runtime, even with a different commit, we can reuse unchanged bodies.
5958
const content = createBuildContentInstance({ source: fs(contentPath) }).withRef(sha)
6059

6160
try {

server/api/revalidate.post.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ export default defineEventHandler(async (event) => {
8989

9090
// Refresh the content SHA
9191
const headSha = await resolveContentSha(branch, contentDir, { refresh: true })
92-
// A throwaway instance pinned to the new commit: the diff needs its index only. Its index lands
93-
// in the commit's cache namespace, which the prod swap and the warm below then reuse.
92+
93+
// Throwaway instance: the diff needs the index only.
94+
// It lands in the commit's cache namespace to be reused by the prod warm below.
9495
const fresh = contentAt(headSha)
9596
await fresh.init()
9697
const newItems = (await fresh.manifest()).items

server/utils/cache.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,44 @@ function cacheAvailable(): boolean {
1313
return !import.meta.dev && Boolean(process.env.VERCEL)
1414
}
1515

16+
/** Every namespace below degrades to per-process memory off Vercel if not available. */
17+
function runtimeCacheDriver(base: string, ttl: number): Driver {
18+
if (!cacheAvailable()) return memoryDriver()
19+
return vercelRuntimeCache({ base, ttl })
20+
}
21+
1622
/**
1723
* Bump when content parser/plugin configuration, relevant parser dependencies, or cached derived
1824
* data changes. Keeping this explicit lets unrelated deployments reuse immutable content artifacts.
1925
*/
2026
export const CONTENT_PARSER_VERSION = 'v3'
2127

2228
/**
23-
* The driver behind everything cached per parser version, under `content:<version>`:
24-
*
25-
* - without `sha`, comark's index, parsed bodies and artifacts for every commit. An instance pinned
26-
* with `content.withRef(sha)` adds its own `ref:<sha>:` prefix, so instances pinned to different
27-
* commits share this driver without reading each other's entries;
28-
* - with `sha`, ad-hoc per-commit data (commit history, RSS dates) under `content:<version>:<sha>`.
29-
* Those keys start with `gh:` and never meet comark's.
30-
*
31-
* Bumping the parser version leaves every commit's entries behind at once.
29+
* Comark cache: index, parsed bodies and artifacts of every commit.
30+
* Sharing content across all perser versions BUT keys are per-sha.
3231
*/
33-
export function contentCacheDriver(sha?: string): Driver {
34-
if (!cacheAvailable()) return memoryDriver()
35-
return vercelRuntimeCache({
36-
base: sha ? `content:${CONTENT_PARSER_VERSION}:${sha}` : `content:${CONTENT_PARSER_VERSION}`,
37-
ttl: TTL,
38-
})
39-
}
40-
41-
/** Ad-hoc per-SHA storage for non-content data (commit history, RSS dates). */
42-
export function shaCacheStorage(sha: string): Storage {
43-
return createStorage({ driver: contentCacheDriver(sha) })
32+
export function contentCacheDriver(): Driver {
33+
return runtimeCacheDriver(`content:${CONTENT_PARSER_VERSION}`, TTL)
4434
}
4535

4636
/**
4737
* Shared driver backing the branch + content directory → content commit pointer
4838
* (`resolveContentSha` in `github.ts`), in its own namespace so every instance reads one pointer
4939
* instead of keeping its own timer.
5040
*
51-
* Vercel Runtime Cache is **regional**, not global (https://vercel.com/docs/caching/runtime-cache):
52-
* this assumes Functions run in a single region (no `regions` in `vercel.json`/`nuxt.config.ts`).
53-
* Multi-region would confine the webhook's forced refresh to its region others self-heal on TTL,
54-
* so reach for a globally replicated store (e.g. Edge Config) only if that day comes.
41+
* TODO: Vercel Runtime Cache is **regional**, not global (https://vercel.com/docs/caching/runtime-cache):
42+
* It assumes Functions run in a single region.
43+
* Multi-region would confine the webhook's forced refresh to its region (others self-heal on TTL)
44+
* We should reach for a globally replicated store (e.g. Edge Config).
5545
*/
5646
export function refCacheDriver(): Driver {
57-
if (!cacheAvailable()) return memoryDriver()
58-
return vercelRuntimeCache({
59-
base: 'content:refs',
60-
ttl: REF_TTL,
61-
})
47+
return runtimeCacheDriver('content:refs', REF_TTL)
48+
}
49+
50+
/**
51+
* Per-commit data Comark knows nothing about (commit history, RSS dates).
52+
* Namespace is per-sha and keys start with `gh:`.
53+
*/
54+
export function shaCacheStorage(sha: string): Storage {
55+
return createStorage({ driver: runtimeCacheDriver(`content:${CONTENT_PARSER_VERSION}:${sha}`, TTL) })
6256
}

0 commit comments

Comments
 (0)