Add project files.
This commit is contained in:
32
JSMR.Infrastructure/Caching/MemoryCacheAdapter.cs
Normal file
32
JSMR.Infrastructure/Caching/MemoryCacheAdapter.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using JSMR.Application.Common.Caching;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace JSMR.Infrastructure.Caching;
|
||||
|
||||
public class MemoryCacheAdapter(IMemoryCache memoryCache) : ICache
|
||||
{
|
||||
public ValueTask<T?> GetAsync<T>(string key, CancellationToken cancellationToken = default)
|
||||
{
|
||||
memoryCache.TryGetValue(key, out T? value);
|
||||
|
||||
return ValueTask.FromResult(value);
|
||||
}
|
||||
|
||||
public ValueTask SetAsync<T>(string key, T value, CacheEntryOptions options, CancellationToken cancellationToken = default)
|
||||
{
|
||||
MemoryCacheEntryOptions memoryCacheOptions = new();
|
||||
|
||||
if (options.AbsoluteExpirationRelativeToNow != null)
|
||||
memoryCacheOptions.SetAbsoluteExpiration(options.AbsoluteExpirationRelativeToNow.Value);
|
||||
|
||||
if (options.AbsoluteExpiration != null)
|
||||
memoryCacheOptions.SetAbsoluteExpiration(options.AbsoluteExpiration.Value);
|
||||
|
||||
if (options.SlidingExpiration != null)
|
||||
memoryCacheOptions.SetSlidingExpiration(options.SlidingExpiration.Value);
|
||||
|
||||
memoryCache.Set(key, value, memoryCacheOptions);
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user