---
title: Transaction
summary: A cache transaction resulting from a transactional lookup.
url: https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/
---

**Kind:** Class  
**Module:** [core](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/)  
**Package:** compute-sdk-cpp 0.7.0  
**Source:** https://github.com/fastly/compute-sdk-cpp/blob/ff911e47be2f319e83a9cde960d2c3c5db534280/include/fastly/cache/core.h#L672

Include `fastly/cache/core.h` to use this class.

A cache transaction resulting from a transactional lookup.

This type is returned from [`TransactionLookupBuilder::execute()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.TransactionLookupBuilder/#method-execute) and provides access to the result of the lookup as well as methods to insert or update cached items when required.

## Methods

### cancel_insert_or_update

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

```cpp
tl::expected<void, CacheError> fastly::cache::core::Transaction::cancel_insert_or_update() const
```

Cancels the obligation for this transaction client to insert or update a cache item.

If there are concurrent transactional lookups that were blocked waiting on this client to provide the item, one of them will be chosen to be unblocked and given the [`must_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-must_insert_or_update) obligation.

This method should only be called when [`must_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-must_insert_or_update) is true; otherwise, a `CacheError::InvalidOperation` will be returned.

**Returns**

`tl::expected<void, CacheError>`

### found

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

```cpp
std::optional<Found> fastly::cache::core::Transaction::found() const
```

Returns a [`Found`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Found/) object for this cache item, if one is available.

Even if an object is found, the cache item might be stale and require updating. Use [`Transaction::must_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-must_insert_or_update) to determine whether this transaction client is expected to update the cached item.

**Returns**

`std::optional<Found>`

### insert

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

```cpp
TransactionInsertBuilder fastly::cache::core::Transaction::insert(std::chrono::nanoseconds max_age) &&
```

Returns a [`TransactionInsertBuilder`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.TransactionInsertBuilder/) that will perform a transactional cache insertion.

This method should only be called when [`must_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-must_insert_or_update) is true; otherwise, a `CacheError::InvalidOperation` will be returned when attempting to execute the insertion.

**Parameters**

- `max_age`: `std::chrono::nanoseconds`

**Returns**

`TransactionInsertBuilder`

### lookup

**Flags:** static

_Overload 1 of 2_

```cpp
static TransactionLookupBuilder fastly::cache::core::Transaction::lookup(std::span<const std::uint8_t> key)
```

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

Returns a [`TransactionLookupBuilder`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.TransactionLookupBuilder/) that will perform a transactional cache lookup.

**Parameters**

- `key`: `std::span<const std::uint8_t>`

**Returns**

`TransactionLookupBuilder`

_Overload 2 of 2_

```cpp
static TransactionLookupBuilder fastly::cache::core::Transaction::lookup(std::string_view key)
```

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

Transactional cache lookup with string key.

**Parameters**

- `key`: `std::string_view`

**Returns**

`TransactionLookupBuilder`

### must_insert

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

```cpp
bool fastly::cache::core::Transaction::must_insert() const
```

Returns `true` if a usable cached item was not found, and this transaction client is expected to insert one.

Use [`insert()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-insert) to insert the cache item, or [`cancel_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-cancel_insert_or_update) to exit the transaction without providing an item.

**Returns**

`bool`

### must_insert_or_update

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

```cpp
bool fastly::cache::core::Transaction::must_insert_or_update() const
```

Returns `true` if a fresh cache item was not found, and this transaction client is expected to insert a new item or update a stale item.

Use:
- [`update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-update) to freshen a found item by updating its metadata
- [`insert()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-insert) to insert a new item (including object data)
- [`cancel_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-cancel_insert_or_update) to exit the transaction without providing an item

**Returns**

`bool`

### update

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

```cpp
TransactionUpdateBuilder fastly::cache::core::Transaction::update(std::chrono::nanoseconds max_age) &&
```

Returns a [`TransactionUpdateBuilder`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.TransactionUpdateBuilder/) that will perform a transactional cache update.

Updating an item freshens it by updating its metadata, e.g. its age, without changing the object itself.

This method should only be called when [`must_insert_or_update()`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.Transaction/#method-must_insert_or_update) is true AND the item is found. Otherwise, a `CacheError::InvalidOperation` will be returned when attempting to execute the update.

**Important note**: The [`TransactionUpdateBuilder`](https://www.fastly.com/documentation/reference/compute/sdks/cpp/fastly/cache/core/class.TransactionUpdateBuilder/) will replace ALL of the configuration in the underlying cache item; if any configuration is not set on the builder, it will revert to the default value.

**Parameters**

- `max_age`: `std::chrono::nanoseconds`

**Returns**

`TransactionUpdateBuilder`
