Fastly Compute C++ SDK
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1#ifndef FASTLY_CACHE_CORE_H
2#define FASTLY_CACHE_CORE_H
3
4#include <chrono>
5#include <cstdint>
6#include <fastly/expected.h>
7#include <fastly/http/body.h>
10#include <optional>
11#include <span>
12#include <string>
13#include <string_view>
14#include <variant>
15#include <vector>
16
18
21public:
33 CacheError(Code code) : code_(code) {}
34 Code code() const { return code_; }
35
36private:
37 Code code_;
38};
39
40namespace detail {
42public:
43 explicit CacheHandle(std::uint32_t handle) : handle_(handle) {}
44 std::uint32_t handle() const { return handle_; }
46
47private:
48 std::uint32_t handle_;
49};
50
52public:
53 explicit CacheReplaceHandle(std::uint32_t handle) : handle_(handle) {}
54 std::uint32_t handle() const { return handle_; }
56
57private:
58 std::uint32_t handle_;
59};
60
62public:
63 explicit CacheBusyHandle(std::uint32_t handle) : handle_(handle) {}
64 std::uint32_t handle() const { return handle_; }
66
67private:
68 std::uint32_t handle_;
69};
70
71// TODO: this should really live somewhere else, but for now it's the only
72// place that we need it, so we'll take an ad-hoc approach.
74public:
76 explicit RequestHandle(std::uint32_t handle) : handle_(handle) {}
77 std::uint32_t handle() const { return handle_; }
79
80private:
81 std::uint32_t handle_;
82};
83} // namespace detail
84
87 std::optional<detail::RequestHandle> request_headers;
88 std::optional<std::string> service;
90};
91
121
124 std::optional<detail::RequestHandle> request_headers;
126 std::optional<std::string> service;
128};
129
132 std::chrono::nanoseconds max_age;
133 std::optional<detail::RequestHandle> request_headers;
134 std::optional<std::string> vary_rule;
135 std::optional<std::chrono::nanoseconds> initial_age;
136 std::optional<std::chrono::nanoseconds> stale_while_revalidate;
137 std::optional<std::string> surrogate_keys;
138 std::optional<std::uint64_t> length;
139 std::optional<std::vector<std::uint8_t>> user_metadata;
140 bool sensitive_data = false;
141 std::optional<std::chrono::nanoseconds> edge_max_age;
142 std::optional<std::string> service;
143 explicit WriteOptions(std::chrono::nanoseconds max_age) : max_age(max_age) {}
144};
145
151class TransactionUpdateBuilder {
152public:
178 TransactionUpdateBuilder vary_by(std::vector<std::string> headers) &&;
179
184 TransactionUpdateBuilder age(std::chrono::nanoseconds age) &&;
185
187 TransactionUpdateBuilder
188 deliver_node_max_age(std::chrono::nanoseconds duration) &&;
189
201 TransactionUpdateBuilder
202 stale_while_revalidate(std::chrono::nanoseconds duration) &&;
203
219 TransactionUpdateBuilder surrogate_keys(std::vector<std::string> keys) &&;
220
222 TransactionUpdateBuilder user_metadata(std::vector<std::uint8_t> metadata) &&;
223
234 TransactionUpdateBuilder on_behalf_of(std::string service) &&;
235
239 tl::expected<void, CacheError> execute() &&;
240
241private:
242 TransactionUpdateBuilder(std::shared_ptr<detail::CacheHandle> handle,
243 WriteOptions options)
244 : handle_(std::move(handle)), options_(std::move(options)) {}
245
246 std::shared_ptr<detail::CacheHandle> handle_;
247 WriteOptions options_;
248 friend class Transaction;
249};
250
256class TransactionInsertBuilder {
257public:
264 TransactionInsertBuilder vary_by(std::vector<std::string> headers) &&;
265
268 TransactionInsertBuilder initial_age(std::chrono::nanoseconds age) &&;
269
281 TransactionInsertBuilder
282 stale_while_revalidate(std::chrono::nanoseconds duration) &&;
283
299 TransactionInsertBuilder surrogate_keys(std::vector<std::string> keys) &&;
300
309 TransactionInsertBuilder known_length(std::uint64_t length) &&;
310
312 TransactionInsertBuilder user_metadata(std::vector<std::uint8_t> metadata) &&;
313
321 TransactionInsertBuilder sensitive_data(bool is_sensitive) &&;
322
324 TransactionInsertBuilder
325 deliver_node_max_age(std::chrono::nanoseconds duration) &&;
326
337 TransactionInsertBuilder on_behalf_of(std::string service) &&;
338
348 tl::expected<http::StreamingBody, CacheError> execute() &&;
349
365 tl::expected<std::pair<http::StreamingBody, Found>, CacheError>
367
368private:
369 TransactionInsertBuilder(std::shared_ptr<detail::CacheHandle> handle,
370 WriteOptions options)
371 : handle_(std::move(handle)), options_(std::move(options)) {}
372
373 std::shared_ptr<detail::CacheHandle> handle_;
374 WriteOptions options_;
375 friend class Transaction;
376};
377
378class Transaction;
380
389class PendingTransaction {
390public:
398 tl::expected<bool, CacheError> pending() const;
399
403 tl::expected<Transaction, CacheError> wait() &&;
404
405private:
406 explicit PendingTransaction(std::shared_ptr<detail::CacheBusyHandle> handle)
407 : handle_(std::move(handle)) {}
408
409 std::shared_ptr<detail::CacheBusyHandle> handle_;
411};
412
414class TransactionLookupBuilder {
415public:
438 TransactionLookupBuilder
439 header_values(std::string_view name,
440 std::span<const http::HeaderValue> values) &&;
441
444
467 TransactionLookupBuilder header(std::string_view name,
468 const http::HeaderValue &value) &&;
469
482 TransactionLookupBuilder on_behalf_of(std::string service) &&;
483
506 TransactionLookupBuilder always_use_requested_range() &&;
507
512 tl::expected<Transaction, CacheError> execute() &&;
513
520 tl::expected<PendingTransaction, CacheError> execute_async() &&;
521
522private:
523 explicit TransactionLookupBuilder(std::vector<std::uint8_t> key) : key_(std::move(key)) {}
524 std::vector<std::uint8_t> key_;
525 LookupOptions options_;
526 friend class Transaction;
527};
528
534class Found {
535public:
537 std::chrono::nanoseconds age() const;
538
544 std::chrono::nanoseconds max_age() const;
545
551 std::chrono::nanoseconds remaining_ttl() const;
552
555 std::chrono::nanoseconds stale_while_revalidate() const;
556
560 bool is_stale() const;
561
566 bool is_usable() const;
567
577 uint64_t hits() const;
578
583 std::optional<uint64_t> known_length() const;
584
586 std::vector<uint8_t> user_metadata() const;
587
596 tl::expected<Body, CacheError> to_stream() const;
597
638 tl::expected<Body, CacheError>
639 to_stream_from_range(std::optional<uint64_t> from,
640 std::optional<uint64_t> to) const;
641
642private:
643 explicit Found(std::shared_ptr<detail::CacheHandle> handle)
644 : handle_(std::move(handle)) {}
645 explicit Found(std::shared_ptr<detail::CacheReplaceHandle> handle)
646 : handle_(std::move(handle)) {}
647
648 detail::CacheHandle *handle() const {
649 auto ptr = std::get_if<std::shared_ptr<detail::CacheHandle>>(&handle_);
650 return ptr ? ptr->get() : nullptr;
651 }
652 detail::CacheReplaceHandle *replace_handle() const {
653 auto ptr =
654 std::get_if<std::shared_ptr<detail::CacheReplaceHandle>>(&handle_);
655 return ptr ? ptr->get() : nullptr;
656 }
657
658 std::variant<std::shared_ptr<detail::CacheHandle>,
659 std::shared_ptr<detail::CacheReplaceHandle>>
660 handle_;
661 friend class Transaction;
662 friend class LookupBuilder;
664 friend class ReplaceBuilder;
665};
666
672class Transaction {
673public:
676 static TransactionLookupBuilder lookup(std::span<const std::uint8_t> key);
677
679 static TransactionLookupBuilder lookup(std::string_view key) {
680 return lookup(std::span(reinterpret_cast<const std::uint8_t *>(key.data()),
681 key.size()));
682 }
683
689 std::optional<Found> found() const;
690
696 bool must_insert() const;
697
707
717 tl::expected<void, CacheError> cancel_insert_or_update() const;
718
732 TransactionUpdateBuilder update(std::chrono::nanoseconds max_age) &&;
733
740 TransactionInsertBuilder insert(std::chrono::nanoseconds max_age) &&;
741
742private:
743 explicit Transaction(std::shared_ptr<detail::CacheHandle> handle)
744 : handle_(std::move(handle)) {}
745
746 std::shared_ptr<detail::CacheHandle> handle_;
748 friend class PendingTransaction;
749};
750
761class LookupBuilder {
762public:
785 LookupBuilder header_values(std::string_view name,
786 std::span<const http::HeaderValue> values) &&;
787
790
813 LookupBuilder header(std::string_view name,
814 const http::HeaderValue &value) &&;
815
826 LookupBuilder on_behalf_of(std::string service) &&;
827
850 LookupBuilder always_use_requested_range() &&;
851
858 tl::expected<std::optional<Found>, CacheError> execute() &&;
859
860private:
861 explicit LookupBuilder(std::vector<std::uint8_t> key) : key_(std::move(key)) {}
862 std::vector<std::uint8_t> key_;
863 LookupOptions options_;
864 friend LookupBuilder lookup(std::span<const std::uint8_t> key);
865};
866
880LookupBuilder lookup(std::span<const std::uint8_t> key);
881
883inline LookupBuilder lookup(std::string_view key) {
884 return lookup(std::span(reinterpret_cast<const std::uint8_t *>(key.data()),
885 key.size()));
886}
887
897class InsertBuilder {
898public:
921 InsertBuilder header_values(std::string_view name,
922 std::span<const http::HeaderValue> values) &&;
923
926
949 InsertBuilder header(std::string_view name,
950 const http::HeaderValue &value) &&;
951
953 InsertBuilder vary_by(std::vector<std::string> headers) &&;
954
959 InsertBuilder initial_age(std::chrono::nanoseconds age) &&;
960
963 InsertBuilder stale_while_revalidate(std::chrono::nanoseconds duration) &&;
964
980 InsertBuilder surrogate_keys(std::vector<std::string> keys) &&;
981
983 InsertBuilder known_length(std::uint64_t length) &&;
984
986 InsertBuilder user_metadata(std::vector<std::uint8_t> metadata) &&;
987
995 InsertBuilder sensitive_data(bool is_sensitive) &&;
996
998 InsertBuilder deliver_node_max_age(std::chrono::nanoseconds duration) &&;
999
1010 InsertBuilder on_behalf_of(std::string service) &&;
1011
1014 tl::expected<http::StreamingBody, CacheError> execute() &&;
1015
1016private:
1017 explicit InsertBuilder(std::vector<std::uint8_t> key, WriteOptions options)
1018 : key_(std::move(key)), options_(std::move(options)) {}
1019 std::vector<std::uint8_t> key_;
1020 WriteOptions options_;
1021 friend InsertBuilder insert(std::span<const std::uint8_t> key,
1022 std::chrono::nanoseconds max_age);
1023};
1024
1031InsertBuilder insert(std::span<const std::uint8_t> key,
1032 std::chrono::nanoseconds max_age);
1033
1035inline InsertBuilder insert(std::string_view key,
1036 std::chrono::nanoseconds max_age) {
1037 return insert(std::span(reinterpret_cast<const std::uint8_t *>(key.data()),
1038 key.size()),
1039 max_age);
1040}
1041
1042class Replace;
1043
1045class ReplaceBuilder {
1046public:
1062 tl::expected<Replace, CacheError> begin() &&;
1063
1065 ReplaceBuilder replace_strategy(ReplaceStrategy strategy) &&;
1066
1089 ReplaceBuilder always_use_requested_range() &&;
1090
1091private:
1092 explicit ReplaceBuilder(std::vector<std::uint8_t> key) : key_(std::move(key)) {}
1093 std::vector<std::uint8_t> key_;
1094 ReplaceOptions options_;
1095 friend ReplaceBuilder replace(std::span<const std::uint8_t> key);
1096};
1097
1101class Replace {
1102public:
1109 tl::expected<http::StreamingBody, CacheError>
1110 execute(std::chrono::nanoseconds max_age) &&;
1111
1113 const std::optional<Found> &existing_object() const {
1114 return existing_object_;
1115 }
1116
1139 Replace header_values(std::string_view name,
1140 std::span<const http::HeaderValue> values) &&;
1141
1144
1166 Replace header(std::string_view name, const http::HeaderValue &value) &&;
1167
1169 Replace vary_by(std::vector<std::string> headers) &&;
1170
1173 Replace initial_age(std::chrono::nanoseconds age) &&;
1174
1177 Replace stale_while_revalidate(std::chrono::nanoseconds duration) &&;
1178
1194 Replace surrogate_keys(std::vector<std::string> keys) &&;
1195
1197 Replace known_length(std::uint64_t length) &&;
1198
1200 Replace user_metadata(std::vector<std::uint8_t> metadata) &&;
1201
1209 Replace sensitive_data(bool is_sensitive) &&;
1210
1212 Replace deliver_node_max_age(std::chrono::nanoseconds duration) &&;
1213
1214private:
1215 Replace(std::shared_ptr<detail::CacheReplaceHandle> handle,
1216 std::optional<Found> existing_object)
1217 : handle_(std::move(handle)),
1218 existing_object_(std::move(existing_object)) {}
1219
1220 std::shared_ptr<detail::CacheReplaceHandle> handle_;
1221 std::optional<Found> existing_object_;
1222 WriteOptions options_{std::chrono::nanoseconds(0)};
1223 friend class ReplaceBuilder;
1224};
1225
1228ReplaceBuilder replace(std::span<const std::uint8_t> key);
1229
1231inline ReplaceBuilder replace(std::string_view key) {
1232 return replace(std::span(reinterpret_cast<const std::uint8_t *>(key.data()),
1233 key.size()));
1234}
1235
1236} // namespace fastly::cache::core
1237
1238#endif // FASTLY_CACHE_H
Errors that can arise during cache operations.
Definition core.h:20
@ Unsupported
Cache operation is not supported.
Definition core.h:29
@ Unknown
An unknown error occurred.
Definition core.h:31
@ InvalidOperation
Definition core.h:27
@ LimitExceeded
Operation failed due to a limit.
Definition core.h:24
Code code() const
Definition core.h:34
CacheError(Code code)
Definition core.h:33
Definition core.h:534
friend class TransactionInsertBuilder
Definition core.h:663
std::chrono::nanoseconds stale_while_revalidate() const
std::optional< uint64_t > known_length() const
std::chrono::nanoseconds remaining_ttl() const
friend class Transaction
Definition core.h:661
std::chrono::nanoseconds max_age() const
friend class ReplaceBuilder
Definition core.h:664
tl::expected< Body, CacheError > to_stream() const
std::chrono::nanoseconds age() const
The current age of the cached item.
std::vector< uint8_t > user_metadata() const
The user-controlled metadata associated with the cached item.
friend class LookupBuilder
Definition core.h:662
tl::expected< Body, CacheError > to_stream_from_range(std::optional< uint64_t > from, std::optional< uint64_t > to) const
InsertBuilder initial_age(std::chrono::nanoseconds age) &&
InsertBuilder on_behalf_of(std::string service) &&
InsertBuilder header_values(std::string_view name, std::span< const http::HeaderValue > values) &&
InsertBuilder deliver_node_max_age(std::chrono::nanoseconds duration) &&
Sets the maximum time the cached item may live on a deliver node in a POP.
InsertBuilder sensitive_data(bool is_sensitive) &&
InsertBuilder header(std::string_view name, const http::HeaderValue &value) &&
friend InsertBuilder insert(std::span< const std::uint8_t > key, std::chrono::nanoseconds max_age)
InsertBuilder surrogate_keys(std::vector< std::string > keys) &&
InsertBuilder vary_by(std::vector< std::string > headers) &&
Sets the list of headers that must match when looking up this cached item.
InsertBuilder user_metadata(std::vector< std::uint8_t > metadata) &&
Sets the user-defined metadata to associate with the cached item.
tl::expected< http::StreamingBody, CacheError > execute() &&
InsertBuilder stale_while_revalidate(std::chrono::nanoseconds duration) &&
InsertBuilder known_length(std::uint64_t length) &&
Sets the known length of the cached item.
LookupBuilder on_behalf_of(std::string service) &&
friend LookupBuilder lookup(std::span< const std::uint8_t > key)
LookupBuilder header_values(std::string_view name, std::span< const http::HeaderValue > values) &&
LookupBuilder header(std::string_view name, const http::HeaderValue &value) &&
LookupBuilder always_use_requested_range() &&
tl::expected< std::optional< Found >, CacheError > execute() &&
friend class TransactionLookupBuilder
Definition core.h:410
tl::expected< Transaction, CacheError > wait() &&
tl::expected< bool, CacheError > pending() const
A builder-style API for configuring a non-transactional cache replacement.
Definition core.h:1045
ReplaceBuilder always_use_requested_range() &&
tl::expected< Replace, CacheError > begin() &&
friend ReplaceBuilder replace(std::span< const std::uint8_t > key)
ReplaceBuilder replace_strategy(ReplaceStrategy strategy) &&
Sets the strategy for performing the replace.
Replace initial_age(std::chrono::nanoseconds age) &&
Replace known_length(std::uint64_t length) &&
Sets the known length of the cached item.
const std::optional< Found > & existing_object() const
The existing object, if one exists. The existing object may be stale.
Definition core.h:1113
Replace stale_while_revalidate(std::chrono::nanoseconds duration) &&
Replace user_metadata(std::vector< std::uint8_t > metadata) &&
Sets the user-defined metadata to associate with the cached item.
Replace sensitive_data(bool is_sensitive) &&
friend class ReplaceBuilder
Definition core.h:1223
Replace surrogate_keys(std::vector< std::string > keys) &&
Replace deliver_node_max_age(std::chrono::nanoseconds duration) &&
Sets the maximum time the cached item may live on a deliver node in a POP.
tl::expected< http::StreamingBody, CacheError > execute(std::chrono::nanoseconds max_age) &&
Replace header(std::string_view name, const http::HeaderValue &value) &&
Replace header_values(std::string_view name, std::span< const http::HeaderValue > values) &&
Replace vary_by(std::vector< std::string > headers) &&
Sets the list of headers that must match when looking up this cached item.
TransactionInsertBuilder on_behalf_of(std::string service) &&
friend class Transaction
Definition core.h:375
TransactionInsertBuilder vary_by(std::vector< std::string > headers) &&
TransactionInsertBuilder stale_while_revalidate(std::chrono::nanoseconds duration) &&
TransactionInsertBuilder initial_age(std::chrono::nanoseconds age) &&
TransactionInsertBuilder surrogate_keys(std::vector< std::string > keys) &&
tl::expected< http::StreamingBody, CacheError > execute() &&
TransactionInsertBuilder deliver_node_max_age(std::chrono::nanoseconds duration) &&
Sets the maximum time the cached item may live on a deliver node in a POP.
TransactionInsertBuilder user_metadata(std::vector< std::uint8_t > metadata) &&
Sets the user-defined metadata to associate with the cached item.
TransactionInsertBuilder sensitive_data(bool is_sensitive) &&
tl::expected< std::pair< http::StreamingBody, Found >, CacheError > execute_and_stream_back() &&
TransactionInsertBuilder known_length(std::uint64_t length) &&
TransactionLookupBuilder header_values(std::string_view name, std::span< const http::HeaderValue > values) &&
TransactionLookupBuilder header(std::string_view name, const http::HeaderValue &value) &&
friend class Transaction
Definition core.h:526
tl::expected< Transaction, CacheError > execute() &&
TransactionLookupBuilder always_use_requested_range() &&
tl::expected< PendingTransaction, CacheError > execute_async() &&
TransactionLookupBuilder on_behalf_of(std::string service) &&
TransactionUpdateBuilder vary_by(std::vector< std::string > headers) &&
TransactionUpdateBuilder surrogate_keys(std::vector< std::string > keys) &&
friend class Transaction
Definition core.h:248
tl::expected< void, CacheError > execute() &&
TransactionUpdateBuilder age(std::chrono::nanoseconds age) &&
TransactionUpdateBuilder stale_while_revalidate(std::chrono::nanoseconds duration) &&
TransactionUpdateBuilder on_behalf_of(std::string service) &&
TransactionUpdateBuilder deliver_node_max_age(std::chrono::nanoseconds duration) &&
Sets the maximum time the cached item may live on a deliver node in a POP.
TransactionUpdateBuilder user_metadata(std::vector< std::uint8_t > metadata) &&
Sets the user-defined metadata to associate with the cached item.
friend class TransactionLookupBuilder
Definition core.h:747
std::optional< Found > found() const
static TransactionLookupBuilder lookup(std::string_view key)
Transactional cache lookup with string key.
Definition core.h:679
TransactionUpdateBuilder update(std::chrono::nanoseconds max_age) &&
TransactionInsertBuilder insert(std::chrono::nanoseconds max_age) &&
tl::expected< void, CacheError > cancel_insert_or_update() const
static TransactionLookupBuilder lookup(std::span< const std::uint8_t > key)
friend class PendingTransaction
Definition core.h:748
CacheBusyHandle(std::uint32_t handle)
Definition core.h:63
std::uint32_t handle() const
Definition core.h:64
CacheHandle(std::uint32_t handle)
Definition core.h:43
std::uint32_t handle() const
Definition core.h:44
CacheReplaceHandle(std::uint32_t handle)
Definition core.h:53
std::uint32_t handle() const
Definition core.h:54
RequestHandle(std::uint32_t handle)
Definition core.h:76
std::uint32_t handle() const
Definition core.h:77
Represents a single HTTP header value.
Definition header.h:15
Definition core.h:40
Definition core.h:17
ReplaceStrategy
Strategy to use when replacing an existing cached object.
Definition core.h:93
@ Immediate
Definition core.h:104
@ ImmediateForceMiss
Definition core.h:109
InsertBuilder insert(std::span< const std::uint8_t > key, std::chrono::nanoseconds max_age)
ReplaceBuilder replace(std::span< const std::uint8_t > key)
LookupBuilder lookup(std::span< const std::uint8_t > key)
Options for cache lookup operations.
Definition core.h:86
std::optional< detail::RequestHandle > request_headers
Definition core.h:87
bool always_use_requested_range
Definition core.h:89
std::optional< std::string > service
Definition core.h:88
Options for cache replace operations.
Definition core.h:123
std::optional< detail::RequestHandle > request_headers
Definition core.h:124
std::optional< std::string > service
Definition core.h:126
bool always_use_requested_range
Definition core.h:127
ReplaceStrategy replace_strategy
Definition core.h:125
Options for cache write operations (insert/update).
Definition core.h:131
std::optional< std::vector< std::uint8_t > > user_metadata
Definition core.h:139
std::optional< std::string > surrogate_keys
Definition core.h:137
bool sensitive_data
Definition core.h:140
std::optional< detail::RequestHandle > request_headers
Definition core.h:133
std::optional< std::string > service
Definition core.h:142
std::chrono::nanoseconds max_age
Definition core.h:132
WriteOptions(std::chrono::nanoseconds max_age)
Definition core.h:143
std::optional< std::chrono::nanoseconds > edge_max_age
Definition core.h:141
std::optional< std::chrono::nanoseconds > initial_age
Definition core.h:135
std::optional< std::string > vary_rule
Definition core.h:134
std::optional< std::chrono::nanoseconds > stale_while_revalidate
Definition core.h:136
std::optional< std::uint64_t > length
Definition core.h:138