get_or_set_with

Functionin simplecompute-sdk-cpp 0.7.0

Get the entry associated with the given cache key if it exists, or insert and return an entry specified by running the given closure.

The closure is only run when no value is present for the key, and no other client is in the process of setting it. It takes no arguments, and returns either a CacheEntry describing the entry to set, or std::nullopt in the case of error.

Example successful insertion:

Overload 1 of 2

template<class F> tl::expected<std::optional<http::Body>, CacheError> fastly::cache::simple::get_or_set_with(std::span<const std::uint8_t> key, F make_entry) requires std::invocable<F> && std::same_as<std::invoke_result_t<F>, std::optional<CacheEntry>>
View source

Get the entry associated with the given cache key if it exists, or insert and return an entry specified by running the given closure.

The closure is only run when no value is present for the key, and no other client is in the process of setting it. It takes no arguments, and returns either a CacheEntry describing the entry to set, or std::nullopt in the case of error.

Example successful insertion:

Parameters

key: std::span<const std::uint8_t>
make_entry: F

Returns

tl::expected<std::optional<http::Body>, CacheError>
std::vector<std::uint8_t> key_bytes = {0x01, 0x02, 0x03};
auto value = get_or_set_with(key_bytes, []() -> std::optional<CacheEntry> {
return CacheEntry{http::Body("hello!"), std::chrono::nanoseconds(6000)};
}).value();

Overload 2 of 2

template<class F> tl::expected<std::optional<http::Body>, CacheError> fastly::cache::simple::get_or_set_with(std::string_view key, F make_entry)
View source

Get the entry associated with the given cache key if it exists, or insert and return an entry specified by running the given closure.

The closure is only run when no value is present for the key, and no other client is in the process of setting it. It takes no arguments, and returns either a CacheEntry describing the entry to set, or std::nullopt in the case of error.

Example successful insertion:

Parameters

key: std::string_view
make_entry: F

Returns

tl::expected<std::optional<http::Body>, CacheError>
auto value = get_or_set_with("my_key", []() -> std::optional<CacheEntry> {
return CacheEntry{http::Body("hello!"), std::chrono::nanoseconds(6000)};
}).value();