MySQL 9.3.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
62constexpr const size_t k_default_object_cache_size = 1000000;
63
65 public:
69
70 explicit ResponseCache(const std::string &config_key)
71 : config_key_(config_key) {}
72
73 void configure(const std::string &options);
74
75 size_t max_cache_size() const { return max_size_; }
76
77 private:
78 void push(std::shared_ptr<CacheEntry> entry);
79 void remove(std::shared_ptr<CacheEntry> entry);
80 void remove_nolock(std::shared_ptr<CacheEntry> entry);
81
83
84 void shrink_object_cache(size_t extra_size = 0);
85
86 std::string config_key_;
87
88 std::shared_ptr<CacheEntry> newest_entry_;
89 std::shared_ptr<CacheEntry> oldest_entry_;
90 std::mutex entries_mutex_;
91 std::atomic<size_t> cache_size_ = 0;
92
94};
95
97 public:
100
101 protected:
102 EndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms);
103 virtual ~EndpointResponseCache() = default;
104
105 std::shared_ptr<CacheEntry> create_entry(
106 const std::string &key, const std::string &data, int64_t items = 0,
107 std::optional<helper::MediaType> media_type = {},
108 std::optional<std::string> media_type_str = {});
109
110 void remove_entry(std::shared_ptr<CacheEntry> entry, bool ejected);
111 virtual void remove_entry_nolock(std::shared_ptr<CacheEntry> entry,
112 bool ejected);
113
114 std::shared_ptr<CacheEntry> lookup(const std::string &key);
115
116 friend class ResponseCache;
117
119
121
122 std::unordered_map<std::string, std::shared_ptr<CacheEntry>> cache_;
123 std::shared_mutex cache_mutex_;
124};
125
127 public:
128 ItemEndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms);
130
131 std::shared_ptr<CacheEntry> create_table_entry(const Uri &uri,
132 const std::string &user_id,
133 const std::string &data,
134 int64_t items);
135
136 std::shared_ptr<CacheEntry> create_routine_entry(
137 const Uri &uri, std::string_view req_body, const std::string &data,
138 std::optional<helper::MediaType> media_type = {});
139
140 std::shared_ptr<CacheEntry> create_routine_entry(
141 const Uri &uri, std::string_view req_body, const std::string &data,
142 const std::string &media_type_str);
143
144 std::shared_ptr<CacheEntry> lookup_table(const Uri &uri,
145 const std::string &user_id);
146
147 std::shared_ptr<CacheEntry> lookup_routine(const Uri &uri,
148 std::string_view req_body);
149
150 private:
151 void remove_entry_nolock(std::shared_ptr<CacheEntry> entry,
152 bool ejected) override;
153};
154
156 public:
159
160 std::shared_ptr<CacheEntry> create_file_entry(const UniversalId &id,
161 const std::string &data,
162 helper::MediaType media_type);
163
164 std::shared_ptr<CacheEntry> lookup_file(const UniversalId &id);
165
166 private:
167 void remove_entry_nolock(std::shared_ptr<CacheEntry> entry,
168 bool ejected) override;
169};
170
171} // namespace mrs
172
173#endif // ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_REST_RESPONSE_CACHE_H_
Definition: uri.h:40
Definition: response_cache.h:96
void remove_entry(std::shared_ptr< CacheEntry > entry, bool ejected)
Definition: response_cache.cc:242
std::shared_ptr< CacheEntry > lookup(const std::string &key)
Definition: response_cache.cc:256
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:206
ResponseCache * owner_
Definition: response_cache.h:118
EndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms)
Definition: response_cache.cc:200
std::chrono::milliseconds ttl_
Definition: response_cache.h:120
virtual void remove_entry_nolock(std::shared_ptr< CacheEntry > entry, bool ejected)
Definition: response_cache.cc:251
std::shared_mutex cache_mutex_
Definition: response_cache.h:123
std::unordered_map< std::string, std::shared_ptr< CacheEntry > > cache_
Definition: response_cache.h:122
::mrs::database::entry::UniversalId UniversalId
Definition: response_cache.h:99
virtual ~EndpointResponseCache()=default
Definition: response_cache.h:155
std::shared_ptr< CacheEntry > lookup_file(const UniversalId &id)
Definition: response_cache.cc:359
FileEndpointResponseCache(ResponseCache *owner)
Definition: response_cache.cc:356
std::shared_ptr< CacheEntry > create_file_entry(const UniversalId &id, const std::string &data, helper::MediaType media_type)
Definition: response_cache.cc:381
~FileEndpointResponseCache() override
Definition: response_cache.cc:369
void remove_entry_nolock(std::shared_ptr< CacheEntry > entry, bool ejected) override
Definition: response_cache.cc:392
Definition: response_cache.h:126
std::shared_ptr< CacheEntry > lookup_routine(const Uri &uri, std::string_view req_body)
Definition: response_cache.cc:338
ItemEndpointResponseCache(ResponseCache *owner, uint64_t ttl_ms)
Definition: response_cache.cc:278
void remove_entry_nolock(std::shared_ptr< CacheEntry > entry, bool ejected) override
Definition: response_cache.cc:348
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:294
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:305
std::shared_ptr< CacheEntry > lookup_table(const Uri &uri, const std::string &user_id)
Definition: response_cache.cc:328
~ItemEndpointResponseCache() override
Definition: response_cache.cc:282
Definition: response_cache.h:64
void shrink_object_cache(size_t extra_size=0)
Definition: response_cache.cc:107
std::shared_ptr< CacheEntry > oldest_entry_
Definition: response_cache.h:89
std::string config_key_
Definition: response_cache.h:86
ResponseCache(const std::string &config_key)
Definition: response_cache.h:70
std::atomic< size_t > max_size_
Definition: response_cache.h:93
void configure(const std::string &options)
Definition: response_cache.cc:94
void remove_nolock(std::shared_ptr< CacheEntry > entry)
Definition: response_cache.cc:145
void remove(std::shared_ptr< CacheEntry > entry)
Definition: response_cache.cc:138
int remove_all(EndpointResponseCache *cache)
Definition: response_cache.cc:159
std::shared_ptr< CacheEntry > newest_entry_
Definition: response_cache.h:88
size_t max_cache_size() const
Definition: response_cache.h:75
void push(std::shared_ptr< CacheEntry > entry)
Definition: response_cache.cc:121
std::atomic< size_t > cache_size_
Definition: response_cache.h:91
std::mutex entries_mutex_
Definition: response_cache.h:90
MediaType
Definition: media_type.h:33
Request::Uri Uri
Definition: request.cc:36
std::chrono::milliseconds milliseconds
Definition: authorize_manager.cc:68
Definition: authorize_manager.h:48
constexpr const size_t k_default_object_cache_size
Definition: response_cache.h:62
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
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