/// <summary> /// 从hash表获取数据 /// </summary> /// <param name="keys"></param> /// <returns></returns> public Dictionary <string, Dictionary <string, string> > HashGet(Dictionary <string, string[]> keys) { Dictionary <string, Dictionary <string, string> > dic = new Dictionary <string, Dictionary <string, string> >(); if (keys != null && keys.Count > 0) { Dictionary <string, string[]> values = QuickHelperBase.HashGet(keys); foreach (var item in keys) { string[] valuesList = values.Where(x => x.Key == string.Concat(QuickHelperBase.Name, item.Key)) .Select(x => x.Value).FirstOrDefault(); Dictionary <string, string> newDic = new Dictionary <string, string>(); if (valuesList != null && valuesList.Length > 0) { for (int i = 0; i < item.Value.Length; i++) { if (!newDic.ContainsKey(item.Value[i]) && valuesList[i] != null) { newDic.Add(item.Value[i], valuesList[i]); } } } dic.Add(item.Key, newDic); } } return(dic); }
/// <summary> /// 获取指定key的List /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public List <T> ListRange <T>(string key, long count = 1000) where T : class, new() { List <T> list = new List <T>(); QuickHelperBase.LRang(key, 0, count).ToList().ForEach(p => { list.Add(ConvertObj <T>(p)); }); return(list); }
/// <summary> /// 存储数据到hash表 /// </summary> /// <param name="kValues"></param> /// <param name="second"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public bool HashSet <T>(Dictionary <string, Dictionary <string, T> > kValues, long second = -1, bool isSetHashKeyExpire = true) { Dictionary <string, object[]> keyValues = new Dictionary <string, object[]>(); foreach (var item in kValues) { List <object> dataKeyValues = new List <object>(); foreach (var kvp in item.Value) { dataKeyValues.Add(isSetHashKeyExpire ? GetKey(kvp.Key) : kvp.Key); dataKeyValues.Add(ConvertJson(kvp.Value)); } keyValues.Add(isSetHashKeyExpire ? GetKey(item.Key) : item.Key, dataKeyValues.ToArray()); } if (isSetHashKeyExpire) { return(string.Equals(QuickHelperBase.HashSetHashFileExpire(keyValues, GetExpire(second)), "OK", StringComparison.OrdinalIgnoreCase)); } return(string.Equals(QuickHelperBase.HashSetExpire(keyValues, GetExpire(second)), "OK", StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// 查找所有符合给定模式( pattern)的 key /// </summary> /// <param name="pattern">如:runoob*,不含prefix前辍RedisHelper.Name</param> /// <returns></returns> public List <string> Keys(string pattern) { var keys = new List <string>(); QuickHelperBase.Keys(_prefix + pattern).ToList().ForEach(p => { keys.Add(p.Substring(_prefix.Length)); }); return(keys); }
/// <summary> /// 保存一个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="obj"></param> /// <param name="expiry"></param> /// <param name="overdueStrategy"></param> /// <returns></returns> public Task <bool> StringSetAsync <T>(string key, T obj, TimeSpan?expiry = default(TimeSpan?), OverdueStrategy overdueStrategy = null) { return(base.Execute(overdueStrategy, () => QuickHelperBase.SetAsync(key, ConvertJson <T>(obj), expiry.HasValue ? Convert.ToInt32(expiry.Value.TotalSeconds) : -1), () => { return Task.FromResult(false); })); }
/// <summary> /// 存储数据到hash表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <param name="t"></param> /// <param name="overdueStrategy"></param> /// <returns></returns> public async Task <bool> HashSetAsync <T>(string key, string dataKey, T t, OverdueStrategy overdueStrategy = null) { return((await base.Execute(overdueStrategy, () => { return QuickHelperBase.HashSetAsync(key, dataKey, ConvertJson <T>(t)); }, () => { return Task.FromResult("False"); })).Equals("TRUE", StringComparison.OrdinalIgnoreCase)); // return string.Equals(await QuickHelperBase.HashSetAsync(key, dataKey, ConvertJson<T>(t)), "TRUE", // StringComparison.OrdinalIgnoreCase); }
/// <summary> /// 添加 (当score一样value一样时不插入) /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="score"></param> /// <param name="isOverlap"></param> /// <returns></returns> public bool SortedSetAdd <T>(string key, T value, double score, bool isOverlap = false) { if (isOverlap) { SortedSetRemove <T>(key, value); } return(QuickHelperBase.ZAdd(key, (score, ConvertJson <T>(value))) > 0); }
/// <summary> /// 获取已过期的hashKey /// 其中Item1为SortSet的Key,Item2为SortSet的Value,Item3为HashSet的Key,Item4为HashSet的HashKey /// </summary> /// <param name="count"></param> /// <returns></returns> public List <ValueTuple <string, string, string, string> > SortedSetRangeByRankAndOverTime(long count = 1000) { var keyList = QuickHelperBase .ZRevRangeByScore(QuickHelperBase.GetCacheFileKeys(), TimeCommon.GetTimeSpan(DateTime.Now), 0, count, null); //得到过期的key集合 List <ValueTuple <string, string, string, string> > result = new List <(string, string, string, string)>(); keyList.ForEach(item => { for (int i = 0; i < item.Item2.Length; i += 2) { result.Add((item.Item1.Replace(_prefix, ""), item.Item2[i].ToString(), item.Item2[i].ToString().Replace("~_~", "!").Split('!')[0], item.Item2[i].ToString().Replace("~_~", "!").Split('!')[1])); } }); return(result); }
/// <summary> /// 存储数据到hash表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <param name="t"></param> /// <param name="second">秒</param> /// <param name="isSetHashKeyExpire">false:设置key的过期时间,true:设置hashkey的过期时间,默认设置的为HashKey的过期时间。</param> /// <returns></returns> public bool HashSet <T>(string key, string dataKey, T t, long second = -1, bool isSetHashKeyExpire = true) { string value = ""; if (!isSetHashKeyExpire) { value = QuickHelperBase.HashSetExpire(key, GetExpire(second), dataKey, ConvertJson(t)); } else { value = QuickHelperBase.HashSetHashFileExpire(GetKey(key), GetKey(dataKey), GetExpire(second), ConvertJson(t)); } bool result = string.Equals(value, "OK", StringComparison.OrdinalIgnoreCase); return(result); }
/// <summary> /// 从hash表获取数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKeys"></param> /// <returns></returns> public Dictionary <string, string> HashGet(string key, List <string> dataKeys) { if (dataKeys != null && dataKeys.Count > 0) { dataKeys = dataKeys.Distinct().ToList(); var values = QuickHelperBase.HashGet(key, dataKeys.ToArray()).ToList(); Dictionary <string, string> dic = new Dictionary <string, string>(); for (int i = 0; i < dataKeys.Count; i++) { if (!dic.ContainsKey(dataKeys[i]) && values[i] != null) { dic.Add(dataKeys[i], values[i]); } } return(dic); } return(QuickHelperBase.HashGetAll(key)); }
/// <summary> /// 存储数据到hash表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="kvalues"></param> /// <param name="second">秒</param> /// <param name="isSetHashKeyExpire"></param> /// <returns></returns> public bool HashSet <T>(string key, Dictionary <string, T> kvalues, long second = -1, bool isSetHashKeyExpire = true) { List <object> keyValues = new List <object>(); foreach (var kvp in kvalues) { keyValues.Add(isSetHashKeyExpire ? GetKey(kvp.Key) : kvp.Key); keyValues.Add(ConvertJson(kvp.Value)); } if (isSetHashKeyExpire) { return(string.Equals( QuickHelperBase.HashSetHashFileExpire(GetKey(key), GetExpire(second), keyValues.ToArray()), "OK", StringComparison.OrdinalIgnoreCase)); } return(string.Equals(QuickHelperBase.HashSetExpire(key, GetExpire(second), keyValues.ToArray()), "OK", StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// 存储数据到hash表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <param name="t"></param> /// <param name="second">秒</param> /// <param name="isSetHashKeyExpire">false:设置key的过期时间(整个键使用一个过期时间),true:设置hashkey的过期时间,默认设置的为HashKey的过期时间(单个datakey使用一个过期时间)。</param> /// <param name="overdueStrategy"></param> /// <returns></returns> public bool HashSet <T>(string key, string dataKey, T t, long second = -1L, bool isSetHashKeyExpire = true, OverdueStrategy overdueStrategy = null) { string value = ""; return(base.Execute(overdueStrategy, () => { if (!isSetHashKeyExpire) { value = QuickHelperBase.HashSetExpire(key, GetExpire(second), dataKey, ConvertJson(t)); } else { value = QuickHelperBase.HashSetHashFileExpire(GetKey(key), GetKey(dataKey), GetExpire(second), ConvertJson(t)); } bool result = string.Equals(value, "OK", StringComparison.OrdinalIgnoreCase); return result; }, () => { return false; })); }
/// <summary> /// 获取单个key的值 /// </summary> /// <param name="key">Redis Key</param> /// <returns></returns> public string StringGet(string key) { return(QuickHelperBase.Get(key)); }
/// <summary> /// 获取全部 /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public List <T> SortedSetRangeByRank <T>(string key, long count = 1000) where T : class, new() { return(ConvertListObj <T>(QuickHelperBase.ZRange(key, 0, count).ToList())); }
/// <summary> /// 获取全部 /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public List <string> SortedSetRangeByRank(string key, long count = 1000) { return(QuickHelperBase.ZRange(key, 0, count).ToList()); }
/// <summary> /// 删除 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SortedSetRemove <T>(string key, T value) { return(QuickHelperBase.ZRem(key, new string[1] { ConvertJson <T>(value) }) > 0); }
/// <summary> /// 获取集合中的数量 /// </summary> /// <param name="key"></param> /// <returns></returns> public long ListLength(string key) { return(QuickHelperBase.LLen(key)); }
/// <summary> /// 保存一个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="obj"></param> /// <param name="expiry"></param> /// <returns></returns> public bool StringSet <T>(string key, T obj, TimeSpan?expiry = default(TimeSpan?)) { return(QuickHelperBase.Set(key, ConvertJson(obj), expiry.HasValue ? Convert.ToInt32(expiry.Value.TotalSeconds) : -1)); }
/// <summary> /// 入栈 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public async Task <long> ListLeftPushAsync <T>(string key, T value) { return(await QuickHelperBase.LPushAsync(key, new string[1] { ConvertJson <T>(value) })); }
/// <summary> /// 入队 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public async Task <long> ListRightPushAsync <T>(string key, T value) { return(await QuickHelperBase.RPushAsync(key, new[] { ConvertJson(value) })); }
/// <summary> /// 获取指定key的List /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public async Task <List <T> > ListRangeAsync <T>(string key, long count = 1000) where T : class, new() { return(ConvertListObj <T>((await QuickHelperBase.LRangAsync(key, 0, count)).ToList())); }
/// <summary> /// 获取指定key的List /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public async Task <List <string> > ListRangeAsync(string key, long count = 1000) { return((await QuickHelperBase.LRangAsync(key, 0, count)).ToList()); }
/// <summary> /// 移除指定ListId的内部List的值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public async Task <long> ListRemoveAsync <T>(string key, T value) { return(await QuickHelperBase.LRemAsync(key, int.MaxValue, ConvertJson(value))); }
/// <summary> /// 降序获取指定索引的集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="fromRank"></param> /// <param name="toRank"></param> /// <returns></returns> public List <string> GetRangeFromSortedSetDesc(string key, long fromRank, long toRank) { return(QuickHelperBase.ZRevRange(key, fromRank, toRank).ToList()); }
/// <summary> /// 降序获取指定索引的集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="fromRank"></param> /// <param name="toRank"></param> /// <returns></returns> public List <T> GetRangeFromSortedSetDesc <T>(string key, long fromRank, long toRank) where T : class, new() { return(ConvertListObj <T>(QuickHelperBase.ZRevRange(key, fromRank, toRank).ToList())); }
/// <summary> /// 出栈 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public async Task <T> ListLeftPopAsync <T>(string key) where T : class, new() { return(ConvertObj <T>(await QuickHelperBase.LPopAsync(key))); }
/// <summary> /// 获取集合中的数量 /// </summary> /// <param name="key"></param> /// <returns></returns> public async Task <long> ListLengthAsync(string key) { return(await QuickHelperBase.LLenAsync(key)); }
/// <summary> /// 保存单个key value /// </summary> /// <param name="key">Redis Key</param> /// <param name="value">保存的值</param> /// <param name="expiry">过期时间</param> /// <returns></returns> public bool StringSet(string key, string value, TimeSpan?expiry = default(TimeSpan?)) { return(QuickHelperBase.Set(key, value, expiry.HasValue ? Convert.ToInt32(expiry.Value.TotalSeconds) : -1)); }
/// <summary> /// 出栈 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public async Task <string> ListLeftPopAsync(string key) { return(await QuickHelperBase.LPopAsync(key)); }
/// <summary> /// 出栈 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T ListLeftPop <T>(string key) where T : class, new() { return(ConvertObj <T>(QuickHelperBase.LPop(key))); }