Open MercatoDocsSearch docsAPI reference

Architecture

Cache CLI architecture

If an operator clears the cache for one organization, what else gets removed? Trace each CLI purge mode to the service that chooses the keys and tenant scope.

The cache CLI is intentionally implemented as a module CLI, not as a hard-coded command in the core CLI binary. That keeps cache operations aligned with Open Mercato's module architecture: the configs module owns the cache management feature in the UI, the HTTP API, and the CLI.

Discovery through the module generator

The command lives in packages/core/src/modules/configs/cli.ts. It becomes runnable only after the generator scans enabled modules and emits the CLI registry used by yarn mercato.

Flow:

  1. The configs module exports a cli.ts file with the cache subcommand.
  2. yarn generate scans enabled modules and collects every cli.ts.
  3. The generator emits apps/mercato/.mercato/generated/modules.cli.generated.ts.
  4. packages/cli loads that generated registry at runtime.
  5. yarn mercato configs cache ... resolves the configs module command and dispatches into the cache helper.

This design matters because:

For the full generator system, see Generator architecture.

Runtime architecture

From a cache command to a scoped cache backend
The CLI resolves the tenant scope before it inspects or purges entries.
Diagram source
flowchart TD
  cli["Cache CLI<br/>stats, purge and structural"] --> scope["Parse options and choose tenant scope"]
  scope --> helper["Normalize request<br/>Dry-run and JSON output"]
  helper --> cache["Resolve the scoped cache service"]
  cache --> stats["Inspect<br/>stats"]
  cache --> targets["Purge selection<br/>segment, tag, key, ID or pattern"]
  cache --> full["Wider purge<br/>all or structural entries"]
  stats --> store["Storage backend<br/>Memory, SQLite, Redis or JSON file"]
  targets --> store
  full --> store
    classDef focus fill:#e0f29c,stroke:#34402d,color:#172011,stroke-width:2px
    class helper focus

The cache command delegates into packages/core/src/modules/configs/lib/cache-cli.ts, which normalizes user input into one of a few request shapes.

Request types

Request typeHelper pathUnderlying cache primitive
statscollectCacheStats()collectCrudCacheStats()
purge --segmentexecuteCachePurge({ kind: 'segment' })purgeCrudCacheSegment()
purge --tagexecuteCachePurge({ kind: 'tags' })cache.deleteByTags()
purge --keyexecuteCachePurge({ kind: 'keys' })cache.delete() for each exact key
purge --idexecuteCachePurge({ kind: 'ids' })cache.keys('*token*') + cache.delete()
purge --patternexecuteCachePurge({ kind: 'pattern' })cache.keys(pattern) + cache.delete()
purge --allexecuteCachePurge({ kind: 'all' })cache.clear()
structuralrunStructuralCachePurge()purge --pattern 'nav:*' + the admin-nav and portal-nav CRUD segments

Scope handling

The helper does not talk to cache storage directly. It always resolves the tenant-aware cache service from DI and applies the selected scope through runWithCacheTenant(...).

Scope rules:

That is why structural --all-tenants is the safe default for page/module/sidebar changes: navigation cache can exist per user and per tenant, so a single-tenant purge is often insufficient during development.

Structural purge behavior

The structural shortcut is intentionally narrow. It targets nav:* keys and the cached admin/portal navigation API segments because those are the stale entries that typically survive:

It does not wipe unrelated CRUD caches unless you explicitly ask for --all, --segment, or another broader purge target.

Relationship to the admin cache page

The Backend → Configuration → Cache page and the CLI share the same ownership boundary but not the same surface area.

This split is deliberate:

Generate-time structural purge

yarn generate performs a best-effort, bootstrap-free structural invalidation after successful generation when the app enables configs. The automatic path scans the configured stock cache backend's tenant metadata once, removes the structural navigation entries across every stored scope, and refreshes generated artifacts for Turbopack. It does not load the generated CLI registry, initialize application DI, connect to the tenant database, or create request containers.

That post-step is intentionally non-fatal:

Source files

FileResponsibility
packages/core/src/modules/configs/cli.tsCLI command parsing, scope resolution, command entrypoints
packages/core/src/modules/configs/lib/cache-cli.tsRequest normalization, preview mode, purge execution
packages/core/src/modules/configs/api/cache/route.tsAdmin page HTTP API (stats, purgeAll, purgeSegment)
packages/shared/src/lib/crud/cache-stats.tsCRUD/widget segment analysis and segment purge
packages/cache/src/service.tsTenant-aware cache wrapper plus bootstrap-free cross-scope maintenance over memory/SQLite/Redis/jsonfile strategies
packages/cli/src/lib/post-generate-invalidation.tsLightweight automatic structural cache purge and generated-artifact refresh

For operator usage, see mercato configs cache.

Next: Configuration decision guide