using Common; using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading; namespace KC.Cache.Redis { public class RedisHelper { #region 属性 /// /// 数据库编号 /// private int DbNum { get; } /// /// 连接 /// private readonly ConnectionMultiplexer _conn; #endregion #region 构造函数 /// /// 构造函数 /// /// 数据库编号 /// 链接地址 public RedisHelper() { this.DbNum = ConfigHelper.GetValue("RedisDbNum").TryToInt32(); string readWriteHosts = ConfigHelper.GetValue("RedisExchangeHosts"); this._conn = ConnectionMultiplexer.Connect(readWriteHosts); } #endregion 构造函数 #region 方法 /// /// 单个string值存入 /// /// /// /// /// public bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return Do(db => db.StringSet(key, value, expiry)); } /// /// 单个string值存入 /// /// /// /// /// public bool StringSet(string key, T value, TimeSpan? expiry = default(TimeSpan?)) { return Do(db => db.StringSet(key, ConvertJson(value), expiry)); } /// /// 对象存储 /// /// /// /// /// public void ListSet(string key, T value) { Do(db => db.ListRightPush(key, value.TryToJson())); } /// /// 删除单个key /// /// redis key /// 是否删除成功 public bool KeyDelete(string key) { return Do(db => db.KeyDelete(key)); } /// /// 获取单个key的值 /// /// Redis Key /// public string StringGet(string key) { return Do(db => db.StringGet(key)); } /// /// 获取指定key的List /// /// /// public List ListGet(string key) { return Do(redis => { try { var values = redis.ListRange(key); return ConvetList(values); } catch (Exception) { return new List(); } }); } /// /// 获取所有key /// /// public List GetAllKeys() { var res = Do(db => { EndPoint[] endpoints = _conn.GetEndPoints(); List keyList = new List(); foreach (EndPoint ep in endpoints) { var server = _conn.GetServer(ep); var keys = server.Keys(DbNum, "*"); foreach (var item in keys) { keyList.Add((string)item); } }; return keyList; }); return res; } /// /// 获取指定key的object /// /// /// public T ListRangeObj(string key) {; return Do(redis => { try { var values = redis.ListRange(key); var content = values.Count() > 0 ? values[0] : new RedisValue(); return ConvertObj(content); } catch (Exception) { Thread.Sleep(1000); var values = redis.ListRange(key); var content = values.Count() > 0 ? values[0] : new RedisValue(); return ConvertObj(content); } }); } /// /// 判断key是否存储 /// /// redis key /// public bool KeyExists(string key) { return Do(db => db.KeyExists(key)); } /// /// 执行操作 /// /// /// /// private T Do(Func func) { var database = _conn.GetDatabase(DbNum); return func(database); } /// /// 设置Key的时间 /// /// redis key /// /// public bool KeyExpire(string key, TimeSpan? expiry = default(TimeSpan?)) { return Do(db => db.KeyExpire(key, expiry)); } /// /// 删除多个key /// /// rediskey /// 成功删除的个数 public long KeyDelete(List keys) { return Do(db => db.KeyDelete(ConvertRedisKeys(keys))); } #endregion #region 转换 /// /// 转换成集合 /// /// /// /// private List ConvetList(RedisValue[] values) { List result = new List(); //try //{ // //一个Key下面存了多个相同实体 // foreach (RedisValue value in values) // { // var model = ConvertObj(value); // result.Add(model); // } //} //catch (Exception e) //{ //一个Key下面存的是一个集合 foreach (RedisValue value in values) { List temp = JsonConvert.DeserializeObject>(value); result = result.Concat(temp).ToList(); } // } return result; } /// /// 转换成Json字符串 /// /// /// /// private string ConvertJson(T value) { string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value); return result; } /// /// 转换成对象 /// /// /// /// private T ConvertObj(RedisValue value) { if (!value.IsNullOrEmpty) { var val = value.ToString(); try { if (!val.IsEmpty() && val.IsJson()) return JsonConvert.DeserializeObject(val); return default(T); } catch (Exception ex) { throw; } } return default(T); } /// /// 批量转换成RedisKey /// /// /// private RedisKey[] ConvertRedisKeys(List redisKeys) { return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray(); } #endregion } }