Skip to content

Caching

Arnel Robles edited this page Sep 28, 2026 · 1 revision

Package 10: Mapsicle.Caching

Cache mapped results in IMemoryCache or IDistributedCache.

using Mapsicle.Caching;

// You choose the key. A later call with the same key returns the cached DTO,
// even if the source has changed since, until the entry expires (5 minute sliding by default).
var dto = user.MapToCached<UserDto>(memoryCache, $"user:{user.Id}");

memoryCache.InvalidateMappingCache($"user:{user.Id}");

CachedMapper wraps a fluent IMapper and keys each entry by the source's content, so a changed source is mapped again rather than served stale. InvalidateAll() removes only the entries that mapper created.

var cached = new CachedMapper(mapper, memoryCache);
var dto = cached.Map<UserDto>(user);

The async methods (MapToCachedAsync, MapCollectionToCachedAsync) work against IDistributedCache and store the mapped value as JSON.


Clone this wiki locally