java 集合 strem操作使用
阿新 • • 發佈:2021-01-11
public class RedisHelper { static RedisClient client; static RedisHelper() { client = new RedisClient("127.0.0.1", 6379); } /// <summary> /// 清空資料庫快取 /// </summary> public static void FlushRedis() { client.FlushAll(); } /// <summary> /// 獲取redis所有key /// </summary> /// <returns></returns> public static List<string> GetAllKeys() { try { return client.GetAllKeys(); } catch (System.Exception) { throw; } } /// <summary> /// 判斷key是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool KeyExists(string key) { try { return client.Exists(key) == 1 ? true : false; } catch (System.Exception) { throw; } } /// <summary> /// 刪除key /// </summary> /// <param name="key"></param> public static void KeyDel(string key) { try { client.Del(key); } catch (System.Exception) { throw; } } /// <summary> /// 獲取key型別 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string KeyType(string key) { try { return client.Type(key); } catch (System.Exception) { throw; } } #region 雜湊集合 /// <summary> /// 新增值到雜湊集合 /// </summary> /// <param name="ID"></param> /// <param name="key"></param> /// <param name="value"></param> public static void AddHash(string ID,string key,string value) { try { client.SetEntryInHash(ID, key, value); } catch (System.Exception) { throw; } } /// <summary> /// 獲取雜湊集合的key /// </summary> /// <param name="ID"></param> /// <returns></returns> public static List<string> GetHashKeys(string ID) { try { return client.GetHashKeys(ID); } catch (System.Exception) { throw; } } /// <summary> /// 獲取雜湊集合的value /// </summary> /// <param name="ID"></param> /// <returns></returns> public static List<string> GetHashValues(string ID) { try { return client.GetHashValues(ID); } catch (System.Exception) { throw; } } /// <summary> /// 獲取雜湊集合指定ID、key的value /// </summary> /// <param name="ID"></param> /// <param name="key"></param> /// <returns></returns> public static string GetHashValue(string ID,string key) { try { return client.GetValueFromHash(ID, key); } catch (System.Exception) { throw; } } #endregion #region 無序集合 /// <summary> /// 新增值到資料集合 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void AddSet(string key,string value) { try { client.AddItemToSet(key, value); } catch { } } /// <summary> /// 獲取資料集合 /// </summary> /// <param name="key"></param> /// <returns></returns> public static HashSet<string> GetSet(string key) { try { return client.GetAllItemsFromSet(key); } catch { string[] strMsg = new string[0]; HashSet<string> hSetMsg = new HashSet<string>(strMsg); return hSetMsg; } } /// <summary> /// 獲取資料集合 /// </summary> /// <param name="type"></param> /// <param name="key1"></param> /// <param name="key2"></param> /// <returns></returns> public static HashSet<string> GetSet(string type,string key1,string key2) { try { HashSet<string> hSet = new HashSet<string>(); switch (type.ToLower()) { case "union"://並集 hSet = client.GetUnionFromSets(new string[] { key1, key2 }); break; case "intersect"://交集 hSet = client.GetIntersectFromSets(new string[] { key1, key2 }); break; case "except"://差集,存在於key1,不存在於key2 hSet = client.GetDifferencesFromSet(key1, new string[] { key2 }); break; default: break; } return hSet; } catch { string[] strMsg = new string[0]; HashSet<string> hSetMsg = new HashSet<string>(strMsg); return hSetMsg; } } #endregion #region 有序集合 /// <summary> /// 新增值到有序集合 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void AddZset(string key,string value,double score) { try { client.AddItemToSortedSet(key, value, score); } catch (System.Exception) { throw; } } /// <summary> /// 獲取有序集合 /// </summary> /// <param name="key"></param> /// <returns></returns> public static List<string> GetZset(string key) { try { return client.GetAllItemsFromSortedSet(key); } catch (System.Exception) { throw; } } /// <summary> /// 按score排序獲取前n位的有序集合 /// </summary> /// <param name="key"></param> /// <param name="i"></param> /// <returns></returns> public static List<string> GetZset(string key,int i) { try { var value = client.GetRangeWithScoresFromSortedSet(key, 0, i); List<string> llist = new List<string>(); foreach (var item in value) { llist.Add(item.Key); } return llist; } catch (System.Exception) { throw; } } #endregion #region 字串 /// <summary> /// 新增值到字串 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void AddString(string key,string value) { try { client.Set<string>(key, value); } catch (System.Exception) { throw; } } /// <summary> /// 獲取字串 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string GetString(string key) { try { return client.Get<string>(key); } catch (System.Exception) { throw; } } #endregion #region 列表堆疊 /// <summary> /// 新增值入堆:先進先出 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void AddListQueue(string key,string value) { try { client.EnqueueItemOnList(key, value); } catch (System.Exception) { throw; } } /// <summary> /// 新增值入棧:先進後出 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void AddListStack(string key,string value) { try { client.PushItemToList(key, value); } catch (System.Exception) { throw; } } /// <summary> /// 獲取列表 /// </summary> /// <param name="key"></param> /// <returns></returns> public static List<string> GetList(string key) { List<string> lList = new List<string>(); try { var p = client.GetListCount(key); for (int i = 0; i < p; i++) { lList.Add(client.PopItemFromList(key)); } return lList; } catch (System.Exception) { throw; } } #endregion }