MemoryCacheImp.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Caching.Memory;
  6. using YiSha.Cache.Interface;
  7. using YiSha.Util;
  8. namespace YiSha.MemoryCache
  9. {
  10. public class MemoryCacheImp : ICache
  11. {
  12. private IMemoryCache cache = GlobalContext.ServiceProvider.GetService<IMemoryCache>();
  13. public bool SetCache<T>(string key, T value, DateTime? expireTime = null)
  14. {
  15. try
  16. {
  17. if (expireTime == null)
  18. {
  19. return cache.Set<T>(key, value) != null;
  20. }
  21. else
  22. {
  23. return cache.Set(key, value, (expireTime.Value - DateTime.Now)) != null;
  24. }
  25. }
  26. catch (Exception ex)
  27. {
  28. LogHelper.Error(ex);
  29. }
  30. return false;
  31. }
  32. public bool RemoveCache(string key)
  33. {
  34. cache.Remove(key);
  35. return true;
  36. }
  37. public T GetCache<T>(string key)
  38. {
  39. var value = cache.Get<T>(key);
  40. return value;
  41. }
  42. #region Hash
  43. public int SetHashFieldCache<T>(string key, string fieldKey, T fieldValue)
  44. {
  45. return SetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, fieldValue } });
  46. }
  47. public int SetHashFieldCache<T>(string key, Dictionary<string, T> dict)
  48. {
  49. int count = 0;
  50. foreach (string fieldKey in dict.Keys)
  51. {
  52. count += cache.Set(key, dict) != null ? 1 : 0;
  53. }
  54. return count;
  55. }
  56. public T GetHashFieldCache<T>(string key, string fieldKey)
  57. {
  58. var dict = GetHashFieldCache<T>(key, new Dictionary<string, T> { { fieldKey, default(T) } });
  59. return dict[fieldKey];
  60. }
  61. public Dictionary<string, T> GetHashFieldCache<T>(string key, Dictionary<string, T> dict)
  62. {
  63. var hashFields = cache.Get<Dictionary<string, T>>(key);
  64. foreach (KeyValuePair<string, T> keyValuePair in hashFields.Where(p => dict.Keys.Contains(p.Key)))
  65. {
  66. dict[keyValuePair.Key] = keyValuePair.Value;
  67. }
  68. return dict;
  69. }
  70. public Dictionary<string, T> GetHashCache<T>(string key)
  71. {
  72. Dictionary<string, T> dict = new Dictionary<string, T>();
  73. var hashFields = cache.Get<Dictionary<string, T>>(key);
  74. foreach (string field in hashFields.Keys)
  75. {
  76. dict[field] = hashFields[field];
  77. }
  78. return dict;
  79. }
  80. public List<T> GetHashToListCache<T>(string key)
  81. {
  82. List<T> list = new List<T>();
  83. var hashFields = cache.Get<Dictionary<string, T>>(key);
  84. foreach (string field in hashFields.Keys)
  85. {
  86. list.Add(hashFields[field]);
  87. }
  88. return list;
  89. }
  90. public bool RemoveHashFieldCache(string key, string fieldKey)
  91. {
  92. Dictionary<string, bool> dict = new Dictionary<string, bool> { { fieldKey, false } };
  93. dict = RemoveHashFieldCache(key, dict);
  94. return dict[fieldKey];
  95. }
  96. public Dictionary<string, bool> RemoveHashFieldCache(string key, Dictionary<string, bool> dict)
  97. {
  98. var hashFields = cache.Get<Dictionary<string, object>>(key);
  99. foreach (string fieldKey in dict.Keys)
  100. {
  101. dict[fieldKey] = hashFields.Remove(fieldKey);
  102. }
  103. return dict;
  104. }
  105. #endregion
  106. }
  107. }