diff options
author | ivanmautin <ivanmautin@yandex-team.com> | 2024-06-06 09:57:03 +0300 |
---|---|---|
committer | ivanmautin <ivanmautin@yandex-team.com> | 2024-06-06 10:07:42 +0300 |
commit | 3babd5b1391836f4f4fdb3add9064a59707f9f7d (patch) | |
tree | 74336eeadabd9f2d99cdd4d93742c6c1fc240ae3 /library/cpp/cache/thread_safe_cache.h | |
parent | caa721da15eab69ba0c5eae383d494627ff115f5 (diff) | |
download | ydb-3babd5b1391836f4f4fdb3add9064a59707f9f7d.tar.gz |
add TThreadSafeLRUCacheWithSizeProvider wrapper
На данный момент никак нельзя создать thread-safe кэш с произвольным SizeProvider, из-за того, что это не позволяет сделать шаблон `TThreadSafeCache`, при этом отредактировтаь его тоже не удастся, так как для этого нужно передать дополнительный параметр `typename TSizeProvider`, что сломает обратную совместимость, так как шаблон принимает далее переменное число аргументов (см. [TThreadSafeCache](https://a.yandex-team.ru/arcadia/library/cpp/cache/thread_safe_cache.h?rev=rXXXXXX#L15))
В связи с этим добавлен еще один хелпер, для создания LRUCache с TSizeProvider
293511a33b45f23d8afc9ff217a817481401932c
Diffstat (limited to 'library/cpp/cache/thread_safe_cache.h')
-rw-r--r-- | library/cpp/cache/thread_safe_cache.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h index 2699c8f7b7..08c899ad3d 100644 --- a/library/cpp/cache/thread_safe_cache.h +++ b/library/cpp/cache/thread_safe_cache.h @@ -201,6 +201,44 @@ namespace NPrivate { using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>; }; + struct TLFUHelper { + template <class TKey, class TValue> + using TListType = TLFUList<TKey, TValue>; + + template <class TKey, class TValue, class... TArgs> + using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>; + }; + + template <class TSizeProvider, class TValue> + struct TSizeProviderRemoveAtomic : TSizeProvider { + // TValue in this signature is TCache::TPtr, using this wrapper user don't need + // to handle TPtr (which is TAtomicSharedPtr<TValue>) and can just accept TValue + // in custom size provider. See example in unittests + size_t operator()(const TValue& value) const { + // We can pass reference to value without synchronization, because TSizeProvider::operator() + // is always called from methods secured by a guard + return TSizeProvider::operator()(*value); + } + }; + + template <template <class, class, class> class TTemplateListType, EGettersPromotionPolicy GettersPromotionPolicy> + struct TCacheWithSizeProviderHelper { + private: + template <class TSizeProvider> + struct TListWithProvider { + template <class TKey, class TValue> + using TListType = TTemplateListType<TKey, TValue, TSizeProviderRemoveAtomic<TSizeProvider, TValue>>; + }; + + public: + template <class TKey, class TValue, class TSizeProvider, class... TArgs> + using TCache = TThreadSafeCache<TKey, TValue, TListWithProvider<TSizeProvider>::template TListType, GettersPromotionPolicy, TArgs...>; + }; + + using TLRUWithSizeProviderHelper = TCacheWithSizeProviderHelper<TLRUList, EGettersPromotionPolicy::Promoted>; + + using TLFUWithSizeProviderHelper = TCacheWithSizeProviderHelper<TLFUList, EGettersPromotionPolicy::Promoted>; + } template <class TKey, class TValue, class... TArgs> @@ -209,3 +247,11 @@ using TThreadSafeCache = typename NPrivate::TLWHelper::template TCache<TKey, TVa template <class TKey, class TValue, class... TArgs> using TThreadSafeLRUCache = typename NPrivate::TLRUHelper::template TCache<TKey, TValue, TArgs...>; +template <class TKey, class TValue, class... TArgs> +using TThreadSafeLFUCache = typename NPrivate::TLFUHelper::template TCache<TKey, TValue, TArgs...>; + +template <class TKey, class TValue, class TSizeProvider, class... TArgs> +using TThreadSafeLRUCacheWithSizeProvider = typename NPrivate::TLRUWithSizeProviderHelper::template TCache<TKey, TValue, TSizeProvider, TArgs...>; + +template <class TKey, class TValue, class TSizeProvider, class... TArgs> +using TThreadSafeLFUCacheWithSizeProvider = typename NPrivate::TLFUWithSizeProviderHelper::template TCache<TKey, TValue, TSizeProvider, TArgs...>; |