依赖命名空间:
Microsoft.AspNetCore.Mvc;//测试调用时
Microsoft.Extensions.Caching.Memory;
Microsoft.Extensions.Caching.Redis;
StackExchange.Redis;
Newtonsoft.Json;
定义通用工具类 :CacheUntity
public class CacheUntity
{
private static ICacheHelper _cache = new RedisCacheHelper();//默认使用Redis
private static bool isInited = false;
public static void Init(ICacheHelper cache)
{
if (isInited)
return;
_cache.Dispose();
_cache = cache;
isInited = true;
}
public static bool Exists(string key)
{
return _cache.Exists(key);
}
public static T GetCache<T>(string key) where T : class
{
return _cache.GetCache<T>(key);
}
public static void SetCache(string key, object value)
{
_cache.SetCache(key, value);
}
public static void SetCache(string key, object value, DateTimeOffset expiressAbsoulte)
{
_cache.SetCache(key, value, expiressAbsoulte);
}
//public void SetCache(string key, object value, double expirationMinute)
//{
//}
public static void RemoveCache(string key)
{
_cache.RemoveCache(key);
}
}
定义统一缓存操作接口:ICacheHelper
public interface ICacheHelper
{
bool Exists(string key);
T GetCache<T>(string key) where T : class;
void SetCache(string key, object value);
void SetCache(string key, object value, DateTimeOffset expiressAbso