VolatileCache.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace Quadarax.Foundation.Common.Cache
  5. {
  6. public class VolatileCache<TKey, TValue> : Cache<TKey, TValue>
  7. {
  8. private ConcurrentDictionary<TKey, TValue> cache = new ConcurrentDictionary<TKey, TValue>();
  9. #region *** Properties ***
  10. public int Count
  11. {
  12. get
  13. {
  14. return this.cache.Count;
  15. }
  16. }
  17. public TKey[] Keys
  18. {
  19. get
  20. {
  21. ICollection<TKey> keys = this.cache.Keys;
  22. TKey[] array = new TKey[keys.Count];
  23. keys.CopyTo(array, 0);
  24. return array;
  25. }
  26. }
  27. public TValue[] Values
  28. {
  29. get
  30. {
  31. ICollection<TValue> values = this.cache.Values;
  32. TValue[] array = new TValue[values.Count];
  33. values.CopyTo(array, 0);
  34. return array;
  35. }
  36. }
  37. #endregion
  38. #region *** Constructors ***
  39. public VolatileCache()
  40. {
  41. this.Enabled = true;
  42. }
  43. #endregion
  44. #region *** Overrides ***
  45. protected override TValue GetValue(TKey key)
  46. {
  47. TValue v;
  48. if (this.cache.TryGetValue(key, out v))
  49. return v;
  50. return default(TValue);
  51. }
  52. protected override void SetValue(TKey key, TValue value)
  53. {
  54. this.cache[key] = value;
  55. }
  56. protected override bool RemoveByKey(TKey key)
  57. {
  58. TValue v;
  59. return this.cache.TryRemove(key, out v);
  60. }
  61. public override bool Invalidate(TKey key)
  62. {
  63. return this.Remove(key);
  64. }
  65. #endregion
  66. public bool ContainsKey(TKey key)
  67. {
  68. return this.cache.ContainsKey(key);
  69. }
  70. public bool FindAndAddValue(TKey key, TValue value)
  71. {
  72. return this.cache.TryAdd(key, value);
  73. }
  74. public bool Remove(TKey key, out TValue value)
  75. {
  76. value = default(TValue);
  77. if (!this.Enabled)
  78. return false;
  79. return this.cache.TryRemove(key, out value);
  80. }
  81. public void Clear()
  82. {
  83. this.cache = new ConcurrentDictionary<TKey, TValue>();
  84. }
  85. public bool TryGetValue(TKey key, out TValue retval)
  86. {
  87. return this.cache.TryGetValue(key, out retval);
  88. }
  89. public bool TryGetValueOrFetchDummyValue(TKey key, Func<TValue> fetchDummyValueDelegate, out TValue retval)
  90. {
  91. bool flag = this.TryGetValue(key, out retval);
  92. if (!flag && fetchDummyValueDelegate != null)
  93. {
  94. flag = this.cache.TryGetValue(key, out retval);
  95. if (!flag)
  96. {
  97. retval = fetchDummyValueDelegate();
  98. this.cache[key] = retval;
  99. }
  100. }
  101. return flag;
  102. }
  103. public TValue[] GetAllValuesAndClear()
  104. {
  105. ICollection<TValue> values = this.cache.Values;
  106. TValue[] array = new TValue[values.Count];
  107. values.CopyTo(array, 0);
  108. this.Clear();
  109. return array;
  110. }
  111. }
  112. }