using CSRedis; using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using VOL.Core.Configuration; using VOL.Core.Const; namespace VOL.Core.CacheManager { public class RedisCacheService : ICacheService { public RedisCacheService() { var csredis = new CSRedisClient(AppSetting.RedisConnectionString); RedisHelper.Initialization(csredis); } /// /// 验证缓存项是否存在 /// /// 缓存Key /// public bool Exists(string key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } return RedisHelper.Exists(key); } public void LPush(string key, string val) { RedisHelper.LPush(key, val); } public void RPush(string key, string val) { RedisHelper.RPush(key, val); } public T ListDequeue(string key) where T : class { string value = RedisHelper.RPop(key); if (string.IsNullOrEmpty(value)) return null; return JsonConvert.DeserializeObject(value); } public object ListDequeue(string key) { string value = RedisHelper.RPop(key); if (string.IsNullOrEmpty(value)) return null; return value; } /// /// 移除list中的数据,keepIndex为保留的位置到最后一个元素如list 元素为1.2.3.....100 /// 需要移除前3个数,keepindex应该为4 /// /// /// public void ListRemove(string key, int keepIndex) { RedisHelper.LTrim(key, keepIndex, -1); } public bool Add(string key, string value, int expireSeconds = -1, bool isSliding = false) { return RedisHelper.Set(key, value, expireSeconds); } public bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false) { return RedisHelper.Set(key, value, expireSeconds); } /// /// 删除缓存 /// /// 缓存Key /// public bool Remove(string key) { RedisHelper.Del(key); return true; } /// /// 批量删除缓存 /// /// 缓存Key集合 /// public void RemoveAll(IEnumerable keys) { RedisHelper.Del(keys.ToArray()); } /// /// 获取缓存 /// /// 缓存Key /// public T Get(string key) where T : class { return RedisHelper.Get(key); } /// /// 获取缓存 /// /// 缓存Key /// public string Get(string key) { return RedisHelper.Get(key); } public void Dispose() { } } }