@setu-ts/cache-plugin — response and value caching
Add it: setu add cache adds it and inserts CachePlugin() (in-memory store) into
setu.config.ts. It provides CAPABILITIES.CACHE (ICacheStore) and a health indicator.
Cache a route’s response
Put cacheMiddleware on the route, in the module’s routes file:
import type { IRouterApi } from '@setu-ts/common';
import { cacheMiddleware } from '@setu-ts/cache-plugin';
export function registerProductRoutes(router: IRouterApi): void {
router.get('/products', {
middleware: [cacheMiddleware({ ttlSeconds: 60 })],
handler: (ctx) => ctx.response.json([{ id: 'p1', name: 'Pen' }]),
});
}
Cache a value
import { CAPABILITIES } from '@setu-ts/common';
import type { ICacheStore } from '@setu-ts/common';
const cache = ctx.services.get<ICacheStore>(CAPABILITIES.CACHE);
await cache.set('rates', { eur: 1.1 }, 300);
const rates = await cache.get<{ eur: number }>('rates');
Behaviour the types do not show
- The middleware sets
X-Cacheitself:MISSwhen the handler ran,HITwhen replayed,COALESCEDwhen a concurrent miss shared one handler run. Do not set it in the handler. - Only status
200is stored by default (cacheableStatuseschanges that); streamed responses and responses setting a cookie are never stored. The key is the method and URL, plus the tenant and locale when those are resolved. getreturnsnullfor an absent or expired key, neverundefined.ttlSecondsis seconds; omitted, the store’sdefaultTtlapplies.CAPABILITIES.CACHEresolves only afterapp.start(); inside a handlerctx.servicesis ready.
Other options
CachePlugin({ store: 'redis', options: { url } })uses Redis (installioredis);'noop'disables caching.options:prefix,defaultTtl,maxSize,commandTimeoutMs.cacheMiddlewarealso takeskey(ctx),vary(ctx),bypass(ctx)andstore(a named instance:CachePlugin({ name: 'pages' })registerscache.pages).CacheService.getOrSet(key, factory, ttl)coalesces concurrent cold reads of one key.