# Appending data to a cache

## Introduction

We've already noted how the [add to cache](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape) shape can be added to a process flow to cache the entire payload at a given point in the flow. The default behaviour is that when a process flow runs and hits an **add to cache** shape, any existing data associated with that cache is overwritten with a new payload from the new run.

However, it is possible to **append** data to a cache, so each time the process flow runs and the **add to cache** shape is reached, the current cache is appended to the existing cache. This works for any cache type (**flow**, **flow run**, and **company**).

## Need to know

* **Paginated data**. If your connection shape receives paginated data, it's important to understand how the **save all pages** option works in conjunction with **append**. For more information please see our [cache pagination options page](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape/cache-pagination-options).
* **Cache size**. Theoretically, if a cache is set to append data and then runs on a regular basis indefinitely, the cache size may grow to an unmanageable size. With this in mind, a limit is in place to ensure that a single cache cannot exceed 50MB.
* **Append data format**. Appending cached data is supported for JSON only.
* **Shared caches.** The append to cache operation is not atomic - as such we advise against multiple process flows attempting to update the same cache at the same time.

## Using the append option

To use the **append** option, follow the steps below.

**Step 1**\
[Drop an add to cache shape into your process flow in the normal way](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape/..#adding-an-add-to-cache-shape-to-a-process-flow) - create your cache, then select it and add your cache key.

**Step 2**\
Ensure that the save all pages option is set as needed. For more information about how this option affects appended data please see our [cache pagination options page](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape/cache-pagination-options).

**Step 3**\
Enable the **append** option:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Ffcugb7v09glDmcZPoeEA%2Fappend%201.png?alt=media&#x26;token=c51c121e-fe26-4196-a83d-e74db299a3cd" alt="" width="351"><figcaption></figcaption></figure></div>

**Step 4**\
A **path to append to** field is displayed:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FhqYLs4v4RxnXzzj6iA36%2Fappend%202.png?alt=media&#x26;token=012920ee-fe4d-460a-a403-1091bb427237" alt="" width="351"><figcaption></figcaption></figure></div>

Here, you need to consider the structure of the payload that you're passing in and specify a path that ensures that each new payload is appended in the right place.

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FuKTXJEY34CVnak6PxrOz%2Fexample%20icon%202.svg?alt=media&#x26;token=7c8af2c5-9519-4757-bea9-172569a023bd" alt="" data-size="line"> Example</summary>

Consider the payload below:

{% code lineNumbers="true" %}

```json
{
  "orders": [
    {
      "id": 1,
      "customer": {
        "email": "joe.bloggs@wearepatchworks.com"
      }
    },
    {
      "id": 2,
      "customer": {
        "email": "jane.bloggs@wearepatchworks.com"
      }
    }
  ]
}
```

{% endcode %}

In this case, we want to append new data to the `orders` object, so our path to append to would be defined as `orders`. The first time the cache is updated, the payload would be:

{% code overflow="wrap" lineNumbers="true" %}

```json
[{"orders":[{"id":1,"customer":{"email":"joe.bloggs@wearepatchworks.com"}},{"id":2,"customer":{"email":"jane.bloggs@wearepatchworks.com"}}]}]
```

{% endcode %}

The next time the payload is appended, it would be in the form below:

{% code overflow="wrap" lineNumbers="true" %}

```json
{"0":{"orders":[{"id":1,"customer":{"email":"joe.bloggs@wearepatchworks.com"}},{"id":2,"customer":{"email":"jane.bloggs@wearepatchworks.com"}}]},"orders":[{"orders":[{"id":3,"customer":{"email":"dave.bowie@wearepatchworks.com"}},{"id":4,"customer":{"email":"michael.jagger@wearepatchworks.com"}}]}]}
```

{% endcode %}

</details>

{% hint style="info" %}
If required, [flow variables](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/dynamic-variables/flow-variables) can be specified here.
{% endhint %}

**Step 5**\
Save the shape. Next time the process flow runs the data will be cached and appended.

## Viewing the appended cache

If you choose to view the payload for an **add to cache** shape, the payload will always show data from the latest run - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F9cRkfAp4GCcAHAdt1Xzr%2Fview%20append%201.png?alt=media&#x26;token=b1330a8e-82ab-44ef-b1e3-b25597e81709" alt=""><figcaption></figcaption></figure></div>

However, when you add a load from cache shape, the payload will show ALL appended data so far - for example:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FGGOJqM8rv0kZ5ZHVYDrL%2Fview%20append%202.png?alt=media&#x26;token=d04ba00b-9865-45e6-a0ac-35afc320c316" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape/appending-data-to-a-cache.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
