RedisExample.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Microsoft.AspNetCore.Mvc.Formatters;
  2. using StackExchange.Redis;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Text;
  9. namespace MS.WebCore.Core
  10. {
  11. public class RedisExample
  12. {
  13. private static ConnectionMultiplexer redisConnection;
  14. private static IDatabase redisDb;
  15. //string connectionString = "localhost,password=DFS2024@,db=0,connectTimeout=5000";
  16. //string connectionString = "localhost,password=DFS2024@";
  17. public RedisExample(int dbIndex = 0)
  18. {
  19. //string connectionString = $"localhost,db={dbIndex},connectTimeout=5000";
  20. //redisConnection = ConnectionMultiplexer.Connect(connectionString);
  21. redisConnection = ConnectionMultiplexer.Connect("localhost");
  22. redisDb = redisConnection.GetDatabase();
  23. }
  24. public void SetData(string key, string value)
  25. {
  26. redisDb.StringSet(key, value);
  27. }
  28. public string GetData(string key)
  29. {
  30. return redisDb.StringGet(key);
  31. }
  32. public void DeleteData(string key, string field)
  33. {
  34. redisDb.HashDelete(key, field);
  35. //redisDb.KeyDelete(key);
  36. }
  37. public void DeleteData(string key)
  38. {
  39. redisDb.KeyDelete(key);
  40. //redisDb.KeyDelete(key);
  41. }
  42. public bool HashKeyExists(string hashKey, string fieldKey)
  43. {
  44. return redisDb.HashExists(hashKey, fieldKey);
  45. }
  46. public void SetDataWithTime(string key, string value, DateTime time)
  47. {
  48. redisDb.SortedSetAdd("mySortedSet", value, time.Ticks);
  49. }
  50. public string GetValueByTime(DateTime time)
  51. {
  52. var result = redisDb.SortedSetRangeByScore("mySortedSet", time.Ticks, time.Ticks);
  53. return result.FirstOrDefault();
  54. }
  55. public void AddToHash(string key, string field, string value)
  56. {
  57. RedisType keyType = redisDb.KeyType(key);
  58. if (keyType != RedisType.None && keyType != RedisType.Hash)
  59. {
  60. // throw new Exception("Key is not of type Hash");
  61. }
  62. redisDb.HashSet(key, field, value);
  63. }
  64. //public List<string> GetListValues()
  65. //{
  66. // var values = redisDb.ListRange("myList");
  67. // List<string> result = new List<string>();
  68. // foreach (var value in values)
  69. // {
  70. // result.Add(value.ToString());
  71. // }
  72. // return result;
  73. //}
  74. public List<string> GetValuesByUUID(string uuid)
  75. {
  76. var values = redisDb.ListRange("myList");
  77. List<string> result = new List<string>();
  78. foreach (var value in values)
  79. {
  80. var parts = value.ToString().Split(new[] { ' ' }, 2);
  81. var storedUUID = parts[0];
  82. if (storedUUID == uuid)
  83. {
  84. result.Add(value.ToString());
  85. }
  86. }
  87. return result;
  88. }
  89. public List<KeyValuePair<string, string>> GetListValues()
  90. {
  91. var values = redisDb.HashGetAll("myList");
  92. List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
  93. foreach (var value in values)
  94. {
  95. result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
  96. }
  97. return result;
  98. }
  99. public List<KeyValuePair<string, string>> GetValuesByKey(string key)
  100. {
  101. var values = redisDb.HashGetAll("myList");
  102. List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
  103. foreach (var value in values)
  104. {
  105. if (value.Name == key)
  106. {
  107. result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
  108. }
  109. }
  110. return result;
  111. }
  112. public List<string> GetListFromRedis(string listKey)
  113. {
  114. var redisValues = redisDb.ListRange(listKey);
  115. return redisValues.Select(value => value.ToString()).ToList();
  116. }
  117. public void AddToList(string key, string value)
  118. {
  119. RedisType keyType = redisDb.KeyType(key);
  120. if (keyType != RedisType.None && keyType != RedisType.Hash)
  121. {
  122. //
  123. //throw new Exception("Key is not of type Hash");
  124. }
  125. redisDb.HashSet(key, value, value);
  126. }
  127. public List<KeyValuePair<string, string>> GetListValues(string key)
  128. {
  129. RedisType keyType = redisDb.KeyType(key);
  130. if (keyType != RedisType.Hash)
  131. {
  132. // throw new Exception("Key is not of type Hash");
  133. }
  134. var values = redisDb.HashGetAll(key);
  135. List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
  136. foreach (var value in values)
  137. {
  138. result.Add(new KeyValuePair<string, string>(value.Name, value.Value));
  139. }
  140. return result;
  141. }
  142. public List<string> GetHashKeysByPattern(string pattern)
  143. {
  144. List<string> keys = new List<string>();
  145. // 使用服务器的 EndPoint 获取服务器的键
  146. EndPoint[] endPoints = redisDb.Multiplexer.GetEndPoints();
  147. foreach (EndPoint endPoint in endPoints)
  148. {
  149. IServer server = redisDb.Multiplexer.GetServer(endPoint);
  150. keys.AddRange(server.Keys(pattern: pattern, database: redisDb.Database).Select(key => (string)key));
  151. }
  152. return keys;
  153. }
  154. public bool SetHashField(string hashKey, string fieldKey, string value)
  155. {
  156. // 获取数据库(默认是0)
  157. // 使用HashSet方法来更改哈希中字段的值
  158. // 注意:HashSet方法返回bool值,表示操作是否成功
  159. return redisDb.HashSet(hashKey, fieldKey, value);
  160. }
  161. }
  162. }