Skip to content

NestJS: support multiple named caches (forFeature) #61

Description

@camcima

Deferred from the architecture review (architectural observation #2) — see ARCHITECTURE_REVIEW.md.

Problem

ZigguratModule is single-cache by construction: one global module, one CACHE_MANAGER token, one CacheManager. Both forRoot and forRootAsync register global: true unconditionally (packages/nestjs/src/ziggurat.module.ts).

Real applications usually want several caches with different shapes — users in memory+Redis with a 30s L1, reports in SQLite with an hour TTL, sessions somewhere else entirely. Today that means hand-rolling providers and tokens per cache and bypassing the module, which is the boilerplate this package exists to remove.

The core supports it fine: CacheManager already takes a namespace and its own layer stack. It is only the Nest binding that assumes one.

Sketch

  • ZigguratModule.forFeature(name, options) returning a scoped provider under a derived token.
  • An @InjectCache(name) decorator, or an exported getCacheToken(name) for manual @Inject.
  • @Cached({ cache: name, ... }) so the decorator can target a named cache; it currently hardcodes CACHE_MANAGER (packages/nestjs/src/cached.decorator.ts).
  • forRoot/forRootAsync keep working unchanged as the default/unnamed cache.

Worth deciding as part of this: whether global: true should stay unconditional. A forFeature cache scoped to the module that declared it is the more Nest-idiomatic default, and a global escape hatch can be opt-in.

Done when

An app can register two independently configured caches, inject either one by name, and point @Cached() at a specific one — without writing custom providers.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions