using AUTS.Services.Enums;
using AUTS.Services.Extensions;
using AUTS.Services.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AUTS.Services.Cache
{
public class BaseCacheHelp : BaseCacheHelpRedis
{
}
#region 缓存管理-MVC
///
/// 缓存管理-MVC内核
///
public abstract class BaseCacheHelpMVC
{
///
/// 获取设置-string
///
/// 泛型
/// 委托方法
/// 缓存key
/// 超时设置
/// 超时设置
/// 返回泛型实例
public static T GetCache(string key, Func setAcrion, CacheTimeType? cacheTimeType = null, int? cacheTime = null)
{
if (CacheExtensions.CheckCache(key))
{
return CacheExtensions.GetCache(key);
}
else
{
T model = setAcrion();
if (model != null)
{
if (cacheTimeType != null && cacheTime != null)
{
CacheExtensions.SetCache(key, model, (CacheTimeType)cacheTimeType, (int)cacheTime);
}
}
return model;
}
}
///
/// 设置参数 string
///
/// 泛型
/// 委托方法
/// 缓存key
/// 超时设置
/// 超时设置
public static void SetCache(string key, Func setAcrion, CacheTimeType? cacheTimeType = null, int? cacheTime = null)
{
T model = setAcrion();
if (model != null)
{
if (cacheTimeType != null && cacheTime != null)
{
CacheExtensions.SetCache(key, model, (CacheTimeType)cacheTimeType, (int)cacheTime);
}
}
}
///
/// 获取 string 失败返回 null
///
/// 泛型
/// 缓存key
/// 返回泛型实例
public static T GetVaue(string key)
{
if (CacheExtensions.CheckCache(key))
{
return CacheExtensions.GetCache(key);
}
else
{
return default(T);
}
}
///
/// 清除缓存
///
/// 缓存key
public static void ClearCache(string key)
{
if (CacheExtensions.CheckCache(key))
{
CacheExtensions.ClearCache(key);
}
}
#region 缓存管理
///
/// 设置缓存
///
///
///
///
public static T GetCacheHelp(string key, Func action)
{
if (CacheExtensions.CheckCache(key))
{
return CacheExtensions.GetCache(key);
}
else
{
var model = action();
CacheExtensions.SetCache(key, model);
return model;
}
}
#endregion
}
#endregion
#region 缓存管理-Redis 基于Redis-string
///
/// 缓存管理-Redis内核 基于Redis-string
///
public abstract class BaseCacheHelpRedis
{
///
/// 获取设置-string
///
/// 泛型
/// 委托方法
/// 缓存key
/// 超时设置
/// 返回泛型实例
public static T GetCache(string key, Func setAcrion, TimeSpan? expiry = default(TimeSpan?))
{
if (RedisHelper.CheckKey(key))
{
return RedisHelper.StringGet(key);
}
else
{
T model = setAcrion();
if (model != null)
{
RedisHelper.StringSet(key, model, expiry);
}
return model;
}
}
///
/// 设置参数 string
///
/// 泛型
/// 委托方法
/// 缓存key
/// 超时设置
public static void SetCache(string key, Func setAcrion, TimeSpan? expiry = default(TimeSpan?))
{
T model = setAcrion();
if (model != null)
{
RedisHelper.StringSet(key, model, expiry);
}
}
///
/// 获取 string 失败返回 null
///
/// 泛型
/// 缓存key
/// 返回泛型实例
public static T GetVaue(string key)
{
if (RedisHelper.CheckKey(key))
{
return RedisHelper.StringGet(key);
}
else
{
return default(T);
}
}
///
/// 清除缓存
///
/// 缓存key
public static void ClearCache(string key)
{
if (RedisHelper.CheckKey(key))
{
RedisHelper.RemoveKey(key);
}
}
///
/// 获取哈希缓存,没有则添加
///
/// 泛型
/// 缓存key
/// 键
/// 委托方法
///
public static T GetHashCache(string key, string sid, Func setAcrion)
{
if (RedisHelper.HashExists(key, sid))
{
return RedisHelper.HashGet(key, sid);
}
else
{
T model = setAcrion();
if (model != null)
{
RedisHelper.HashSet(key, sid, model);
}
return model;
}
}
///
/// 写入哈希缓存
///
/// 泛型
/// 缓存key
/// 键
/// 实体
public static void SetHashCache(string key, string sid, T model)
{
RedisHelper.HashSet(key, sid, model);
}
///
/// 写入哈希缓存
///
/// 泛型
/// 缓存key
/// 字典
public static void SetHashCache(string key, Dictionary dic)
{
RedisHelper.HashSet(key, dic);
}
///
/// 获取哈希缓存字典
///
/// 泛型
/// 缓存key
///
public static Dictionary GetHashAllCache(string key)
{
return RedisHelper.HashGetAll(key);
}
public static bool CheckKey(string key)
{
return RedisHelper.CheckKey(key);
}
}
#endregion
}