初始化项目
This commit is contained in:
87
Services/Cache/BaseCacheHelpRedis.cs
Normal file
87
Services/Cache/BaseCacheHelpRedis.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
// 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)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
return RedisHelper.StringGet<T>(key);
|
||||
}
|
||||
T val = setAcrion();
|
||||
if (val != null)
|
||||
{
|
||||
RedisHelper.StringSet(key, val, expiry);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
public static void SetCache<T>(string key, Func<T> setAcrion, TimeSpan? expiry = null)
|
||||
{
|
||||
T val = setAcrion();
|
||||
if (val != null)
|
||||
{
|
||||
RedisHelper.StringSet(key, val, expiry);
|
||||
}
|
||||
}
|
||||
|
||||
public static T GetVaue<T>(string key)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
return RedisHelper.StringGet<T>(key);
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public static void ClearCache(string key)
|
||||
{
|
||||
if (RedisHelper.CheckKey(key))
|
||||
{
|
||||
RedisHelper.RemoveKey(key);
|
||||
}
|
||||
}
|
||||
public static void ClearCacheList(List<string> keyList)
|
||||
{
|
||||
RedisHelper.RemoveKeyList(keyList);
|
||||
}
|
||||
|
||||
public static T GetHashCache<T>(string key, string sid, Func<T> setAcrion)
|
||||
{
|
||||
if (RedisHelper.HashExists(key, sid))
|
||||
{
|
||||
return RedisHelper.HashGet<T>(key, sid);
|
||||
}
|
||||
T val = setAcrion();
|
||||
if (val != null)
|
||||
{
|
||||
RedisHelper.HashSet(key, sid, val);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
public static void SetHashCache<T>(string key, string sid, T model)
|
||||
{
|
||||
RedisHelper.HashSet(key, sid, model);
|
||||
}
|
||||
|
||||
public static void SetHashCache<T>(string key, Dictionary<string, T> dic)
|
||||
{
|
||||
RedisHelper.HashSet(key, dic);
|
||||
}
|
||||
|
||||
public static Dictionary<string, T> GetHashAllCache<T>(string key)
|
||||
{
|
||||
return RedisHelper.HashGetAll<T>(key);
|
||||
}
|
||||
|
||||
public static bool CheckKey(string key)
|
||||
{
|
||||
return RedisHelper.CheckKey(key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user