Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ResponseExpirationCache.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Mutex.hpp"
23
24#include <boost/json/object.hpp>
25
26#include <chrono>
27#include <optional>
28#include <shared_mutex>
29#include <string>
30#include <unordered_map>
31#include <unordered_set>
32
33namespace util {
34
42 class Entry {
43 std::chrono::steady_clock::time_point lastUpdated_;
44 std::optional<boost::json::object> response_;
45
46 public:
52 void
53 put(boost::json::object response);
54
60 std::optional<boost::json::object>
61 get() const;
62
68 std::chrono::steady_clock::time_point
69 lastUpdated() const;
70
74 void
75 invalidate();
76 };
77
78 std::chrono::steady_clock::duration cacheTimeout_;
79 std::unordered_map<std::string, util::Mutex<Entry, std::shared_mutex>> cache_;
80
81 bool
82 shouldCache(std::string const& cmd);
83
84public:
92 std::chrono::steady_clock::duration cacheTimeout,
93 std::unordered_set<std::string> const& cmds
94 )
95 : cacheTimeout_(cacheTimeout)
96 {
97 for (auto const& command : cmds) {
98 cache_.emplace(command, Entry{});
99 }
100 }
101
108 [[nodiscard]] std::optional<boost::json::object>
109 get(std::string const& cmd) const;
110
117 void
118 put(std::string const& cmd, boost::json::object const& response);
119
123 void
124 invalidate();
125};
126} // namespace util
Cache of requests' responses with TTL support and configurable cachable commands.
Definition ResponseExpirationCache.hpp:38
void invalidate()
Invalidate all entries in the cache.
Definition ResponseExpirationCache.cpp:93
std::optional< boost::json::object > get(std::string const &cmd) const
Get a response from the cache.
Definition ResponseExpirationCache.cpp:67
ResponseExpirationCache(std::chrono::steady_clock::duration cacheTimeout, std::unordered_set< std::string > const &cmds)
Construct a new Cache object.
Definition ResponseExpirationCache.hpp:91
void put(std::string const &cmd, boost::json::object const &response)
Put a response into the cache if the request should be cached.
Definition ResponseExpirationCache.cpp:81
This namespace contains various utilities.
Definition AccountUtils.hpp:30