RedisService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using FreeRedis;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace WMS.BZServices
  9. {
  10. public interface IRedisService
  11. {
  12. int DistributeLock(string lockKey, int timeoutSeconds = 30, bool autoDelay = false);
  13. string Get(string key = "key");
  14. T Get<T>(string key = "key");
  15. long Publish(string channel, string message);
  16. bool Set(string key, string value, TimeSpan timeSpan);
  17. bool Set<T>(string key, T value, TimeSpan timeSpan);
  18. void Del(string key);
  19. }
  20. public class RedisService : IRedisService
  21. {
  22. private readonly RedisClient _redisClient;
  23. private static int _tempInt = 0;
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <param name="logger"></param>
  28. /// <param name="redisIdleBus"></param>
  29. public RedisService( string redisconnect)
  30. {
  31. _redisClient = new RedisClient(redisconnect);
  32. _redisClient.Serialize = obj => JsonConvert.SerializeObject(obj); ;// JsonConvert.SerializeObject(obj);
  33. _redisClient.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);// JsonConvert.DeserializeObject(json, type);
  34. }
  35. /// <summary>
  36. /// Redis插入
  37. /// </summary>
  38. /// <param name="key"></param>
  39. /// <param name="value"></param>
  40. /// <param name="timeSpan"></param>
  41. /// <returns></returns>
  42. public bool Set(string key, string value, TimeSpan timeSpan)
  43. {
  44. _redisClient.Set(key, value, timeSpan);
  45. return true;
  46. }
  47. public bool Set<T>(string key, T value, TimeSpan timeSpan)
  48. {
  49. _redisClient.Set<T>(key, value, timeSpan);
  50. return true;
  51. }
  52. /// <summary>
  53. /// 通过key或者Redis值
  54. /// </summary>
  55. /// <param name="key"></param>
  56. /// <returns></returns>
  57. public string Get(string key)
  58. {
  59. //可随时改用原生对象操作redis
  60. var value = _redisClient.Get<string>(key);
  61. return value;
  62. }
  63. public T Get<T>(string key)
  64. {
  65. //可随时改用原生对象操作redis
  66. var value = _redisClient.Get<T>(key);
  67. return value;
  68. }
  69. public void Del(string key)
  70. {
  71. _redisClient.Del(key);
  72. }
  73. /// <summary>
  74. /// 消息发布
  75. /// </summary>
  76. /// <param name="channel"></param>
  77. /// <param name="message"></param>
  78. /// <returns></returns>
  79. public long Publish(string channel, string message)
  80. {
  81. var value = _redisClient.Publish(channel, message);
  82. return value;
  83. }
  84. /// <summary>
  85. /// 分布式锁
  86. /// </summary>
  87. /// <param name="lockKey">指定锁的key</param>
  88. /// <param name="timeoutSeconds">超时秒数,超过时间自动释放锁</param>
  89. /// <param name="autoDelay">是否需要自动延时,true:自动延长过期时间false:不延长过期时间,以设置的过期时间为准</param>
  90. /// <returns></returns>
  91. public int DistributeLock(string lockKey, int timeoutSeconds = 30, bool autoDelay = false)
  92. {
  93. //通过别名为1的redis库进行分布式锁
  94. using (_redisClient.Lock(lockKey, timeoutSeconds, autoDelay))
  95. {
  96. //被锁住的逻辑
  97. return _tempInt;
  98. }
  99. }
  100. }
  101. }