MySQL 9.0.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
29#include "components/keyrings/common/data/data.h" /* data::Data */
30#include "components/keyrings/common/data/meta.h" /* meta::Metadata */
31
33
34template <typename Data_extension>
35using Cache =
36 std::unordered_map<meta::Metadata, Data_extension, meta::Metadata::Hash>;
37
38template <typename Data_extension = data::Data>
39class Datacache final {
40 public:
41 /** Constructor */
42 explicit Datacache() { version_ = 0; }
43
44 /** Disable copy */
45 Datacache(const Datacache &src) = delete;
46 Datacache(Datacache &&src) = delete;
47 Datacache &operator=(const Datacache &src) = delete;
48 Datacache &operator=(Datacache &&src) = delete;
49
50 /** Destructor */
51 ~Datacache() = default;
52
53 /**
54 Retrieve an element from cache
55 @param [in] metadata Key to search data
56 @param [out] data Fetched data. Can be empty.
57
58 @returns status of find operation
59 @retval true Success. data contains retrieved data.
60 @retval false Failure. data may not contain a valid value.
61 */
62 bool get(const meta::Metadata metadata, Data_extension &data) const {
63 auto it = cache_.find(metadata);
64 if (it == cache_.end()) return false;
65 data = it->second;
66 return true;
67 }
68
69 /**
70 Store and element in cache
71 @param [in] metadata Key to store data
72 @param [in] data Actual data. Can be empty.
73
74 @returns status of insert operation
75 @retval true Success
76 @retval false Error. Element already exists in the cache.
77 */
78 bool store(const meta::Metadata metadata, const Data_extension data) {
79 bool ok = cache_.insert({metadata, data}).second;
80 if (ok) ++version_;
81 return ok;
82 }
83
84 /**
85 Remove an entry from cache
86 @param [in] metadata Key to entry to be erased
87
88 @returns status of find operation
89 @retval true Success. Data removed successfully.
90 @retval false Failure. Either key is not present or removal failed.
91 */
92 bool erase(const meta::Metadata metadata) {
93 bool removed = cache_.erase(metadata) != 0;
94 if (removed) ++version_;
95 return removed;
96 }
97
98 /** Clear the cache */
99 void clear() { cache_.clear(); }
100
101 /** Check if cache is empty */
102 bool empty() const { return cache_.empty(); }
103
104 /** Get size */
105 size_t size() const { return cache_.size(); }
106
107 /** Get cache version */
108 size_t version() const { return version_; }
109
110 /* Iterators */
112 return cache_.cbegin();
113 }
115 return cache_.cend();
116 }
117
118 /**
119 Retrieve iterator at an element from cache
120 @param [in] metadata Key to search data
121 */
123 const meta::Metadata metadata) const {
124 return cache_.find(metadata);
125 }
126
127 private:
128 /** Sensitive data cache */
130 /** Cache version */
131 size_t version_{0};
132};
133
134} // namespace keyring_common::cache
135
136#endif // !CACHE_INCLUDED
Definition: cache.h:39
bool erase(const meta::Metadata metadata)
Remove an entry from cache.
Definition: cache.h:92
Cache< Data_extension >::const_iterator at(const meta::Metadata metadata) const
Retrieve iterator at an element from cache.
Definition: cache.h:122
Datacache & operator=(const Datacache &src)=delete
void clear()
Clear the cache.
Definition: cache.h:99
Cache< Data_extension >::const_iterator begin() const
Definition: cache.h:111
size_t version() const
Get cache version.
Definition: cache.h:108
Cache< Data_extension > cache_
Sensitive data cache.
Definition: cache.h:129
size_t size() const
Get size.
Definition: cache.h:105
~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:78
Cache< Data_extension >::const_iterator end() const
Definition: cache.h:114
Datacache & operator=(Datacache &&src)=delete
bool empty() const
Check if cache is empty.
Definition: cache.h:102
Datacache()
Constructor.
Definition: cache.h:42
size_t version_
Cache version.
Definition: cache.h:131
bool get(const meta::Metadata metadata, Data_extension &data) const
Retrieve an element from cache.
Definition: cache.h:62
Common metadata.
Definition: meta.h:38
Definition: cache.h:32
std::unordered_map< meta::Metadata, Data_extension, meta::Metadata::Hash > Cache
Definition: cache.h:36