MySQL 9.1.0
Source Code Documentation
cache.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef CACHE_INCLUDED
25#define CACHE_INCLUDED
26
27#include <unordered_map>
28#include <utility>
29
30#include "components/keyrings/common/data/data.h" /* data::Data */
31#include "components/keyrings/common/data/meta.h" /* meta::Metadata */
32
34
35template <typename Data_extension>
36using Cache =
37 std::unordered_map<meta::Metadata, Data_extension, meta::Metadata::Hash>;
38
39template <typename Data_extension = data::Data>
40class Datacache final {
41 public:
42 /** Constructor */
43 explicit Datacache() { version_ = 0; }
44
45 /** Disable copy */
46 Datacache(const Datacache &src) = delete;
47 Datacache(Datacache &&src) = delete;
48 Datacache &operator=(const Datacache &src) = delete;
49 Datacache &operator=(Datacache &&src) = delete;
50
51 /** Destructor */
52 ~Datacache() = default;
53
54 /**
55 Swap content of two caches.
56
57 @param [in, out] a first cache to be swapped
58 @param [in, out] b second cache to be swapped
59 */
60 static void swap(Datacache &a, Datacache &b) {
63 }
64
65 /**
66 Retrieve an element from cache
67 @param [in] metadata Key to search data
68 @param [out] data Fetched data. Can be empty.
69
70 @returns status of find operation
71 @retval true Success. data contains retrieved data.
72 @retval false Failure. data may not contain a valid value.
73 */
74 bool get(const meta::Metadata metadata, Data_extension &data) const {
75 auto it = cache_.find(metadata);
76 if (it == cache_.end()) return false;
77 data = it->second;
78 return true;
79 }
80
81 /**
82 Store and element in cache
83 @param [in] metadata Key to store data
84 @param [in] data Actual data. Can be empty.
85
86 @returns status of insert operation
87 @retval true Success
88 @retval false Error. Element already exists in the cache.
89 */
90 bool store(const meta::Metadata metadata, const Data_extension data) {
91 bool ok = cache_.insert({metadata, data}).second;
92 if (ok) ++version_;
93 return ok;
94 }
95
96 /**
97 Remove an entry from cache
98 @param [in] metadata Key to entry to be erased
99
100 @returns status of find operation
101 @retval true Success. Data removed successfully.
102 @retval false Failure. Either key is not present or removal failed.
103 */
104 bool erase(const meta::Metadata metadata) {
105 bool removed = cache_.erase(metadata) != 0;
106 if (removed) ++version_;
107 return removed;
108 }
109
110 /** Clear the cache */
111 void clear() { cache_.clear(); }
112
113 /** Check if cache is empty */
114 bool empty() const { return cache_.empty(); }
115
116 /** Get size */
117 size_t size() const { return cache_.size(); }
118
119 /** Get cache version */
120 size_t version() const { return version_; }
121
122 /* Iterators */
124 return cache_.cbegin();
125 }
127 return cache_.cend();
128 }
129
130 /**
131 Retrieve iterator at an element from cache
132 @param [in] metadata Key to search data
133 */
135 const meta::Metadata metadata) const {
136 return cache_.find(metadata);
137 }
138
139 private:
140 /** Sensitive data cache */
142 /** Cache version */
143 size_t version_{0};
144};
145
146} // namespace keyring_common::cache
147
148#endif // !CACHE_INCLUDED
Definition: cache.h:40
bool erase(const meta::Metadata metadata)
Remove an entry from cache.
Definition: cache.h:104
Cache< Data_extension >::const_iterator at(const meta::Metadata metadata) const
Retrieve iterator at an element from cache.
Definition: cache.h:134
Datacache & operator=(const Datacache &src)=delete
void clear()
Clear the cache.
Definition: cache.h:111
Cache< Data_extension >::const_iterator begin() const
Definition: cache.h:123
static void swap(Datacache &a, Datacache &b)
Swap content of two caches.
Definition: cache.h:60
size_t version() const
Get cache version.
Definition: cache.h:120
Cache< Data_extension > cache_
Sensitive data cache.
Definition: cache.h:141
size_t size() const
Get size.
Definition: cache.h:117
~Datacache()=default
Destructor.
Datacache(const Datacache &src)=delete
Disable copy.
Datacache(Datacache &&src)=delete
bool store(const meta::Metadata metadata, const Data_extension data)
Store and element in cache.
Definition: cache.h:90
Cache< Data_extension >::const_iterator end() const
Definition: cache.h:126
Datacache & operator=(Datacache &&src)=delete
bool empty() const
Check if cache is empty.
Definition: cache.h:114
Datacache()
Constructor.
Definition: cache.h:43
size_t version_
Cache version.
Definition: cache.h:143
bool get(const meta::Metadata metadata, Data_extension &data) const
Retrieve an element from cache.
Definition: cache.h:74
Common metadata.
Definition: meta.h:38
Definition: cache.h:33
std::unordered_map< meta::Metadata, Data_extension, meta::Metadata::Hash > Cache
Definition: cache.h:37
static void swap(String &a, String &b) noexcept
Definition: sql_string.h:663