终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FRedisClient.cs 5.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. int dbi = 0;
  50. public void Connect(string connection)
  51. {
  52. _connection = ConnectionMultiplexer.Connect(ConfigurationOptions.Parse(connection));
  53. if (connection.Contains("defaultDatabase="))
  54. {
  55. string[] str=connection.Split(',');
  56. string stro = str.ToList().Find(s => s.Contains("defaultDatabase="));
  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(dbi);//默认使用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. foreach (var key in server.Keys(dbi,"*"))
  93. {
  94. //获取key对于的值
  95. var val = _database.StringGet(key);
  96. Console.WriteLine($"key: {key}, value: {val}");
  97. keys[key] = val;
  98. }
  99. }
  100. return keys;
  101. }
  102. /// <summary>
  103. /// 订阅通道消息
  104. /// </summary>
  105. public void SubscribeChanne(string channelname)
  106. {
  107. if (subscibe == null) return;
  108. subscibe.Subscribe(channelname,(channel,message) =>
  109. {
  110. MessageLog(channel,message);
  111. });
  112. }
  113. /// <summary>
  114. /// 发布通道消息
  115. /// </summary>
  116. public void PublishChanne(string channelname,string value)
  117. {
  118. if (subscibe == null) return;
  119. subscibe.Publish(channelname,value);
  120. }
  121. /// <summary>
  122. /// 获取 key 值
  123. /// </summary>
  124. public RedisValue RedisGet(string key,string hashField = "")
  125. {
  126. if (_database == null) return new RedisValue();
  127. RedisValue result;
  128. if (string.IsNullOrEmpty(hashField))
  129. {
  130. result = _database.StringGet(key);
  131. }
  132. else
  133. {
  134. result = _database.HashGet(key,hashField);
  135. }
  136. return result;
  137. //MessageLog(key,result);
  138. }
  139. /// <summary>
  140. /// 设置 redis 的值
  141. /// </summary>
  142. public bool RedisSet(string key,string hashField,string value)
  143. {
  144. bool result;
  145. if (string.IsNullOrEmpty(hashField))
  146. {
  147. result = _database.StringSet(key,value);
  148. }
  149. else
  150. {
  151. result = _database.HashSet(key,hashField,value);
  152. }
  153. return result;
  154. }
  155. /// <summary>
  156. /// 消息打印
  157. /// </summary>
  158. private void MessageLog(string key,string msg)
  159. {
  160. if (LogMeaage != null)
  161. {
  162. LogMeaage.Invoke(key,msg);
  163. }
  164. }
  165. }
  166. }