RedisCacheImp.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Newtonsoft.Json;
  5. using StackExchange.Redis;
  6. using YiSha.Cache.Interface;
  7. using YiSha.Util;
  8. namespace YiSha.RedisCache
  9. {
  10. public class RedisCacheImp : ICache
  11. {
  12. private IDatabase cache;
  13. private ConnectionMultiplexer connection;
  14. public RedisCacheImp()
  15. {
  16. connection = ConnectionMultiplexer.Connect(GlobalContext.SystemConfig.RedisConnectionString);
  17. cache = connection.GetDatabase();
  18. }
  19. public bool SetCache<T>(string key, T value, DateTime? expireTime = null)
  20. {
  21. try
  22. {
  23. var jsonOption = new JsonSerializerSettings()
  24. {
  25. ReferenceLoopHandling = ReferenceLoopHandling.Ignore
  26. };
  27. string strValue = JsonConvert.SerializeObject(value, jsonOption);
  28. if (string.IsNullOrEmpty(strValue))
  29. {
  30. return false;
  31. }
  32. if (expireTime == null)
  33. {
  34. return cache.StringSet(key, strValue);
  35. }
  36. else
  37. {
  38. return cache.StringSet(key, strValue, (expireTime.Value - DateTime.Now));
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. LogHelper.Error(ex);
  44. }
  45. return false;
  46. }
  47. public bool RemoveCache(string key)
  48. {
  49. return cache.KeyDelete(key);
  50. }
  51. public T GetCache<T>(string key)
  52. {
  53. var t = default(T);
  54. try
  55. {
  56. var value = cache.StringGet(key);
  57. if (string.IsNullOrEmpty(value))
  58. {
  59. return t;
  60. }
  61. t = JsonConvert.DeserializeObject<T>(value);
  62. }
  63. catch (Exception ex)
  64. {
  65. LogHelper.Error(ex);
  66. }
  67. return t;
  68. }
  69. #region Hash
  70. public int SetHashFieldCache<T>(string key, string fieldKey, T fieldValue)
  71. {
  72. return SetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, fieldValue } });
  73. }
  74. public int SetHashFieldCache<T>(string key, Dictionary<string, T> dict)
  75. {
  76. int count = 0;
  77. var jsonOption = new JsonSerializerSettings()
  78. {
  79. ReferenceLoopHandling = ReferenceLoopHandling.Ignore
  80. };
  81. foreach (string fieldKey in dict.Keys)
  82. {
  83. string fieldValue = JsonConvert.SerializeObject(dict[fieldKey], jsonOption);
  84. count += cache.HashSet(key, fieldKey, fieldValue) ? 1 : 0;
  85. }
  86. return count;
  87. }
  88. public T GetHashFieldCache<T>(string key, string fieldKey)
  89. {
  90. var dict = GetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, default(T) } });
  91. return dict[fieldKey];
  92. }
  93. public Dictionary<string, T> GetHashFieldCache<T>(string key, Dictionary<string, T> dict)
  94. {
  95. foreach (string fieldKey in dict.Keys)
  96. {
  97. string fieldValue = cache.HashGet(key, fieldKey);
  98. dict[fieldKey] = JsonConvert.DeserializeObject<T>(fieldValue);
  99. }
  100. return dict;
  101. }
  102. public Dictionary<string, T> GetHashCache<T>(string key)
  103. {
  104. Dictionary<string, T> dict = new Dictionary<string, T>();
  105. var hashFields = cache.HashGetAll(key);
  106. foreach (HashEntry field in hashFields)
  107. {
  108. dict[field.Name] = JsonConvert.DeserializeObject<T>(field.Value);
  109. }
  110. return dict;
  111. }
  112. public List<T> GetHashToListCache<T>(string key)
  113. {
  114. List<T> list = new List<T>();
  115. var hashFields = cache.HashGetAll(key);
  116. foreach (HashEntry field in hashFields)
  117. {
  118. list.Add(JsonConvert.DeserializeObject<T>(field.Value));
  119. }
  120. return list;
  121. }
  122. public bool RemoveHashFieldCache(string key, string fieldKey)
  123. {
  124. Dictionary<string, bool> dict = new Dictionary<string, bool> { { fieldKey, false } };
  125. dict = RemoveHashFieldCache(key, dict);
  126. return dict[fieldKey];
  127. }
  128. public Dictionary<string, bool> RemoveHashFieldCache(string key, Dictionary<string, bool> dict)
  129. {
  130. foreach (string fieldKey in dict.Keys)
  131. {
  132. dict[fieldKey] = cache.HashDelete(key, fieldKey);
  133. }
  134. return dict;
  135. }
  136. #endregion
  137. public void Dispose()
  138. {
  139. if (connection != null)
  140. {
  141. connection.Close();
  142. }
  143. GC.SuppressFinalize(this);
  144. }
  145. }
  146. }