75 const FunctionRef<std::unique_ptr<CachedValue>()> compute_fn)
86 return accessor->second.value;
93 std::shared_ptr<CachedValue>
result = compute_fn();
99 const bool newly_inserted = cache.
map.
add(accessor, std::ref(
key));
100 if (!newly_inserted) {
103 return accessor->second.value;
107 accessor->second.key =
key.to_storable();
109 const_cast<std::reference_wrapper<const GenericKey> &
>(accessor->first) = std::ref(
110 *accessor->second.key);
113 accessor->second.value =
result;
121 accessor->second.value->count_memory(memory_counter);
122 cache.
keys.append(&accessor->first.get());
189 if (old_size < approximate_limit) {
203 keys_with_time.
append({accessor->second.last_use_time,
key});
206 std::sort(keys_with_time.
begin(), keys_with_time.
end());
207 std::reverse(keys_with_time.
begin(), keys_with_time.
end());
212 std::optional<int> first_bad_index;
221 accessor->second.value->count_memory(memory_counter);
232 if (!first_bad_index) {
239 bool need_memory_recount =
false;
241 *first_bad_index += 1;
242 if (*first_bad_index == keys_with_time.
size()) {
247 need_memory_recount =
true;
259 cache.
keys.append(keys_with_time[i].second);
262 if (need_memory_recount) {
273 accessor->second.value->count_memory(memory_counter);