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

**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 the specified entry.

If the value is costly to compute, consider using [`get_or_set_with()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/simple/fn.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_

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

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

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

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

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

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

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

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