thread_safe_cache.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include "cache.h"
  3. #include <util/generic/singleton.h>
  4. #include <util/system/rwlock.h>
  5. namespace NPrivate {
  6. // We are interested in getters promotion policy _here_ because of Read-Write-Lock optimizations.
  7. enum class EGettersPromotionPolicy {
  8. Promoted, // LRU, TLRU, MRU, etc.
  9. Unpromoted // FIFO, LIFO, LW, etc.
  10. };
  11. template <class Key, class Value, template <class, class> class List, EGettersPromotionPolicy GettersPromotionPolicy, class... TArgs>
  12. class TThreadSafeCache {
  13. public:
  14. using TPtr = TAtomicSharedPtr<Value>;
  15. class ICallbacks {
  16. public:
  17. using TKey = Key;
  18. using TValue = Value;
  19. using TOwner = TThreadSafeCache<Key, Value, List, GettersPromotionPolicy, TArgs...>;
  20. public:
  21. virtual ~ICallbacks() = default;
  22. virtual TKey GetKey(TArgs... args) const = 0;
  23. virtual TValue* CreateObject(TArgs... args) const = 0;
  24. };
  25. public:
  26. TThreadSafeCache(const ICallbacks& callbacks, size_t maxSize = Max<size_t>())
  27. : Callbacks(callbacks)
  28. , Cache(maxSize)
  29. {
  30. }
  31. bool Insert(const Key& key, const TPtr& value) {
  32. if (!Contains(key)) {
  33. TWriteGuard w(Mutex);
  34. return Cache.Insert(key, value);
  35. }
  36. return false;
  37. }
  38. void Update(const Key& key, const TPtr& value) {
  39. TWriteGuard w(Mutex);
  40. Cache.Update(key, value);
  41. }
  42. const TPtr Get(TArgs... args) const {
  43. return GetValue<true>(args...);
  44. }
  45. const TPtr GetUnsafe(TArgs... args) const {
  46. return GetValue<false>(args...);
  47. }
  48. void Clear() {
  49. TWriteGuard w(Mutex);
  50. Cache.Clear();
  51. }
  52. void Erase(TArgs... args) {
  53. Key key = Callbacks.GetKey(args...);
  54. if (!Contains(key)) {
  55. return;
  56. }
  57. TWriteGuard w(Mutex);
  58. typename TInternalCache::TIterator i = Cache.Find(key);
  59. if (i == Cache.End()) {
  60. return;
  61. }
  62. Cache.Erase(i);
  63. }
  64. bool Contains(const Key& key) const {
  65. TReadGuard r(Mutex);
  66. auto iter = Cache.FindWithoutPromote(key);
  67. return iter != Cache.End();
  68. }
  69. template <class TCallbacks>
  70. static const TPtr Get(TArgs... args) {
  71. return TThreadSafeCacheSingleton<TCallbacks>::Get(args...);
  72. }
  73. template <class TCallbacks>
  74. static const TPtr Erase(TArgs... args) {
  75. return TThreadSafeCacheSingleton<TCallbacks>::Erase(args...);
  76. }
  77. template <class TCallbacks>
  78. static void Clear() {
  79. return TThreadSafeCacheSingleton<TCallbacks>::Clear();
  80. }
  81. size_t GetMaxSize() const {
  82. TReadGuard w(Mutex);
  83. return Cache.GetMaxSize();
  84. }
  85. void SetMaxSize(size_t newSize) {
  86. TWriteGuard w(Mutex);
  87. Cache.SetMaxSize(newSize);
  88. }
  89. private:
  90. template <bool AllowNullValues>
  91. const TPtr GetValue(TArgs... args) const {
  92. Key key = Callbacks.GetKey(args...);
  93. switch (GettersPromotionPolicy) {
  94. case EGettersPromotionPolicy::Promoted:
  95. break;
  96. case EGettersPromotionPolicy::Unpromoted: {
  97. TReadGuard r(Mutex);
  98. typename TInternalCache::TIterator i = Cache.FindWithoutPromote(key);
  99. if (i != Cache.End()) {
  100. return i.Value();
  101. }
  102. break;
  103. }
  104. }
  105. TWriteGuard w(Mutex);
  106. typename TInternalCache::TIterator i = Cache.Find(key);
  107. if (i != Cache.End()) {
  108. return i.Value();
  109. }
  110. TPtr value = Callbacks.CreateObject(args...);
  111. if (value || AllowNullValues) {
  112. Cache.Insert(key, value);
  113. }
  114. return value;
  115. }
  116. private:
  117. using TInternalCache = TCache<Key, TPtr, List<Key, TPtr>, TNoopDelete>;
  118. template <class TCallbacks>
  119. class TThreadSafeCacheSingleton {
  120. public:
  121. static const TPtr Get(TArgs... args) {
  122. return Singleton<TThreadSafeCacheSingleton>()->Cache.Get(args...);
  123. }
  124. static const TPtr Erase(TArgs... args) {
  125. return Singleton<TThreadSafeCacheSingleton>()->Cache.Erase(args...);
  126. }
  127. static void Clear() {
  128. return Singleton<TThreadSafeCacheSingleton>()->Cache.Clear();
  129. }
  130. TThreadSafeCacheSingleton()
  131. : Cache(Callbacks)
  132. {
  133. }
  134. private:
  135. TCallbacks Callbacks;
  136. typename TCallbacks::TOwner Cache;
  137. };
  138. private:
  139. TRWMutex Mutex;
  140. const ICallbacks& Callbacks;
  141. mutable TInternalCache Cache;
  142. };
  143. struct TLWHelper {
  144. template <class TValue>
  145. struct TConstWeighter {
  146. static int Weight(const TValue& /*value*/) {
  147. return 0;
  148. }
  149. };
  150. template <class TKey, class TValue>
  151. using TListType = TLWList<TKey, TValue, int, TConstWeighter<TValue>>;
  152. template <class TKey, class TValue, class... TArgs>
  153. using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Unpromoted, TArgs...>;
  154. };
  155. struct TLRUHelper {
  156. template <class TKey, class TValue>
  157. using TListType = TLRUList<TKey, TValue>;
  158. template <class TKey, class TValue, class... TArgs>
  159. using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>;
  160. };
  161. }
  162. template <class TKey, class TValue, class... TArgs>
  163. using TThreadSafeCache = typename NPrivate::TLWHelper::template TCache<TKey, TValue, TArgs...>;
  164. template <class TKey, class TValue, class... TArgs>
  165. using TThreadSafeLRUCache = typename NPrivate::TLRUHelper::template TCache<TKey, TValue, TArgs...>;