初始化项目
This commit is contained in:
271
AUTS.Services/Cache/BaseCacheHelp.cs
Normal file
271
AUTS.Services/Cache/BaseCacheHelp.cs
Normal file
@@ -0,0 +1,271 @@
|
||||
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
|
||||
/// <summary>
|
||||
/// 缓存管理-MVC内核
|
||||
/// </summary>
|
||||
public abstract class BaseCacheHelpMVC
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取设置-string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="cacheTimeType">超时设置</param>
|
||||
/// <param name="cacheTime">超时设置</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetCache<T>(string key, Func<T> setAcrion, CacheTimeType? cacheTimeType = null, int? cacheTime = null)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
return CacheExtensions.GetCache<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
if (cacheTimeType != null && cacheTime != null)
|
||||
{
|
||||
CacheExtensions.SetCache(key, model, (CacheTimeType)cacheTimeType, (int)cacheTime);
|
||||
}
|
||||
}
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置参数 string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="cacheTimeType">超时设置</param>
|
||||
/// <param name="cacheTime">超时设置</param>
|
||||
public static void SetCache<T>(string key, Func<T> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 string 失败返回 null
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetVaue<T>(string key)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
return CacheExtensions.GetCache<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
public static void ClearCache(string key)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
CacheExtensions.ClearCache(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region 缓存管理
|
||||
/// <summary>
|
||||
/// 设置缓存
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetCacheHelp<T>(string key, Func<T> action)
|
||||
{
|
||||
if (CacheExtensions.CheckCache(key))
|
||||
{
|
||||
return CacheExtensions.GetCache<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
var model = action();
|
||||
CacheExtensions.SetCache(key, model);
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 缓存管理-Redis 基于Redis-string
|
||||
/// <summary>
|
||||
/// 缓存管理-Redis内核 基于Redis-string
|
||||
/// </summary>
|
||||
public abstract class BaseCacheHelpRedis
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取设置-string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="expiry">超时设置</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = default(TimeSpan?))
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
return RedisHelper.StringGet<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
RedisHelper.StringSet(key, model, expiry);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置参数 string
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="expiry">超时设置</param>
|
||||
public static void SetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = default(TimeSpan?))
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
RedisHelper.StringSet(key, model, expiry);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 string 失败返回 null
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns>返回泛型实例</returns>
|
||||
public static T GetVaue<T>(string key)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
return RedisHelper.StringGet<T>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存key</param>
|
||||
public static void ClearCache(string key)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
RedisHelper.RemoveKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取哈希缓存,没有则添加
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="sid">键</param>
|
||||
/// <param name="setAcrion">委托方法</param>
|
||||
/// <returns></returns>
|
||||
public static T GetHashCache<T>(string key, string sid, Func<T> setAcrion)
|
||||
{
|
||||
if (RedisHelper.HashExists(key, sid))
|
||||
{
|
||||
return RedisHelper.HashGet<T>(key, sid);
|
||||
}
|
||||
else
|
||||
{
|
||||
T model = setAcrion();
|
||||
if (model != null)
|
||||
{
|
||||
RedisHelper.HashSet(key, sid, model);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入哈希缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="sid">键</param>
|
||||
/// <param name="model">实体</param>
|
||||
public static void SetHashCache<T>(string key, string sid, T model)
|
||||
{
|
||||
RedisHelper.HashSet(key, sid, model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入哈希缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <param name="dic">字典</param>
|
||||
public static void SetHashCache<T>(string key, Dictionary<string, T> dic)
|
||||
{
|
||||
RedisHelper.HashSet(key, dic);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取哈希缓存字典
|
||||
/// </summary>
|
||||
/// <typeparam name="T">泛型</typeparam>
|
||||
/// <param name="key">缓存key</param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<string, T> GetHashAllCache<T>(string key)
|
||||
{
|
||||
return RedisHelper.HashGetAll<T>(key);
|
||||
}
|
||||
|
||||
public static bool CheckKey(string key)
|
||||
{
|
||||
return RedisHelper.CheckKey(key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user