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