---
title: insert
summary: Returns an InsertBuilder that will perform a non-transactional cache insertion.
url: https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/fn.insert/
---

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

```rust
fn insert(key: CacheKey, max_age: Duration) -> InsertBuilder
```

Returns an [`InsertBuilder`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/struct.InsertBuilder/) that will perform a non-transactional cache insertion.

The required `max_age` argument is the maximal "time to live" for the cache item:
the time for which the item will be considered fresh, starting from the start of its history.
(The start of history defaults to "now" unless [initial_age](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/struct.InsertBuilder/#method-initial_age) is provided.)
All other insertion arguments are optional, and may be set using the returned builder.

```rust
let contents = b"my cached object";
let mut writer = insert(CacheKey::from_static(b"my_key"), Duration::from_secs(3600))
    .surrogate_keys(["my_key"])
    .known_length(contents.len() as u64)
    .execute()?;
writer.write_all(contents)?;
writer.finish()?;
```

#### Relationship with [`Transaction::lookup()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/struct.Transaction/#method-lookup)

Like [`lookup()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/fn.lookup/), [`insert()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/fn.insert/) may race with concurrent lookups or insertions, and will
unconditionally overwrite existing cached items rather than allowing for revalidation of an
existing object.

The transactional equivalent of this function is [`Transaction::insert()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/struct.Transaction/#method-insert), which may only be
called following a transactional lookup when [`Transaction::must_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/fastly/cache/core/struct.Transaction/#method-must_insert_or_update) returns
`true`.

**Parameters**

- `key`: `CacheKey`
- `max_age`: `std::time::Duration`

**Returns**

`InsertBuilder`
