2025-06-06 14:57:20 +08:00

53 lines
1.3 KiB
C#

using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace YD_WeChatApplet.Commons.MemoryCaches
{
/// <summary>
/// 实例化缓存接口ICaching
/// </summary>
public class MemoryCaching : ICaching
{
private readonly IMemoryCache _cache;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="cache"></param>
public MemoryCaching(IMemoryCache cache)
{
_cache = cache;
}
public object Get(string cacheKey)
{
return _cache.Get(cacheKey);
}
public void Update(string cacheKey, object cacheValue, int seconds)
{
_cache.Remove(cacheKey);
_cache.Set(cacheKey, cacheValue, TimeSpan.FromSeconds(seconds));
}
public void Set(string cacheKey, object cacheValue, int seconds)
{
_cache.Set(cacheKey, cacheValue, TimeSpan.FromSeconds(seconds));
}
public void Set(string cacheKey, object cacheValue)
{
_cache.Set(cacheKey, cacheValue);
}
public void Remove(string cacheKey)
{
_cache.Remove(cacheKey);
}
}
}