|
| 1 | +#pragma once |
| 2 | +#include <functional> |
| 3 | + |
| 4 | +#include "request.hpp" |
| 5 | +#include "userver/cache/expirable_lru_cache.hpp" |
| 6 | +#include "userver/components/component_base.hpp" |
| 7 | +#include "userver/components/component_context.hpp" |
| 8 | +#include "userver/components/statistics_storage.hpp" |
| 9 | +#include "userver/utils/statistics/entry.hpp" |
| 10 | +#include "userver/yaml_config/schema.hpp" |
| 11 | + |
| 12 | + |
| 13 | +namespace ton_http::cache { |
| 14 | + |
| 15 | +namespace impl { |
| 16 | +userver::yaml_config::Schema GetExpirableLruCacheStaticConfigSchema(); |
| 17 | +} |
| 18 | + |
| 19 | +// clang-format on |
| 20 | +template <typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>> |
| 21 | +// NOLINTNEXTLINE(fuchsia-multiple-inheritance) |
| 22 | +class ExpirableLruCacheComponent : public userver::components::ComponentBase { |
| 23 | +public: |
| 24 | + using Cache = userver::cache::ExpirableLruCache<Key, Value, Hash, Equal>; |
| 25 | + |
| 26 | + ExpirableLruCacheComponent(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context) |
| 27 | + : ComponentBase(config, context), name_(userver::components::GetCurrentComponentName(config)), |
| 28 | + static_config_(config), cache_(std::make_shared<Cache>(static_config_.ways, static_config_.GetWaySize())) { |
| 29 | + cache_->SetMaxLifetime(static_config_.config.lifetime); |
| 30 | + cache_->SetBackgroundUpdate(static_config_.config.background_update); |
| 31 | + |
| 32 | + statistics_holder_ = context.FindComponent<userver::components::StatisticsStorage>().GetStorage().RegisterWriter( |
| 33 | + "cache", [this](userver::utils::statistics::Writer& writer) { |
| 34 | + writer = *cache_; |
| 35 | + }, {{"cache_name", name_}}); |
| 36 | + } |
| 37 | + ~ExpirableLruCacheComponent() { |
| 38 | + statistics_holder_.Unregister(); |
| 39 | + } |
| 40 | + |
| 41 | + static userver::yaml_config::Schema GetStaticConfigSchema() { |
| 42 | + return impl::GetExpirableLruCacheStaticConfigSchema(); |
| 43 | + } |
| 44 | + |
| 45 | + std::optional<Value> Get(const Key& key) { |
| 46 | + return cache_->GetOptionalNoUpdate(key); |
| 47 | + } |
| 48 | + void Put(const Key& key, const Value& value) { |
| 49 | + return cache_->Put(key, value); |
| 50 | + } |
| 51 | +private: |
| 52 | + const std::string name_; |
| 53 | + const userver::cache::LruCacheConfigStatic static_config_; |
| 54 | + const std::shared_ptr<Cache> cache_; |
| 55 | + |
| 56 | + userver::utils::statistics::Entry statistics_holder_; |
| 57 | +}; |
| 58 | + |
| 59 | +class CacheApiV2Component final : public ExpirableLruCacheComponent<handlers::TonlibApiRequest, userver::formats::json::Value> { |
| 60 | +public: |
| 61 | + static constexpr std::string_view kName = "cache-api-v2"; |
| 62 | + CacheApiV2Component(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context) |
| 63 | + : ExpirableLruCacheComponent(config, context) {}; |
| 64 | +}; |
| 65 | + |
| 66 | +class ConstCacheApiV2Component final : public ExpirableLruCacheComponent<handlers::TonlibApiRequest, userver::formats::json::Value> { |
| 67 | +public: |
| 68 | + static constexpr std::string_view kName = "const-cache-api-v2"; |
| 69 | + ConstCacheApiV2Component(const userver::components::ComponentConfig& config, const userver::components::ComponentContext& context) |
| 70 | + : ExpirableLruCacheComponent(config, context) {}; |
| 71 | +}; |
| 72 | +// clang-format off |
| 73 | +} |
0 commit comments