CacheService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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. public V this[string key]
  47. {
  48. get
  49. {
  50. return this.Get(key);
  51. }
  52. }
  53. public bool ContainsKey(string key)
  54. {
  55. return this.InstanceCache.ContainsKey(key);
  56. }
  57. public V Get(string key)
  58. {
  59. if (this.ContainsKey(key))
  60. return this.InstanceCache[key];
  61. else
  62. return default(V);
  63. }
  64. public static ReflectionInoCore<V> GetInstance()
  65. {
  66. if (_instance == null)
  67. lock (_instanceLock)
  68. if (_instance == null)
  69. {
  70. _instance = new ReflectionInoCore<V>();
  71. Action addItem =()=> { ReflectionInoCore<V>.GetInstance().RemoveAllCache(); };
  72. ReflectionInoHelper.AddRemoveFunc(addItem);
  73. }
  74. return _instance;
  75. }
  76. public void Add(string key, V value)
  77. {
  78. this.InstanceCache.GetOrAdd(key, value);
  79. }
  80. public void Add(string key, V value, int cacheDurationInSeconds)
  81. {
  82. Check.ThrowNotSupportedException("ReflectionInoCache.Add(string key, V value, int cacheDurationInSeconds)");
  83. }
  84. public void Remove(string key)
  85. {
  86. V val;
  87. this.InstanceCache.TryRemove(key, out val);
  88. }
  89. public void RemoveAllCache()
  90. {
  91. foreach (var key in GetAllKey())
  92. {
  93. this.Remove(key);
  94. }
  95. }
  96. public IEnumerable<string> GetAllKey()
  97. {
  98. return this.InstanceCache.Keys;
  99. }
  100. public V GetOrCreate(string cacheKey, Func<V> create)
  101. {
  102. if (this.ContainsKey(cacheKey)) return Get(cacheKey);
  103. else
  104. {
  105. var reval = create();
  106. this.Add(cacheKey, reval);
  107. return reval;
  108. }
  109. }
  110. }
  111. internal static class ReflectionInoHelper
  112. {
  113. private static List<Action> removeActions = new List<Action>();
  114. internal static void AddRemoveFunc(Action removeAction)
  115. {
  116. removeActions.Add(removeAction);
  117. }
  118. public static void RemoveAllCache()
  119. {
  120. lock (removeActions)
  121. {
  122. foreach (var item in removeActions)
  123. {
  124. item();
  125. }
  126. }
  127. }
  128. }
  129. }