|
| 1 | +// Compiler for PHP (aka KPHP) |
| 2 | +// Copyright (c) 2025 LLC «V Kontakte» |
| 3 | +// Distributed under the GPL v3 License, see LICENSE.notice.txt |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <cstdint> |
| 8 | +#include <limits> |
| 9 | +#include <string_view> |
| 10 | +#include <utility> |
| 11 | + |
| 12 | +#include "runtime-common/core/runtime-core.h" |
| 13 | +#include "runtime-common/stdlib/serialization/msgpack-functions.h" |
| 14 | +#include "runtime-light/coroutine/task.h" |
| 15 | +#include "runtime-light/stdlib/component/component-api.h" |
| 16 | +#include "runtime-light/stdlib/instance-cache/instance-cache-state.h" |
| 17 | +#include "runtime-light/stdlib/serialization/msgpack-functions.h" |
| 18 | +#include "runtime-light/tl/tl-core.h" |
| 19 | +#include "runtime-light/tl/tl-functions.h" |
| 20 | +#include "runtime-light/tl/tl-types.h" |
| 21 | +#include "runtime-light/utils/logs.h" |
| 22 | + |
| 23 | +namespace kphp::instance_cache::details { |
| 24 | + |
| 25 | +inline constexpr std::string_view COMPONENT_NAME{"instance_cache"}; |
| 26 | + |
| 27 | +} // namespace kphp::instance_cache::details |
| 28 | + |
| 29 | +template<typename InstanceType> |
| 30 | +kphp::coro::task<bool> f$instance_cache_store(string key, InstanceType instance, int64_t ttl = 0) noexcept { |
| 31 | + if (ttl < 0) [[unlikely]] { |
| 32 | + kphp::log::warning("ttl can't be negative: ttl -> {}, key -> {}", ttl, key.c_str()); |
| 33 | + co_return false; |
| 34 | + } |
| 35 | + if (ttl > std::numeric_limits<uint32_t>::max()) [[unlikely]] { |
| 36 | + kphp::log::warning("ttl exceeds maximum allowed value, key will be stored forever: ttl -> {}, max -> {}, key -> {}", ttl, |
| 37 | + std::numeric_limits<uint32_t>::max(), key.c_str()); |
| 38 | + ttl = 0; |
| 39 | + } |
| 40 | + |
| 41 | + auto serialized_instance{f$instance_serialize(instance)}; |
| 42 | + if (!serialized_instance.has_value()) [[unlikely]] { |
| 43 | + kphp::log::warning("can't serialize instance: key -> {}", key.c_str()); |
| 44 | + co_return false; |
| 45 | + } |
| 46 | + |
| 47 | + tl::TLBuffer tlb; |
| 48 | + tl::CacheStore{.key = tl::string{.value = {key.c_str(), key.size()}}, |
| 49 | + .value = tl::string{.value = {serialized_instance.val().c_str(), serialized_instance.val().size()}}, |
| 50 | + .ttl = tl::u32{.value = static_cast<uint32_t>(ttl)}} |
| 51 | + .store(tlb); |
| 52 | + |
| 53 | + auto query{co_await f$component_client_send_request( |
| 54 | + string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())}, |
| 55 | + string{tlb.data(), static_cast<string::size_type>(tlb.size())})}; |
| 56 | + if (query.is_null()) [[unlikely]] { |
| 57 | + co_return false; |
| 58 | + } |
| 59 | + |
| 60 | + auto response{co_await f$component_client_fetch_response(std::move(query))}; |
| 61 | + tlb.clean(); |
| 62 | + tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())}); |
| 63 | + |
| 64 | + tl::Bool tl_bool{}; |
| 65 | + kphp::log::assertion(tl_bool.fetch(tlb)); |
| 66 | + InstanceCacheInstanceState::get().request_cache.emplace(std::move(key), std::move(instance)); |
| 67 | + co_return tl_bool.value; |
| 68 | +} |
| 69 | + |
| 70 | +template<typename InstanceType> |
| 71 | +kphp::coro::task<InstanceType> f$instance_cache_fetch(string /*class_name*/, string key, bool /*even_if_expired*/ = false) noexcept { |
| 72 | + auto& request_cache{InstanceCacheInstanceState::get().request_cache}; |
| 73 | + if (auto it{request_cache.find(key)}; it != request_cache.end()) { |
| 74 | + auto cached_instance{from_mixed<InstanceType>(it->second, {})}; |
| 75 | + co_return std::move(cached_instance); |
| 76 | + } |
| 77 | + |
| 78 | + tl::TLBuffer tlb; |
| 79 | + tl::CacheFetch{.key = tl::string{.value = {key.c_str(), key.size()}}}.store(tlb); |
| 80 | + |
| 81 | + auto query{co_await f$component_client_send_request( |
| 82 | + string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())}, |
| 83 | + string{tlb.data(), static_cast<string::size_type>(tlb.size())})}; |
| 84 | + if (query.is_null()) [[unlikely]] { |
| 85 | + co_return InstanceType{}; |
| 86 | + } |
| 87 | + |
| 88 | + auto response{co_await f$component_client_fetch_response(std::move(query))}; |
| 89 | + tlb.clean(); |
| 90 | + tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())}); |
| 91 | + |
| 92 | + tl::Maybe<tl::string> maybe_string{}; |
| 93 | + kphp::log::assertion(maybe_string.fetch(tlb)); |
| 94 | + if (!maybe_string.opt_value) [[unlikely]] { |
| 95 | + co_return InstanceType{}; |
| 96 | + } |
| 97 | + |
| 98 | + auto cached_instance{f$instance_deserialize<InstanceType>( |
| 99 | + string{(*maybe_string.opt_value).value.data(), static_cast<string::size_type>((*maybe_string.opt_value).value.size())}, {})}; |
| 100 | + request_cache.emplace(std::move(key), cached_instance); |
| 101 | + co_return std::move(cached_instance); |
| 102 | +} |
| 103 | + |
| 104 | +inline kphp::coro::task<bool> f$instance_cache_update_ttl(string key, int64_t ttl = 0) noexcept { |
| 105 | + if (ttl < 0) [[unlikely]] { |
| 106 | + kphp::log::warning("ttl can't be negative: ttl -> {}, key -> {}", ttl, key.c_str()); |
| 107 | + co_return false; |
| 108 | + } |
| 109 | + if (ttl > std::numeric_limits<uint32_t>::max()) [[unlikely]] { |
| 110 | + kphp::log::warning("ttl exceeds maximum allowed value, key will be stored forever: ttl -> {}, max -> {}, key -> {}", ttl, |
| 111 | + std::numeric_limits<uint32_t>::max(), key.c_str()); |
| 112 | + ttl = 0; |
| 113 | + } |
| 114 | + |
| 115 | + tl::TLBuffer tlb; |
| 116 | + tl::CacheUpdateTtl{.key = tl::string{.value = {key.c_str(), key.size()}}, .ttl = tl::u32{.value = static_cast<uint32_t>(ttl)}}.store(tlb); |
| 117 | + |
| 118 | + auto query{co_await f$component_client_send_request( |
| 119 | + string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())}, |
| 120 | + string{tlb.data(), static_cast<string::size_type>(tlb.size())})}; |
| 121 | + if (query.is_null()) [[unlikely]] { |
| 122 | + co_return false; |
| 123 | + } |
| 124 | + |
| 125 | + auto response{co_await f$component_client_fetch_response(std::move(query))}; |
| 126 | + tlb.clean(); |
| 127 | + tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())}); |
| 128 | + |
| 129 | + tl::Bool tl_bool{}; |
| 130 | + kphp::log::assertion(tl_bool.fetch(tlb)); |
| 131 | + co_return tl_bool.value; |
| 132 | +} |
| 133 | + |
| 134 | +inline kphp::coro::task<bool> f$instance_cache_delete(string key) noexcept { |
| 135 | + InstanceCacheInstanceState::get().request_cache.erase(key); |
| 136 | + |
| 137 | + tl::TLBuffer tlb; |
| 138 | + tl::CacheDelete{.key = tl::string{.value = {key.c_str(), key.size()}}}.store(tlb); |
| 139 | + |
| 140 | + auto query{co_await f$component_client_send_request( |
| 141 | + string{kphp::instance_cache::details::COMPONENT_NAME.data(), static_cast<string::size_type>(kphp::instance_cache::details::COMPONENT_NAME.size())}, |
| 142 | + string{tlb.data(), static_cast<string::size_type>(tlb.size())})}; |
| 143 | + if (query.is_null()) [[unlikely]] { |
| 144 | + co_return false; |
| 145 | + } |
| 146 | + |
| 147 | + auto response{co_await f$component_client_fetch_response(std::move(query))}; |
| 148 | + tlb.clean(); |
| 149 | + tlb.store_bytes({response.c_str(), static_cast<size_t>(response.size())}); |
| 150 | + |
| 151 | + tl::Bool tl_bool{}; |
| 152 | + kphp::log::assertion(tl_bool.fetch(tlb)); |
| 153 | + co_return tl_bool.value; |
| 154 | +} |
0 commit comments