终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

171 рядки
5.0 KiB

  1. using StackExchange.Redis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.Compiler
  8. {
  9. public class FRedisClient
  10. {
  11. #region 单例模式
  12. //private static FRedisClient instance = null;
  13. //public static FRedisClient Instance()
  14. //{
  15. // if (instance == null) instance = new FRedisClient();
  16. // return instance;
  17. //}
  18. #endregion
  19. #region 变量
  20. /// <summary>
  21. /// IP地址
  22. /// </summary>
  23. public string redisconnection = "124.222.238.75:16000,password=123456";
  24. /// <summary>
  25. /// redis 连接状态
  26. /// </summary>
  27. public ConnectionMultiplexer _connection = null;
  28. /// <summary>
  29. /// 数据存储位置
  30. /// </summary>
  31. public IDatabase _database = null;
  32. /// <summary>
  33. /// 通道建立连接
  34. /// </summary>
  35. public ISubscriber subscibe = null;
  36. #endregion
  37. #region 外部访问
  38. /// <summary>
  39. /// 委托出去
  40. /// </summary>
  41. public Action<string,string> LogMeaage = null;
  42. #endregion
  43. public void Connect()
  44. {
  45. _connection = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(redisconnection));
  46. _database = _connection.GetDatabase(0);//默认使用db0
  47. subscibe = _connection.GetSubscriber();
  48. }
  49. public void Connect(string connection)
  50. {
  51. _connection = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(connection));
  52. if (connection.Contains("defaultDatabase="))
  53. {
  54. string[] str=connection.Split(',');
  55. string stro = str.ToList().Find(s => s.Contains("defaultDatabase="));
  56. int dbi = 0;
  57. try
  58. {
  59. dbi=int.Parse(stro.Replace("defaultDatabase=",""));
  60. }
  61. catch (Exception ex)
  62. {
  63. throw;
  64. }
  65. _database = _connection.GetDatabase(dbi);//默认使用db0
  66. }
  67. else
  68. {
  69. _database = _connection.GetDatabase();//默认使用db0
  70. }
  71. subscibe = _connection.GetSubscriber();
  72. }
  73. /// <summary>
  74. /// 获取设备列表
  75. /// </summary>
  76. /// <returns></returns>
  77. public Dictionary<string,string> GetKeys()
  78. {
  79. Dictionary<string,string> keys = new Dictionary<string,string>();
  80. foreach (var endPoint in _connection.GetEndPoints())
  81. {
  82. //获取指定服务器
  83. var server = _connection.GetServer(endPoint);
  84. //在指定服务器上使用 keys 或者 scan 命令来遍历key
  85. foreach (var key in server.Keys(0,"设备列表:*"))
  86. {
  87. //获取key对于的值
  88. var val = _database.StringGet(key);
  89. Console.WriteLine($"key: {key}, value: {val}");
  90. keys[key] = val;
  91. }
  92. }
  93. return keys;
  94. }
  95. /// <summary>
  96. /// 订阅通道消息
  97. /// </summary>
  98. public void SubscribeChanne(string channelname)
  99. {
  100. if (subscibe == null) return;
  101. subscibe.Subscribe(channelname,(channel,message) =>
  102. {
  103. MessageLog(channel,message);
  104. });
  105. }
  106. /// <summary>
  107. /// 发布通道消息
  108. /// </summary>
  109. public void PublishChanne(string channelname,string value)
  110. {
  111. if (subscibe == null) return;
  112. subscibe.Publish(channelname,value);
  113. }
  114. /// <summary>
  115. /// 获取 key 值
  116. /// </summary>
  117. public RedisValue RedisGet(string key,string hashField = "")
  118. {
  119. if (_database == null) return new RedisValue();
  120. RedisValue result;
  121. if (string.IsNullOrEmpty(hashField))
  122. {
  123. result = _database.StringGet(key);
  124. }
  125. else
  126. {
  127. result = _database.HashGet(key,hashField);
  128. }
  129. return result;
  130. //MessageLog(key,result);
  131. }
  132. /// <summary>
  133. /// 设置 redis 的值
  134. /// </summary>
  135. public bool RedisSet(string key,string hashField,string value)
  136. {
  137. bool result;
  138. if (string.IsNullOrEmpty(hashField))
  139. {
  140. result = _database.StringSet(key,value);
  141. }
  142. else
  143. {
  144. result = _database.HashSet(key,hashField,value);
  145. }
  146. return result;
  147. }
  148. /// <summary>
  149. /// 消息打印
  150. /// </summary>
  151. private void MessageLog(string key,string msg)
  152. {
  153. if (LogMeaage != null)
  154. {
  155. LogMeaage.Invoke(key,msg);
  156. }
  157. }
  158. }
  159. }