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.
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:
configs module exports a cli.ts file with the cache subcommand.yarn generate scans enabled modules and collects every cli.ts.apps/mercato/.mercato/generated/modules.cli.generated.ts.packages/cli loads that generated registry at runtime.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.
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 type | Helper path | Underlying cache primitive |
|---|---|---|
stats | collectCacheStats() | collectCrudCacheStats() |
purge --segment | executeCachePurge({ kind: 'segment' }) | purgeCrudCacheSegment() |
purge --tag | executeCachePurge({ kind: 'tags' }) | cache.deleteByTags() |
purge --key | executeCachePurge({ kind: 'keys' }) | cache.delete() for each exact key |
purge --id | executeCachePurge({ kind: 'ids' }) | cache.keys('*token*') + cache.delete() |
purge --pattern | executeCachePurge({ kind: 'pattern' }) | cache.keys(pattern) + cache.delete() |
purge --all | executeCachePurge({ kind: 'all' }) | cache.clear() |
structural | runStructuralCachePurge() | purge --pattern 'nav:*' + the admin-nav and portal-nav CRUD segments |
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:
--tenant <id> runs inside one tenant scope.--global runs inside the global scope (tenantId = null).--all-tenants loads active tenants from the directory module and executes once for global plus once per tenant.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.
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.
The Backend → Configuration → Cache page and the CLI share the same ownership boundary but not the same surface area.
stats, purge all, and purge segment.tag, key, id, pattern, dry-run, JSON output, and multi-scope iteration.This split is deliberate:
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:
configsyarn mercato configs cache structural --all-tenants for the full DI-aware path, including custom cache-service overrides| File | Responsibility |
|---|---|
packages/core/src/modules/configs/cli.ts | CLI command parsing, scope resolution, command entrypoints |
packages/core/src/modules/configs/lib/cache-cli.ts | Request normalization, preview mode, purge execution |
packages/core/src/modules/configs/api/cache/route.ts | Admin page HTTP API (stats, purgeAll, purgeSegment) |
packages/shared/src/lib/crud/cache-stats.ts | CRUD/widget segment analysis and segment purge |
packages/cache/src/service.ts | Tenant-aware cache wrapper plus bootstrap-free cross-scope maintenance over memory/SQLite/Redis/jsonfile strategies |
packages/cli/src/lib/post-generate-invalidation.ts | Lightweight automatic structural cache purge and generated-artifact refresh |
For operator usage, see mercato configs cache.