64 lines
1.8 KiB
C#
Raw Normal View History

2024-06-07 11:04:26 +08:00
namespace ZR.Admin.WebApi.Extensions
2023-02-15 22:13:01 +08:00
{
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>()
{
2023-03-05 16:58:32 +08:00
//return RedisServer.Cache.Keys("*");
return CacheHelper.GetCacheKeys();
2023-02-15 22:13:01 +08:00
}
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
2023-03-05 16:58:32 +08:00
if (ContainsKey<V>(cacheKey))
2023-02-15 22:13:01 +08:00
{
2023-03-05 16:58:32 +08:00
var result = Get<V>(cacheKey);
if (result == null)
{
return create();
}
else
{
return result;
}
2023-02-15 22:13:01 +08:00
}
else
{
var restul = create();
2023-03-05 16:58:32 +08:00
Add(cacheKey, restul, cacheDurationInSeconds);
2023-02-15 22:13:01 +08:00
return restul;
}
}
public void Remove<V>(string key)
{
2023-03-05 16:58:32 +08:00
//RedisServer.Cache.Del(key);
CacheHelper.Remove(key);
2023-02-15 22:13:01 +08:00
}
}
}