Redis SortedSet有序集合

#region SortedSet

/// <summary>
/// 新增
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expireTime">超时时间</param>
/// <returns></returns>
public static bool SortedSetAdd(string key, object value, double score, DateTime? expireTime = null) {
string json = JsonConvert.SerializeObject(value);
var result = SortedSetAdd(key, json, score, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));
return result;
}

public static long SortedSetAdd<T>(string key, List<T> list, Func<T, dynamic> getSorts, DateTime? expireTime = null) {
long result = 0;
try {
List<SortedSetEntry> listSortedSet = new List<SortedSetEntry>();
foreach (var item in list) {
listSortedSet.Add(new SortedSetEntry(JsonConvert.SerializeObject(item), double.Parse(getSorts(item))));
}
result = _db.SortedSetAdd(key, listSortedSet.ToArray());
_db.KeyExpire(key, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));
return result;
}
catch (Exception) {

throw;
}
}
/// <summary>
/// 获取Sorted所有的值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static List<T> SortedGetAll<T>(string key) {
try {
List<T> result = new List<T>();
SortedSetEntry[] arr = _db.SortedSetRangeByScoreWithScores(key);
foreach (var item in arr) {
if (item.Element.HasValue) {
string element = item.Element.ToString();
result.Add(JsonConvert.DeserializeObject<T>(element));
}
}
return result;
}
catch (Exception ex) {
throw;
}
}
public static bool SetRemove(string key, double start, double stop) {
var flag = _db.SortedSetRemoveRangeByScore(key, start, stop).ToString();
if (flag == "the number of elements removed") {
return true;
}
return false;
}
/// <summary>
/// 返回SortedSet,默认情况下从低到高。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <param name="order"></param>
/// <returns></returns>
public static List<T> SortedSetRangeByRank<T>(string key, long start = 0, long stop = -1, Order order = Order.Ascending) {
try {
var result = new List<T>();
RedisValue[] value = _db.SortedSetRangeByRank(key, start, stop, order);
foreach (var item in value) {
if (!item.IsNullOrEmpty) {
result.Add(JsonConvert.DeserializeObject<T>(item));
}
}
return result;
}
catch (Exception) {
throw;
}
}
/// <summary>
/// 返回返回SortedSet元素个数
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static long SortedSetLength(string key) {
return _db.SortedSetLength(key);
}

/// <summary>
/// 移除SortedSet元素
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SortedSetRemove(string key, string value) {
return _db.SortedSetRemove(key, value);
}

/// <summary>
/// 根据范围移除SortedSet
/// </summary>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
public static bool SortedSetRemoveRangeByScore(string key, double start, double stop) {
var flag = _db.SortedSetRemoveRangeByScore(key, start, stop).ToString();
if (flag == "the number of elements removed")
return true;
return false;
}

#endregion

posted @ 2019-09-30 16:53  艺洁  阅读(545)  评论(0编辑  收藏  举报