Serverless Context
Serverless context
Section titled “Serverless context”memocache uses a context object to run non-blocking cache work such as background writes, backfills, and stale revalidation.
If you do not provide one, memocache creates a default stateful context internally. In serverless environments, you may want to pass a platform-specific waitUntil() implementation instead.
Context shape
Section titled “Context shape”export interface Context { waitUntil: (p: Promise<unknown>) => void}Example with Vercel
Section titled “Example with Vercel”import { createCache } from '@alexmchan/memocache'import { waitUntil } from '@vercel/functions'
const cache = createCache({ context: { waitUntil, [Symbol.asyncDispose]() { // optional cleanup }, },})This lets cache maintenance continue after the response has been sent, subject to the platform’s runtime limits.
Example of a simple flushable context
Section titled “Example of a simple flushable context”function createSimpleContext() { const waitables: Promise<unknown>[] = []
return { waitUntil(p: Promise<unknown>) { waitables.push(p) }, async flushCache() { await Promise.allSettled(waitables) waitables.length = 0 }, async [Symbol.asyncDispose]() { await this.flushCache() }, }}This pattern is useful when you need explicit control over when pending cache writes finish.