Caching
Overview
VersaTul.Caching provides a simple cache abstraction with a ready-to-use in-memory implementation built on Microsoft.Extensions.Caching.Memory.
The package is useful when you want your application code to depend on a small cache contract instead of directly depending on the underlying cache engine.
When To Use This Package
Use this package when you want to:
Cache frequently accessed objects in memory.
Hide the underlying cache implementation behind an interface.
Configure default cache duration from a shared configuration model.
Use absolute or sliding expiration policies.
React to cache item eviction events.
Installation
Install the package with the .NET CLI:
dotnet add package VersaTul.Caching
Or with the Package Manager Console:
PM> NuGet\Install-Package VersaTul.Caching -Version latest
Core Types And Concepts
ICacheProviderandICacheProvider<T>The main cache abstraction for reading, writing, removing, and clearing cached entries.
MemCacheProvider<T>The default in-memory cache implementation.
ICacheConfigurationandCacheConfigurationConfiguration types that provide settings such as the default cache duration.
CacheExpirationExpiration model for absolute or sliding expiration.
ICacheClockClock abstraction used for expiration calculations and time-sensitive testing.
MemCacheEventArgsEvent payload used when an item is removed from the cache.
Key Capabilities
Get()andGetAsync()return cached values or the default value forTon a cache miss.Add()andSetAsync()support default duration, explicit duration, andCacheExpirationoverloads.IsExists()checks whether a key is present.Remove()andClear()remove one or all entries.ItemRemovedlets you observe cache evictions.MemCacheProvider<T>supports injectingMemoryCacheOptions,ILoggerFactory, and a customICacheClock.
Basic Example
using VersaTul.Caching;
using VersaTul.Caching.Configurations;
using VersaTul.Caching.Contracts;
using VersaTul.Configuration.Defaults.Caching;
var configSettings = new Builder().BuildConfig();
var cacheConfiguration = new CacheConfiguration(configSettings);
ICacheProvider<Person> cacheProvider = new MemCacheProvider<Person>(cacheConfiguration);
var person = new Person { Age = 10, Name = "Bjorn" };
cacheProvider.Add("Bjorn", person);
var cachedPerson = cacheProvider.Get("Bjorn");
Expiration Example
Use CacheExpiration when you want explicit absolute or sliding expiration behavior.
using VersaTul.Caching;
cacheProvider.Add(
"user:42",
person,
new CacheExpiration
{
Sliding = TimeSpan.FromMinutes(15)
});
Async And Eviction Example
cacheProvider.ItemRemoved += (_, args) =>
{
Console.WriteLine($"Removed cache item {args.Key} because {args.Reason}");
};
await cacheProvider.SetAsync("profile:42", person, 120);
var cached = await cacheProvider.GetAsync("profile:42");
Notes
The default
Add(key, value)overload usesCacheDurationfromICacheConfiguration.CacheExpirationmust contain either an absolute or sliding value.Inject a custom
ICacheClockwhen you need deterministic time behavior in tests.