using ZR.Common.Cache; namespace ZR.ServiceCore.SqlSugar { /// /// 数据库Redis缓存 /// public class SqlSugarRedisCache : ICacheService { public void Add(string key, V value) { RedisServer.Cache.Set(key, value); } public void Add(string key, V value, int cacheDurationInSeconds) { RedisServer.Cache.Set(key, value, cacheDurationInSeconds); } public bool ContainsKey(string key) { return RedisServer.Cache.Exists(key); } public V Get(string key) { return RedisServer.Cache.Get(key); } public IEnumerable GetAllKey() { return RedisServer.Cache.Keys("SqlSugarDataCache.*"); } public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) { if (ContainsKey(cacheKey)) { var result = Get(cacheKey); if (result == null) { return create(); } else { return result; } } else { var restul = create(); Add(cacheKey, restul, cacheDurationInSeconds); return restul; } } public void Remove(string key) { RedisServer.Cache.Del(key); } } }