use-hybrid-caching
Hybrid Caching (ABP)
Guidance for ABP's typed hybrid cache: a fast in-memory L1 tier backed by a distributed L2 tier, with stampede protection. All APIs below are verified against ABP source. Inject everything via constructor.
When to Use
- Cache a typed value that is read very frequently and benefits from a local in-memory hit (L1) while sharing a distributed tier (L2) across instances. Note the L1/L2 coherence limit: a value cached in L1 on one instance is not actively invalidated on other instances — each instance's local copy is stale until its own L1 entry expires. A shared L2 is the source of truth on an L1 miss, not a cross-instance invalidation bus.
- Use
GetOrCreateAsyncto return a cached value or compute-and-store it on a miss, with built-in stampede protection so concurrent callers don't all run the factory. - Tune per-type or global entry lifetimes via
AbpHybridCacheOptions/HybridCacheEntryOptions.
When Not to Use
- Plain single-tier typed distributed caching (
IDistributedCache<T>/GetOrAddAsync) or distributed locking (IAbpDistributedLock) — use the distributed-caching-and-locking skill. If you only need one distributed tier and don't need a local in-memory copy, that abstraction is the simpler fit. - Publishing/handling events, deferred jobs, or recurring workers — use the background-jobs-and-events skill.
- Repository-backed automatic entity caching with automatic invalidation on update/delete — use the cache-entities skill.
How it works
IHybridCache<TCacheItem> and IHybridCache<TCacheItem, TCacheKey> (namespace Volo.Abp.Caching.Hybrid, package Volo.Abp.Caching) are typed wrappers over Microsoft.Extensions.Caching.Hybrid.HybridCache. AbpCachingModule registers AddHybridCache() and binds the open generics to AbpHybridCache<> / AbpHybridCache<,> by default, so nothing extra is needed to inject them. HybridCache keeps a fast in-memory L1 tier and, when a distributed cache (L2) is configured, reads through to L2 on an L1 miss and populates both tiers on a write — plus it protects against cache stampede so concurrent misses for the same key run the factory once. (The ABP wrapper writes with tags: null and adds no cross-instance L1 invalidation of its own.)