MySQL 9.4.0
Source Code Documentation
response_cache.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 2025, 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 ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_REST_RESPONSE_CACHE_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_REST_RESPONSE_CACHE_H_
28
29#include <atomic>
30#include <chrono>
31#include <memory>
32#include <mutex>
33#include <optional>
34#include <shared_mutex>
35#include <string>
36#include <unordered_map>
37#include "helper/media_type.h"
38#include "http/base/uri.h"
40
41namespace mrs {
42
43class EndpointResponseCache;
44
45struct CacheEntry {
46 using TimeType = std::chrono::time_point<std::chrono::system_clock>;
47
48 std::string data;
49 int64_t items = 0;
50 std::optional<helper::MediaType> media_type;
51 std::optional<std::string> media_type_str;
52
53 std::string key;
55
57
58 std::shared_ptr<CacheEntry> next_ptr;
59 std::shared_ptr<CacheEntry> prev_ptr;
60
61 bool is_expired() const { return expiration_time < TimeType::clock::now(); }
62};
63
64constexpr const size_t k_default_object_cache_size = 1000000;
65
67 public:
71
72 explicit ResponseCache(const std::string &config_key)
73 : config_key_(config_key) {}
74
75 void configure(const std::string &options);
76
77 size_t max_cache_size() const { return max_size_; }
78
79 void remove(std::shared_ptr<CacheEntry> entry);
80
81 private:
82 void push(std::shared_ptr<CacheEntry> entry);
83 void remove_nolock(std::shared_ptr<CacheEntry> entry);
84
86
87 void shrink_object_cache(size_t extra_size = 0);
88
89 std::string config_key_;
90
91 std::shared_ptr<CacheEntry> newest_entry_;
92 std::shared_ptr<CacheEntry> oldest_entry_;
93 std::mutex entries_mutex_;
94 std::atomic<size_t> cache_size_ = 0;
95
97};
98
100 public:
103
104 protected:
105 EndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms);
106 virtual ~EndpointResponseCache() = default;
107
108 std::shared_ptr<CacheEntry> create_entry(
109 const std::string &key, const std::string &data, int64_t items = 0,
110 std::optional<helper::MediaType> media_type = {},
111 std::optional<std::string> media_type_str = {});
112
113 void remove_entry(std::shared_ptr<CacheEntry> entry, bool ejected);
114 virtual void remove_entry_nolock(std::shared_ptr<CacheEntry> entry,
115 bool ejected);
116
117 std::shared_ptr<CacheEntry> lookup(const std::string &key) const;
118
119 friend class ResponseCache;
120
122
124
125 std::unordered_map<std::string, std::shared_ptr<CacheEntry>> cache_;
126 mutable std::shared_mutex cache_mutex_;
127};
128
130 public:
131 ItemEndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms);
133
134 std::shared_ptr<CacheEntry> create_table_entry(const Uri &uri,
135 const std::string &user_id,
136 const std::string &data,
137 int64_t items);
138
139 std::shared_ptr<CacheEntry> create_routine_entry(
140 const Uri &uri, std::string_view req_body, const std::string &data,
141 std::optional<helper::MediaType> media_type = {});
142
143 std::shared_ptr<CacheEntry> create_routine_entry(
144 const Uri &uri, std::string_view req_body, const std::string &data,
145 const std::string &media_type_str);
146
147 std::shared_ptr<CacheEntry> lookup_table(const Uri &uri,
148 const std::string &user_id);
149
150 std::shared_ptr<CacheEntry> lookup_routine(const Uri &uri,
151 std::string_view req_body);
152
153 private:
154 void remove_entry_nolock(std::shared_ptr<CacheEntry> entry,
155 bool ejected) override;
156};
157
159 public:
162
163 std::shared_ptr<CacheEntry> create_file_entry(const UniversalId &id,
164 const std::string &data,
165 helper::MediaType media_type);
166
167 std::shared_ptr<CacheEntry> lookup_file(const UniversalId &id);
168
169 private:
170 void remove_entry_nolock(std::shared_ptr<CacheEntry> entry,
171 bool ejected) override;
172};
173
174} // namespace mrs
175
176#endif // ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_REST_RESPONSE_CACHE_H_
Definition: uri.h:40
Definition: response_cache.h:99
void remove_entry(std::shared_ptr< CacheEntry > entry, bool ejected)
Definition: response_cache.cc:234
std::shared_ptr< CacheEntry > create_entry(const std::string &key, const std::string &data, int64_t items=0, std::optional< helper::MediaType > media_type={}, std::optional< std::string > media_type_str={})
Definition: response_cache.cc:202
ResponseCache * owner_
Definition: response_cache.h:121
EndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms)
Definition: response_cache.cc:196
std::shared_ptr< CacheEntry > lookup(const std::string &key) const
Definition: response_cache.cc:247
std::chrono::milliseconds ttl_
Definition: response_cache.h:123
virtual void remove_entry_nolock(std::shared_ptr< CacheEntry > entry, bool ejected)
Definition: response_cache.cc:242
std::shared_mutex cache_mutex_
Definition: response_cache.h:126
std::unordered_map< std::string, std::shared_ptr< CacheEntry > > cache_
Definition: response_cache.h:125
::mrs::database::entry::UniversalId UniversalId
Definition: response_cache.h:102
virtual ~EndpointResponseCache()=default
Definition: response_cache.h:158
std::shared_ptr< CacheEntry > lookup_file(const UniversalId &id)
Definition: response_cache.cc:348
FileEndpointResponseCache(ResponseCache *owner)
Definition: response_cache.cc:345
std::shared_ptr< CacheEntry > create_file_entry(const UniversalId &id, const std::string &data, helper::MediaType media_type)
Definition: response_cache.cc:374
~FileEndpointResponseCache() override
Definition: response_cache.cc:364
void remove_entry_nolock(std::shared_ptr< CacheEntry > entry, bool ejected) override
Definition: response_cache.cc:385
Definition: response_cache.h:129
std::shared_ptr< CacheEntry > lookup_routine(const Uri &uri, std::string_view req_body)
Definition: response_cache.cc:322
ItemEndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms)
Definition: response_cache.cc:259
void remove_entry_nolock(std::shared_ptr< CacheEntry > entry, bool ejected) override
Definition: response_cache.cc:337
std::shared_ptr< CacheEntry > create_table_entry(const Uri &uri, const std::string &user_id, const std::string &data, int64_t items)
Definition: response_cache.cc:273
std::shared_ptr< CacheEntry > create_routine_entry(const Uri &uri, std::string_view req_body, const std::string &data, std::optional< helper::MediaType > media_type={})
Definition: response_cache.cc:284
std::shared_ptr< CacheEntry > lookup_table(const Uri &uri, const std::string &user_id)
Definition: response_cache.cc:307
~ItemEndpointResponseCache() override
Definition: response_cache.cc:263
Definition: response_cache.h:66
void shrink_object_cache(size_t extra_size=0)
Definition: response_cache.cc:106
std::shared_ptr< CacheEntry > oldest_entry_
Definition: response_cache.h:92
std::string config_key_
Definition: response_cache.h:89
ResponseCache(const std::string &config_key)
Definition: response_cache.h:72
std::atomic< size_t > max_size_
Definition: response_cache.h:96
void configure(const std::string &options)
Definition: response_cache.cc:94
void remove_nolock(std::shared_ptr< CacheEntry > entry)
Definition: response_cache.cc:142
void remove(std::shared_ptr< CacheEntry > entry)
Definition: response_cache.cc:132
int remove_all(EndpointResponseCache *cache)
Definition: response_cache.cc:156
std::shared_ptr< CacheEntry > newest_entry_
Definition: response_cache.h:91
size_t max_cache_size() const
Definition: response_cache.h:77
void push(std::shared_ptr< CacheEntry > entry)
Definition: response_cache.cc:116
std::atomic< size_t > cache_size_
Definition: response_cache.h:94
std::mutex entries_mutex_
Definition: response_cache.h:93
MediaType
Definition: media_type.h:33
Request::Uri Uri
Definition: request.cc:36
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:67
Definition: authorize_manager.h:48
constexpr const size_t k_default_object_cache_size
Definition: response_cache.h:64
mrs::database::entry::UniversalId UniversalId
Definition: universal_id.h:33
Definition: options.cc:57
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: completion_hash.h:35
Definition: response_cache.h:45
std::string key
Definition: response_cache.h:53
std::chrono::time_point< std::chrono::system_clock > TimeType
Definition: response_cache.h:46
std::shared_ptr< CacheEntry > prev_ptr
Definition: response_cache.h:59
std::optional< std::string > media_type_str
Definition: response_cache.h:51
bool is_expired() const
Definition: response_cache.h:61
EndpointResponseCache * owner
Definition: response_cache.h:56
std::optional< helper::MediaType > media_type
Definition: response_cache.h:50
TimeType expiration_time
Definition: response_cache.h:54
int64_t items
Definition: response_cache.h:49
std::string data
Definition: response_cache.h:48
std::shared_ptr< CacheEntry > next_ptr
Definition: response_cache.h:58