IDistributedCache implementation for LiteDB
$ dotnet add package LiteDb.Extensions.CachingAn IDistributedCache implementation for LiteDB.
Registering the cache:
IServiceCollection services = //...
services.AddLiteDbCache("MyCache.db")
// or
services.AddLiteDbCache("MyCache.db", "MySuperStrongPassword1111!!!")
// or
services.AddLiteDbCache(options =>
{
options.CachePath = "MyCache.db";
options.Password = "MySuperStrongPassword1111!!!";
});
After this the IDistributedCache can be used as usual;
The package also allows you to have some manual controls for managing the cache through the ILiteDbCache interface:
public interface ILiteDbCache : IDistributedCache
{
int CacheItemCount();
Task<int> CacheCountAsync(CancellationToken cancellationToken = default);
void Clear();
Task ClearAsync(CancellationToken cancellationToke = default);
ulong CacheSizeInBytes();
Task<ulong> CacheSizeInBytesAsync();
}
The library also adds a multi level cache implementation through the IMultiLevelCache interface, which first tries to retrieve an object from the IMemoryCache, then from the IDistributedCache. For serialization it uses System.Text.Json. Another string-based serializer can be used by implementing the IMultiLevelCacheSerializer interface and then registering it through DI.
var key = Guid.NewGuid().ToString();
var value = new TestData(Guid.NewGuid().ToString());
var storedValue = await MultiLevelCache.GetOrSetAsync(key, (_) => Task.FromResult(value), new MemoryCacheEntryOptions(), new DistributedCacheEntryOptions());
Issues and pull requests are welcome.
If you find a bug, please try to recreate it with a test case in the test project and put it in the newly created issue (not mandatory)