---
title: get_or_set_with
summary: Get the entry associated with the given cache key if it exists, or insert and return an entry specified by running the given closure.
url: https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/simple/fn.get_or_set_with/
---

**Kind:** Function  
**Module:** [simple](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/simple/)  
**Package:** compute-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`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/simple/class.CacheEntry/) describing the entry to set, or `std::nullopt` in the case of error.

Example successful insertion:

_Overload 1 of 2_

```cpp
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>>
```

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/cache/simple.h#L190

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`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/simple/class.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>`

```cpp
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_

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

**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/cache/simple.h#L266

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`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/simple/class.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>`

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