---
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/rust/fastly/cache/simple/fn.get_or_set/
---

**Kind:** Function  
**Module:** [simple](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/simple/)  
**Package:** fastly 0.13.1  
**Source:** https://docs.rs/crate/fastly/0.13.1/source/src/cache/simple.rs#116

```rust
fn get_or_set(key: impl Into<CacheKey>, value: impl Into<Body>, ttl: Duration) -> Result<Body, CacheError>
```

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/rust/fastly/cache/simple/fn.get_or_set_with/) instead to avoid
computation in the case where the value is already present.

```rust
let value = get_or_set("my_key", "hello!", Duration::from_secs(60))?;
let cached_string = value.into_string();
println!("the cached string was: {cached_string}");
```

#### Argument type conversions

See the `From` impls for [`Bytes`](https://docs.rs/bytes/latest/bytes/bytes/struct.Bytes.html) and [`Body`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/http/body/struct.Body/) to see which types
can be used as a key or a body, respectively.

**Parameters**

- `key`: `impl Into<CacheKey>`
- `value`: `impl Into<Body>`
- `ttl`: `std::time::Duration`

**Returns**

`Result<crate::Body, CacheError>`
