ServiceStackRedisHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Configuration;
  3. namespace EasyModbusClient.redis
  4. {
  5. class ServiceStackRedisHelper
  6. {
  7. static string ConncetionRedisString = ConfigurationManager.AppSettings["ConncetionRedisString"];
  8. // public static ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient(ConncetionRedisString);
  9. /// <summary>
  10. /// 把值存放到redis里边取
  11. /// </summary>
  12. /// <param name="key"></param>
  13. /// <param name="value"></param>
  14. public static void SetValue(string pkey, string pvalue)
  15. {
  16. using (ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient(ConncetionRedisString))
  17. {
  18. try
  19. {
  20. client.Db = 2;
  21. client.Set<string>(pkey, pvalue);
  22. // YG.Log.Instance.WriteLogAdd($"redis->>{pkey}-->{pvalue}");
  23. }
  24. catch (Exception ex)
  25. {
  26. YG.Log.Instance.WriteLogAdd($"25-->{pkey}-->{pvalue}-->{ex.Message}");
  27. }
  28. }
  29. }
  30. public static void SetValue(string pkey, string pvalue,string redisstring)
  31. {
  32. using (ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient(redisstring))
  33. {
  34. try
  35. {
  36. client.Set<string>(pkey, pvalue);
  37. // YG.Log.Instance.WriteLogAdd($"redis->>{pkey}-->{pvalue}");
  38. }
  39. catch (Exception ex)
  40. {
  41. YG.Log.Instance.WriteLogAdd($"25-->{pkey}-->{pvalue}-->{ex.Message}");
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// 根据key获取redis里边的内容
  47. /// </summary>
  48. /// <param name="key"></param>
  49. /// <returns></returns>
  50. public static string GetValue(string key)
  51. {
  52. string value = "";
  53. using (ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient(ConncetionRedisString))
  54. {
  55. if (!string.IsNullOrWhiteSpace(key))
  56. {
  57. try
  58. {
  59. value = client.GetValue(key);
  60. }
  61. catch (Exception ex)
  62. {
  63. YG.Log.Instance.WriteLogAdd($"37-->{key}-->{ex.Message}-->{value}");
  64. }
  65. }
  66. }
  67. return value;
  68. }
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. /// <param name="key"></param>
  73. /// <param name="redisString">123456@192.168.170.203:6379</param>
  74. /// <returns></returns>
  75. public static string GetValue(string key, string redisString)
  76. {
  77. string value = "";
  78. using (ServiceStack.Redis.RedisClient client = new ServiceStack.Redis.RedisClient(redisString))
  79. {
  80. if (!string.IsNullOrWhiteSpace(key))
  81. {
  82. try
  83. {
  84. value = client.GetValue(key);
  85. }
  86. catch (Exception ex)
  87. {
  88. YG.Log.Instance.WriteLogAdd($"37-->{key}-->{ex.Message}-->{redisString}");
  89. }
  90. }
  91. }
  92. return value;
  93. }
  94. }
  95. }