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 "rpc/Errors.hpp"
23#include "util/BlockingCache.hpp"
24
25#include <boost/asio/spawn.hpp>
26#include <boost/json/array.hpp>
27#include <boost/json/object.hpp>
28
29#include <chrono>
30#include <memory>
31#include <string>
32#include <unordered_map>
33#include <unordered_set>
34
35namespace util {
36
46public:
50 struct EntryData {
51 std::chrono::steady_clock::time_point lastUpdated;
52 boost::json::object response;
53 };
54
58 struct Error {
60 boost::json::array warnings;
61
62 bool
63 operator==(Error const&) const = default;
64 };
65
67
68private:
69 std::chrono::steady_clock::duration cacheTimeout_;
70 std::unordered_map<std::string, std::unique_ptr<CacheEntry>> cache_;
71
72public:
80 std::chrono::steady_clock::duration cacheTimeout,
81 std::unordered_set<std::string> const& cmds
82 );
83
90 bool
91 shouldCache(std::string const& cmd);
92
93 using Updater = CacheEntry::Updater;
94 using Verifier = CacheEntry::Verifier;
95
112 [[nodiscard]] std::expected<boost::json::object, Error>
113 getOrUpdate(boost::asio::yield_context yield, std::string const& cmd, Updater updater, Verifier verifier);
114
121 void
122 invalidate();
123};
124} // namespace util
A thread-safe cache that blocks getting operations until the cache is updated.
Definition BlockingCache.hpp:50
std::function< std::expected< ValueType, ErrorType >(boost::asio::yield_context)> Updater
Function type for cache update operations.
Definition BlockingCache.hpp:87
std::function< bool(ValueType const &)> Verifier
Function type to verify if a value should be cached.
Definition BlockingCache.hpp:93
Cache of requests' responses with TTL support and configurable cachable commands.
Definition ResponseExpirationCache.hpp:45
void invalidate()
Invalidate all entries in the cache.
Definition ResponseExpirationCache.cpp:83
bool shouldCache(std::string const &cmd)
Check if the given command should be cached.
Definition ResponseExpirationCache.cpp:47
std::expected< boost::json::object, Error > getOrUpdate(boost::asio::yield_context yield, std::string const &cmd, Updater updater, Verifier verifier)
Get a cached response or update the cache if necessary.
Definition ResponseExpirationCache.cpp:53
ResponseExpirationCache(std::chrono::steady_clock::duration cacheTimeout, std::unordered_set< std::string > const &cmds)
Construct a new ResponseExpirationCache object.
Definition ResponseExpirationCache.cpp:35
This namespace contains various utilities.
Definition AccountUtils.hpp:30
A status returned from any RPC handler.
Definition Errors.hpp:82
A data structure to store a cache entry with its timestamp.
Definition ResponseExpirationCache.hpp:50
std::chrono::steady_clock::time_point lastUpdated
When the entry was last updated.
Definition ResponseExpirationCache.hpp:51
boost::json::object response
The cached response data.
Definition ResponseExpirationCache.hpp:52
A data structure to represent errors that can occur during an update of the cache.
Definition ResponseExpirationCache.hpp:58
boost::json::array warnings
Any warnings related to the request.
Definition ResponseExpirationCache.hpp:60
rpc::Status status
The status code and message of the error.
Definition ResponseExpirationCache.hpp:59