thread_safe_cache.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Size() const {
  82. TReadGuard r(Mutex);
  83. return Cache.Size();
  84. }
  85. size_t TotalSize() const {
  86. TReadGuard r(Mutex);
  87. return Cache.TotalSize();
  88. }
  89. size_t GetMaxSize() const {
  90. TReadGuard w(Mutex);
  91. return Cache.GetMaxSize();
  92. }
  93. void SetMaxSize(size_t newSize) {
  94. TWriteGuard w(Mutex);
  95. Cache.SetMaxSize(newSize);
  96. }
  97. private:
  98. template <bool AllowNullValues>
  99. const TPtr GetValue(TArgs... args) const {
  100. Key key = Callbacks.GetKey(args...);
  101. switch (GettersPromotionPolicy) {
  102. case EGettersPromotionPolicy::Promoted:
  103. break;
  104. case EGettersPromotionPolicy::Unpromoted: {
  105. TReadGuard r(Mutex);
  106. typename TInternalCache::TIterator i = Cache.FindWithoutPromote(key);
  107. if (i != Cache.End()) {
  108. return i.Value();
  109. }
  110. break;
  111. }
  112. }
  113. TWriteGuard w(Mutex);
  114. typename TInternalCache::TIterator i = Cache.Find(key);
  115. if (i != Cache.End()) {
  116. return i.Value();
  117. }
  118. TPtr value = Callbacks.CreateObject(args...);
  119. if (value || AllowNullValues) {
  120. Cache.Insert(key, value);
  121. }
  122. return value;
  123. }
  124. private:
  125. using TInternalCache = TCache<Key, TPtr, List<Key, TPtr>, TNoopDelete>;
  126. template <class TCallbacks>
  127. class TThreadSafeCacheSingleton {
  128. public:
  129. static const TPtr Get(TArgs... args) {
  130. return Singleton<TThreadSafeCacheSingleton>()->Cache.Get(args...);
  131. }
  132. static const TPtr Erase(TArgs... args) {
  133. return Singleton<TThreadSafeCacheSingleton>()->Cache.Erase(args...);
  134. }
  135. static void Clear() {
  136. return Singleton<TThreadSafeCacheSingleton>()->Cache.Clear();
  137. }
  138. TThreadSafeCacheSingleton()
  139. : Cache(Callbacks)
  140. {
  141. }
  142. private:
  143. TCallbacks Callbacks;
  144. typename TCallbacks::TOwner Cache;
  145. };
  146. private:
  147. TRWMutex Mutex;
  148. const ICallbacks& Callbacks;
  149. mutable TInternalCache Cache;
  150. };
  151. struct TLWHelper {
  152. template <class TValue>
  153. struct TConstWeighter {
  154. static int Weight(const TValue& /*value*/) {
  155. return 0;
  156. }
  157. };
  158. template <class TKey, class TValue>
  159. using TListType = TLWList<TKey, TValue, int, TConstWeighter<TValue>>;
  160. template <class TKey, class TValue, class... TArgs>
  161. using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Unpromoted, TArgs...>;
  162. };
  163. struct TLRUHelper {
  164. template <class TKey, class TValue>
  165. using TListType = TLRUList<TKey, TValue>;
  166. template <class TKey, class TValue, class... TArgs>
  167. using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>;
  168. };
  169. }
  170. template <class TKey, class TValue, class... TArgs>
  171. using TThreadSafeCache = typename NPrivate::TLWHelper::template TCache<TKey, TValue, TArgs...>;
  172. template <class TKey, class TValue, class... TArgs>
  173. using TThreadSafeLRUCache = typename NPrivate::TLRUHelper::template TCache<TKey, TValue, TArgs...>;