You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.1 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Linq.Expressions;
  7. namespace SqlSugar
  8. {
  9. public class ReflectionInoCacheService : ICacheService
  10. {
  11. public void Add<V>(string key, V value)
  12. {
  13. ReflectionInoCore<V>.GetInstance().Add(key, value);
  14. }
  15. public void Add<V>(string key, V value, int cacheDurationInSeconds)
  16. {
  17. ReflectionInoCore<V>.GetInstance().Add(key, value, cacheDurationInSeconds);
  18. }
  19. public bool ContainsKey<V>(string key)
  20. {
  21. return ReflectionInoCore<V>.GetInstance().ContainsKey(key);
  22. }
  23. public V Get<V>(string key)
  24. {
  25. return ReflectionInoCore<V>.GetInstance().Get(key);
  26. }
  27. public IEnumerable<string> GetAllKey<V>()
  28. {
  29. return ReflectionInoCore<V>.GetInstance().GetAllKey();
  30. }
  31. public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
  32. {
  33. return ReflectionInoCore<V>.GetInstance().GetOrCreate(cacheKey, create);
  34. }
  35. public void Remove<V>(string key)
  36. {
  37. ReflectionInoCore<V>.GetInstance().Remove(key);
  38. }
  39. }
  40. public class ReflectionInoCore<V>
  41. {
  42. private readonly System.Collections.Concurrent.ConcurrentDictionary<string, V> InstanceCache = new System.Collections.Concurrent.ConcurrentDictionary<string, V>();
  43. private static ReflectionInoCore<V> _instance = null;
  44. private static readonly object _instanceLock = new object();
  45. private ReflectionInoCore()
  46. {
  47. }
  48. public V this[string key]
  49. {
  50. get
  51. {
  52. return this.Get(key);
  53. }
  54. }
  55. public bool ContainsKey(string key)
  56. {
  57. return this.InstanceCache.ContainsKey(key);
  58. }
  59. public V Get(string key)
  60. {
  61. if (this.ContainsKey(key))
  62. return this.InstanceCache[key];
  63. else
  64. return default(V);
  65. }
  66. public static ReflectionInoCore<V> GetInstance()
  67. {
  68. if (_instance == null)
  69. lock (_instanceLock)
  70. if (_instance == null)
  71. {
  72. _instance = new ReflectionInoCore<V>();
  73. Action addItem = () => { ReflectionInoCore<V>.GetInstance().RemoveAllCache(); };
  74. ReflectionInoHelper.AddRemoveFunc(addItem);
  75. }
  76. return _instance;
  77. }
  78. public void Add(string key, V value)
  79. {
  80. this.InstanceCache.GetOrAdd(key, value);
  81. }
  82. public void Add(string key, V value, int cacheDurationInSeconds)
  83. {
  84. Check.ThrowNotSupportedException("ReflectionInoCache.Add(string key, V value, int cacheDurationInSeconds)");
  85. }
  86. public void Remove(string key)
  87. {
  88. V val;
  89. this.InstanceCache.TryRemove(key, out val);
  90. }
  91. public void RemoveAllCache()
  92. {
  93. foreach (var key in GetAllKey())
  94. {
  95. this.Remove(key);
  96. }
  97. }
  98. public IEnumerable<string> GetAllKey()
  99. {
  100. return this.InstanceCache.Keys;
  101. }
  102. public V GetOrCreate(string cacheKey, Func<V> create)
  103. {
  104. if (this.ContainsKey(cacheKey)) return Get(cacheKey);
  105. else
  106. {
  107. var reval = create();
  108. this.Add(cacheKey, reval);
  109. return reval;
  110. }
  111. }
  112. }
  113. internal static class ReflectionInoHelper
  114. {
  115. private static List<Action> removeActions = new List<Action>();
  116. internal static void AddRemoveFunc(Action removeAction)
  117. {
  118. removeActions.Add(removeAction);
  119. }
  120. public static void RemoveAllCache()
  121. {
  122. lock (removeActions)
  123. {
  124. foreach (var item in removeActions)
  125. {
  126. item();
  127. }
  128. }
  129. }
  130. }
  131. }