get_or_set

Functionin simplecompute-sdk-cpp 0.7.0

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

If the value is costly to compute, consider using get_or_set_with() instead to avoid computation in the case where the value is already present.

The returned Body is always valid; either the cached value was found and returned, or the new value was inserted and returned.

Example:

Overload 1 of 2

tl::expected<http::Body, CacheError> fastly::cache::simple::get_or_set(std::span<const std::uint8_t> key, http::Body value, std::chrono::nanoseconds ttl)
View source

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

If the value is costly to compute, consider using get_or_set_with() instead to avoid computation in the case where the value is already present.

The returned Body is always valid; either the cached value was found and returned, or the new value was inserted and returned.

Example:

Parameters

key: std::span<const std::uint8_t>
value: http::Body
ttl: std::chrono::nanoseconds

Returns

tl::expected<http::Body, CacheError>
std::vector<std::uint8_t> key_bytes = {0x01, 0x02, 0x03};
auto value = get_or_set(key_bytes, http::Body("hello!"),
std::chrono::nanoseconds(6000)).value();
std::string cached_string = value.take_body_string();

Overload 2 of 2

tl::expected<http::Body, CacheError> fastly::cache::simple::get_or_set(std::string_view key, http::Body value, std::chrono::nanoseconds ttl)
View source

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

If the value is costly to compute, consider using get_or_set_with() instead to avoid computation in the case where the value is already present.

The returned Body is always valid; either the cached value was found and returned, or the new value was inserted and returned.

Example:

Parameters

key: std::string_view
value: http::Body
ttl: std::chrono::nanoseconds

Returns

tl::expected<http::Body, CacheError>
auto value = get_or_set("my_key", http::Body("hello!"),
std::chrono::nanoseconds(6000)).value();
std::string cached_string = value.take_body_string();