Files

88 lines
2.2 KiB
C#
Raw Permalink Normal View History

2025-11-20 09:50:21 +08:00
// Services.Cache.BaseCacheHelpRedis
using System;
using System.Collections.Generic;
using Org.BouncyCastle.Utilities;
using Services.Tool;
using StackExchange.Redis;
public abstract class BaseCacheHelpRedis
{
public static T GetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = null)
{
2025-12-22 17:47:19 +08:00
if (StackChangeRedisHelper.CheckKey(key))
2025-11-20 09:50:21 +08:00
{
2025-12-22 17:47:19 +08:00
return StackChangeRedisHelper.StringGet<T>(key);
2025-11-20 09:50:21 +08:00
}
T val = setAcrion();
if (val != null)
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.StringSet(key, val, expiry);
2025-11-20 09:50:21 +08:00
}
return val;
}
public static void SetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = null)
{
T val = setAcrion();
if (val != null)
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.StringSet(key, val, expiry);
2025-11-20 09:50:21 +08:00
}
}
public static T GetVaue<T>(string key)
{
2025-12-22 17:47:19 +08:00
if (StackChangeRedisHelper.CheckKey(key))
2025-11-20 09:50:21 +08:00
{
2025-12-22 17:47:19 +08:00
return StackChangeRedisHelper.StringGet<T>(key);
2025-11-20 09:50:21 +08:00
}
return default(T);
}
public static void ClearCache(string key)
{
2025-12-22 17:47:19 +08:00
if (StackChangeRedisHelper.CheckKey(key))
2025-11-20 09:50:21 +08:00
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.RemoveKey(key);
2025-11-20 09:50:21 +08:00
}
}
public static void ClearCacheList(List<string> keyList)
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.RemoveKeyList(keyList);
2025-11-20 09:50:21 +08:00
}
public static T GetHashCache<T>(string key, string sid, Func<T> setAcrion)
{
2025-12-22 17:47:19 +08:00
if (StackChangeRedisHelper.HashExists(key, sid))
2025-11-20 09:50:21 +08:00
{
2025-12-22 17:47:19 +08:00
return StackChangeRedisHelper.HashGet<T>(key, sid);
2025-11-20 09:50:21 +08:00
}
T val = setAcrion();
if (val != null)
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.HashSet(key, sid, val);
2025-11-20 09:50:21 +08:00
}
return val;
}
public static void SetHashCache<T>(string key, string sid, T model)
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.HashSet(key, sid, model);
2025-11-20 09:50:21 +08:00
}
public static void SetHashCache<T>(string key, Dictionary<string, T> dic)
{
2025-12-22 17:47:19 +08:00
StackChangeRedisHelper.HashSet(key, dic);
2025-11-20 09:50:21 +08:00
}
public static Dictionary<string, T> GetHashAllCache<T>(string key)
{
2025-12-22 17:47:19 +08:00
return StackChangeRedisHelper.HashGetAll<T>(key);
2025-11-20 09:50:21 +08:00
}
public static bool CheckKey(string key)
{
2025-12-22 17:47:19 +08:00
return StackChangeRedisHelper.CheckKey(key);
2025-11-20 09:50:21 +08:00
}
}