MySQL 9.7.0
Source Code Documentation
cache.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_UTILITY_CACHE_CACHE_H_
27#define MYSQL_HARNESS_UTILITY_CACHE_CACHE_H_
28
29#include <map>
30#include <utility>
31
33
34namespace mysql_harness {
35namespace utility {
36namespace cache {
37
38template <typename Key, typename Value, typename Algorithm>
39class Cache {
40 using Container = std::map<Key, Value>;
41
42 public:
43 template <typename... Args>
44 explicit Cache(Args &&...args) : key_cache_(std::forward<Args>(args)...) {}
45
46 void remove(const Key &key) {
47 auto it = container_.find(key);
48
49 if (container_.end() != it) {
50 key_cache_.remove(key);
51 container_.erase(it);
52 }
53 }
54
55 Value *get_cached_value(const Key &key) {
56 auto it = container_.find(key);
57 if (container_.end() == it) {
58 return nullptr;
59 }
60
61 key_cache_.access(key);
62
63 return &it->second;
64 }
65
66 Value *set(const Key &key, Value &&value) {
67 return set_impl(key, std::move(value));
68 }
69
70 Value *set(const Key &key, const Value &value) {
71 return set_impl(key, value);
72 }
73
75
76 bool is_full() const { return key_cache_.is_full(); }
77
78 private:
79 template <typename V>
80 Value *set_impl(const Key &key, V &&value) {
81 auto cached_value = get_cached_value(key);
82 if (cached_value) {
83 *cached_value = std::forward<V>(value);
84 return cached_value;
85 }
86
87 Key *removed_key = nullptr;
88 key_cache_.push(key, &removed_key);
89
90 if (removed_key) {
91 auto node = container_.extract(*removed_key);
92 node.key() = key;
93 node.mapped() = std::forward<V>(value);
94 container_.insert(std::move(node));
95 return &container_[key];
96 }
97
98 return &container_.emplace(std::make_pair(key, std::forward<V>(value)))
99 .first->second;
100 }
101
104};
105
106template <typename Key, typename Value, uint32_t size>
108 Cache<Key, Value,
110 Key, size>>;
111
112template <typename Key, typename Value>
114 Key, Value,
116
117} // namespace cache
118} // namespace utility
119} // namespace mysql_harness
120
121#endif // MYSQL_HARNESS_UTILITY_CACHE_CACHE_H_
Value * set_impl(const Key &key, V &&value)
Definition: cache.h:80
Cache(Args &&...args)
Definition: cache.h:44
Algorithm key_cache_
Definition: cache.h:102
Value * get_cached_value(const Key &key)
Definition: cache.h:55
Value * set(const Key &key, Value &&value)
Definition: cache.h:66
bool is_full() const
Definition: cache.h:76
Container container_
Definition: cache.h:103
Value * set(const Key &key, const Value &value)
Definition: cache.h:70
std::map< Key, Value > Container
Definition: cache.h:40
void remove(const Key &key)
Definition: cache.h:46
Container & get_container()
Definition: cache.h:74
std::string_view Key
The key type for the hash structure in HashJoinRowBuffer.
Definition: hash_join_buffer.h:108
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: common.h:44
size_t size(const char *const c)
Definition: base64.h:46
Algorithm
Definition: base64_encode.h:42
Define std::hash<Gtid>.
Definition: gtid.h:355
required string key
Definition: replication_asynchronous_connection_failover.proto:60