Skip to content

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.

export interface Context {
waitUntil: (p: Promise<unknown>) => void
}
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.

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.