57 lines
1.6 KiB
C#
Raw Normal View History

2023-03-03 21:18:25 +08:00
using ZR.Common;
using ZR.Common.Cache;
2023-02-15 22:13:01 +08:00
namespace ZR.Admin.WebApi.Extensions
{
public class SqlSugarCache : SqlSugar.ICacheService
{
public void Add<V>(string key, V value)
{
2023-03-03 21:18:25 +08:00
//RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
CacheHelper.SetCache(key, value);
2023-02-15 22:13:01 +08:00
}
public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
2023-03-03 21:18:25 +08:00
//RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
2023-02-15 22:13:01 +08:00
}
public bool ContainsKey<V>(string key)
{
2023-03-03 21:18:25 +08:00
//return RedisServer.Cache.Exists(key);
return CacheHelper.Exists(key);
2023-02-15 22:13:01 +08:00
}
public V Get<V>(string key)
{
2023-03-03 21:18:25 +08:00
//return RedisServer.Cache.Get<V>(key);
return (V)CacheHelper.Get(key);
2023-02-15 22:13:01 +08:00
}
public IEnumerable<string> GetAllKey<V>()
{
return RedisServer.Cache.Keys("*");
}
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
if (RedisServer.Cache.Exists(cacheKey))
{
return Get<V>(cacheKey);
}
else
{
var restul = create();
Add(cacheKey, restul);
return restul;
}
}
public void Remove<V>(string key)
{
RedisServer.Cache.Del(key);
}
}
}