Fastly Compute C++ SDK
Loading...
Searching...
No Matches
fastly::cache::simple Namespace Reference

Namespaces

namespace  detail

Classes

class  CacheEntry
 The return type of the closure provided to get_or_set_with(). More...
class  CacheError
 Errors arising from cache operations. More...
class  PurgeOptions
 Options for purge operations. More...

Functions

tl::expected< std::optional< http::Body >, CacheErrorget (std::span< const std::uint8_t > key)
tl::expected< std::optional< http::Body >, CacheErrorget (std::string_view key)
tl::expected< http::Body, CacheErrorget_or_set (std::span< const std::uint8_t > key, http::Body value, std::chrono::nanoseconds ttl)
tl::expected< http::Body, CacheErrorget_or_set (std::string_view key, http::Body value, std::chrono::nanoseconds ttl)
template<class F>
requires std::invocable<F> && std::same_as<std::invoke_result_t<F>, std::optional<CacheEntry>>
tl::expected< std::optional< http::Body >, CacheErrorget_or_set_with (std::span< const std::uint8_t > key, F make_entry)
template<class F>
tl::expected< std::optional< http::Body >, CacheErrorget_or_set_with (std::string_view key, F make_entry)
tl::expected< void, CacheErrorpurge (std::span< const std::uint8_t > key)
tl::expected< void, CacheErrorpurge (std::string_view key)
tl::expected< void, CacheErrorpurge_with_opts (std::span< const std::uint8_t > key, const PurgeOptions &opts)
tl::expected< void, CacheErrorpurge_with_opts (std::string_view key, const PurgeOptions &opts)

Function Documentation

◆ get() [1/2]

tl::expected< std::optional< http::Body >, CacheError > fastly::cache::simple::get ( std::span< const std::uint8_t > key)

Get the entry associated with the given cache key, if it exists.

Returns nullopt if the key is not in the cache, or Body with the cached value if found.

std::vector<std::uint8_t> key_bytes = {0x01, 0x02, 0x03};
auto result = get(key_bytes);
tl::expected< std::optional< http::Body >, CacheError > get(std::span< const std::uint8_t > key)

◆ get() [2/2]

tl::expected< std::optional< http::Body >, CacheError > fastly::cache::simple::get ( std::string_view key)
inline

Get the entry associated with the given cache key, if it exists.

Returns nullopt if the key is not in the cache, or Body with the cached value if found.

auto result = get("my_key");

◆ get_or_set() [1/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 )

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:

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();
Definition body.h:49
tl::expected< http::Body, CacheError > get_or_set(std::span< const std::uint8_t > key, http::Body value, std::chrono::nanoseconds ttl)

◆ get_or_set() [2/2]

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

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:

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

◆ get_or_set_with() [1/2]

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

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:

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();
tl::expected< std::optional< http::Body >, CacheError > get_or_set_with(std::span< const std::uint8_t > key, F make_entry)
Definition simple.h:190

◆ get_or_set_with() [2/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 )

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:

auto value = get_or_set_with("my_key", []() -> std::optional<CacheEntry> {
return CacheEntry{http::Body("hello!"), std::chrono::nanoseconds(6000)};
}).value();
The return type of the closure provided to get_or_set_with().
Definition simple.h:47

◆ purge() [1/2]

tl::expected< void, CacheError > fastly::cache::simple::purge ( std::span< const std::uint8_t > key)

Purge the entry associated with the given cache key.

To configure the behavior of the purge, such as to purge globally rather than within the POP, use purge_with_opts().

Note: Purged values may persist in cache for a short time (~150ms or less) after this function returns.

Example:

std::vector<std::uint8_t> key_bytes = {0x01, 0x02, 0x03};
purge(key_bytes);
tl::expected< void, CacheError > purge(std::span< const std::uint8_t > key)

◆ purge() [2/2]

tl::expected< void, CacheError > fastly::cache::simple::purge ( std::string_view key)
inline

Purge the entry associated with the given cache key.

To configure the behavior of the purge, such as to purge globally rather than within the POP, use purge_with_opts().

Note: Purged values may persist in cache for a short time (~150ms or less) after this function returns.

Example:

purge("my_key");

◆ purge_with_opts() [1/2]

tl::expected< void, CacheError > fastly::cache::simple::purge_with_opts ( std::span< const std::uint8_t > key,
const PurgeOptions & opts )

Purge the entry associated with the given cache key with specific options.

The PurgeOptions argument determines the scope of the purge operation.

Note: Purged values may persist in cache for a short time (~150ms or less) after this function returns.

Example POP-scoped purge (default):

std::vector<std::uint8_t> key_bytes = {0x01, 0x02, 0x03};
// Equivalent to just: purge(key_bytes);
static PurgeOptions pop_scope()
Definition simple.h:77
tl::expected< void, CacheError > purge_with_opts(std::span< const std::uint8_t > key, const PurgeOptions &opts)

Example global-scoped purge:

std::vector<std::uint8_t> key_bytes = {0x01, 0x02, 0x03};
static PurgeOptions global_scope()
Definition simple.h:85

◆ purge_with_opts() [2/2]

tl::expected< void, CacheError > fastly::cache::simple::purge_with_opts ( std::string_view key,
const PurgeOptions & opts )
inline

Purge the entry associated with the given cache key with specific options.

The PurgeOptions argument determines the scope of the purge operation.

Note: Purged values may persist in cache for a short time (~150ms or less) after this function returns.

Example POP-scoped purge (default):

// Equivalent to just: purge("my_key");

Example global-scoped purge: