MySQL 8.3.0
Source Code Documentation
cache.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef CACHE_INCLUDED
24#define CACHE_INCLUDED
25
26#include <unordered_map>
27
28#include "components/keyrings/common/data/data.h" /* data::Data */
29#include "components/keyrings/common/data/meta.h" /* meta::Metadata */
30
31namespace keyring_common {
32
33namespace cache {
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 Retrieve an element from cache
56 @param [in] metadata Key to search data
57 @param [out] data Fetched data. Can be empty.
58
59 @returns status of find operation
60 @retval true Success. data contains retrieved data.
61 @retval false Failure. data may not contain a valid value.
62 */
63 bool get(const meta::Metadata metadata, Data_extension &data) const {
64 auto it = cache_.find(metadata);
65 if (it == cache_.end()) return false;
66 data = it->second;
67 return true;
68 }
69
70 /**
71 Store and element in cache
72 @param [in] metadata Key to store data
73 @param [in] data Actual data. Can be empty.
74
75 @returns status of insert operation
76 @retval true Success
77 @retval false Error. Element already exists in the cache.
78 */
79 bool store(const meta::Metadata metadata, const Data_extension data) {
80 bool ok = cache_.insert({metadata, data}).second;
81 if (ok) ++version_;
82 return ok;
83 }
84
85 /**
86 Remove an entry from cache
87 @param [in] metadata Key to entry to be erased
88
89 @returns status of find operation
90 @retval true Success. Data removed successfully.
91 @retval false Failure. Either key is not present or removal failed.
92 */
93 bool erase(const meta::Metadata metadata) {
94 bool removed = cache_.erase(metadata) != 0;
95 if (removed) ++version_;
96 return removed;
97 }
98
99 /** Clear the cache */
100 void clear() { cache_.clear(); }
101
102 /** Check if cache is empty */
103 bool empty() const { return cache_.empty(); }
104
105 /** Get size */
106 size_t size() const { return cache_.size(); }
107
108 /** Get cache version */
109 size_t version() const { return version_; }
110
111 /* Iterators */
113 return cache_.cbegin();
114 }
116 return cache_.cend();
117 }
118
119 /**
120 Retrieve iterator at an element from cache
121 @param [in] metadata Key to search data
122 */
124 const meta::Metadata metadata) const {
125 return cache_.find(metadata);
126 }
127
128 private:
129 /** Sensitive data cache */
131 /** Cache version */
132 size_t version_{0};
133};
134
135} // namespace cache
136} // namespace keyring_common
137
138#endif // !CACHE_INCLUDED
Definition: cache.h:40
bool erase(const meta::Metadata metadata)
Remove an entry from cache.
Definition: cache.h:93
Cache< Data_extension >::const_iterator at(const meta::Metadata metadata) const
Retrieve iterator at an element from cache.
Definition: cache.h:123
Datacache & operator=(const Datacache &src)=delete
void clear()
Clear the cache.
Definition: cache.h:100
Cache< Data_extension >::const_iterator begin() const
Definition: cache.h:112
size_t version() const
Get cache version.
Definition: cache.h:109
Cache< Data_extension > cache_
Sensitive data cache.
Definition: cache.h:130
size_t size() const
Get size.
Definition: cache.h:106
~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:79
Cache< Data_extension >::const_iterator end() const
Definition: cache.h:115
Datacache & operator=(Datacache &&src)=delete
bool empty() const
Check if cache is empty.
Definition: cache.h:103
Datacache()
Constructor.
Definition: cache.h:43
size_t version_
Cache version.
Definition: cache.h:132
bool get(const meta::Metadata metadata, Data_extension &data) const
Retrieve an element from cache.
Definition: cache.h:63
Common metadata.
Definition: meta.h:38
std::unordered_map< meta::Metadata, Data_extension, meta::Metadata::Hash > Cache
Definition: cache.h:37
Definition: keyring_encryption_service_definition.h:31