using Furion; using SqlSugar; using SugarRedis; //using SugarRedis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cylsg.Core { public class SqlSugarRedisCache : ICacheService { //NUGET安装 SugarRedis (也可以自个实现) //注意:SugarRedis 不要扔到构造函数里面, 一定要单例模式 public static SugarRedisClient _service = new SugarRedisClient(App.Configuration["RedisConfig:ConnectionString"] ?? "127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0"); //+1重载 new SugarRedisClient(字符串) //默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0 public void Add(string key, V value) { _service.Set(key, value); } public void Add(string key, V value, int cacheDurationInSeconds) { _service.SetBySeconds(key, value, cacheDurationInSeconds); } public bool ContainsKey(string key) { return _service.Exists(key); } public V Get(string key) { return _service.Get(key); } public IEnumerable GetAllKey() { //性能注意: 只查sqlsugar用到的key return _service.SearchCacheRegex("SqlSugarDataCache.*"); } public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) { if (this.ContainsKey(cacheKey)) { var result = this.Get(cacheKey); if (result == null) { return create(); } else { return result; } } else { var result = create(); this.Add(cacheKey, result, cacheDurationInSeconds); return result; } } public void Remove(string key) { _service.Remove(key); } } }