Verified private cache
@Cacheable accepts an identity resolver directly. @Evict accepts its own resolver and discovers the cached routes in the same controller automatically. TTL is milliseconds. Guards, authentication, and applicable interceptors run before every lookup, including hits. Optional input schemas are validated after authorization and before cache hits. Validation failures skip the handler, circuit-breaker updates, and cache eviction.
import {Cacheable, Evict, Guard, type HttpCachePolicy} from '@heronjs/common';
import {Get, Post, Rest} from '@heronjs/express';
const identity: NonNullable<HttpCachePolicy['identity']> = ({principal}) => {
const {tenantId, subjectId, permissionRevision} = principal;
if (typeof tenantId !== 'string' || typeof subjectId !== 'string' || typeof permissionRevision !== 'string') return undefined;
return {tenantId, subjectId, authorization: permissionRevision};
};
const recordsGuard = Guard({access: 'records'});
@Rest('/records')
@recordsGuard
class RecordsController {
@Get({uri: '/'})
@Cacheable({ttl: 30000, identity, vary: ['accept-language']})
list() {
return [{status: 'available'}];
}
@Post({uri: '/refresh'})
@Evict({identity})
refresh() {
return {updated: true};
}
}Identity belongs to each decorator. There is no global HttpOptions.cache configuration, scope/private switch, or manual group. Reuse an identity function between cache and eviction decorators when they should address the same tenant/user partition. Each decorator can also supply its own resolver.
The configured gatekeeper must verify the bearer token and produce the principal before the resolver runs. Derive authorization from effective permissions or a permission revision, including every claim that changes the response. Do not use raw bearer tokens, unverified JWT decoding, or request-supplied user IDs as verified identity. A refreshed token with the same identity and permissions shares the entry; a permission change selects a different entry.
A decorator with identity requires a verified principal and nonempty string identity fields. Missing, malformed or failed resolution bypasses caching/eviction without falling back to anonymous entries. Guarded routes without an identity resolver also bypass caching/eviction. For anonymous routes, omit identity; requests carrying authorization, cookies or a principal cannot consume or evict anonymous entries.
The route namespace is automatically Base64(hjs::express:<uri>::<method>), for example Base64(hjs::express:/records/::GET). The URI is the normalized registered route template, including application/controller prefixes; the method is the declared HTTP method in uppercase. Automatically generated HEAD handling shares its GET route namespace, with separate response variants. Explicit HEAD routes have their own HEAD namespace. Only static route metadata is Base64-encoded. Tenant/user identity, authorization, path parameters, sorted query, configured variation headers, and optional requestKey(request) contribute to hashed keys. Repeated query value order is preserved. Authorization and cookie headers cannot be variation headers.
Only finite GET/HEAD results with a successful status are stored. False, null, zero, and empty strings are valid values. Streams, manual replies, sessions, final Set-Cookie headers, no-store, Vary: *, serialization errors, and unsuccessful statuses bypass writes. Dynamic route headers still execute on hits. Legacy value-based decorator keys remain available to non-HTTP consumers; HTTP namespaces ignore them and use requestKey for request-derived variation.
Successful mutation responses await eviction of every cached route declared on that controller, regardless of declaration order. Identity-based eviction is limited to the resolved tenant/user across their authorization variants. Other controllers, users, tenants and anonymous partitions remain unchanged. Anonymous controllers can use @Evict() to invalidate their anonymous caches. Generation changes prevent an earlier in-flight read from restoring the current cache generation. Every target invalidation is attempted and awaited even if another fails. Backend failures fail open; failed invalidation can leave data until TTL expiry. Route/identity generation records are retained while response entries expire. This cache is not transactional consistency or a substitute for authorization.