32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
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;
|
|
}
|
|
} |