GhaylStackDocumentationv1.0.2
العربيةWebsite
Shared services

Shared services

Caching and invalidation

Understand local and distributed cache lifetimes, key namespaces and safe invalidation.

Understand the two layers

The Caching building block registers .NET HybridCache and wraps it with ObservableHybridCache for telemetry. Consumers inject HybridCache; the wrapper is transparent. The local process holds L1 entries, while configured Redis-compatible storage supplies L2. The development platform uses Valkey for that connection.

When CachingOptions.Redis is empty, registration falls back to distributed memory inside the process. That is useful for isolated development, but it is not a shared cache between API instances. The production API separately requires a configured Redis connection.

Set lifetimes with an explicit purpose

The option-class defaults are:

Option Default Meaning
DefaultExpiration 1 hour Overall entry lifetime
DefaultLocalCacheExpiration 2 minutes Lifetime of one process's copy
MaximumKeyLength 1024 Accepted key length
MaximumPayloadBytes 1 MiB Largest serialized cached value
KeyPrefix Empty Optional application/environment namespace

These are code defaults; the effective configuration may override them. There is no built-in L1 invalidation backplane in this registration. Removing an entry on one instance does not justify assuming every other process immediately discards its local copy. Choose a local lifetime consistent with the freshness the feature needs.

Build keys around ownership

CacheKeys defines framework conventions for permissions, tenant themes, default themes, idempotency and impersonation-grant status. Reuse the relevant convention rather than inventing a second key for the same data.

For new business data, include all factors that change the answer: tenant, resource identifier, user or permission scope where relevant, and query parameters. A tenant tag enables grouped invalidation, but a tag alone does not make a non-unique key safe. Never reuse a global key for tenant-filtered results.

CachingOptions.KeyPrefix separates applications and environments sharing the same server. It applies beyond ordinary cache entries, including Data Protection and SignalR channels. The prefix rejects whitespace and braces. Changing it in an existing installation starts a cold cache and moves the key-ring name; migrate the existing Data Protection key ring before switching so protected values remain readable.

Invalidate at the mutation boundary

After changing the source data, remove its entry or invalidate the appropriate tag. The framework exposes tags such as permissions, themes, tenant and user. Role permission synchronization invalidates the permission tag after changing built-in grants.

Do not use the Idempotency tag as a replay-cache purge: its source comment explicitly says replay entries use IDistributedCache, where these HybridCache tags do not apply. Keep replay retention and business-data caching separate.

Check behavior across instances

Test a first read, cached read, mutation and subsequent read. For a multi-instance deployment, read from another instance before and after its local lifetime expires. Verify two tenants cannot share results even when their record identifiers or query text are similar.

If stale data persists, inspect the exact key, prefix, tag and configured lifetime. If a cache appears to miss continuously, inspect serialized payload size and whether a new key is accidentally generated for every request. Use the emitted cache telemetry alongside the authoritative database result.

Implementation references

View MarkdownBack to top

Search documentation

Use ↑ ↓ to move, Enter to open and Esc to close.