# Caffeine Cache

**Caffeine Cache** uses the [Caffeine library](https://github.com/ben-manes/caffeine) to store data in memory for quick access. It helps reduce database queries and speeds up applications by automatically managing stored data with smart expiration and eviction rules. Ideal for caching API responses, database results, and frequently used data.

{% hint style="warning" %}
This connector uses pipeline memory. If not managed efficiently, it may lead to [“Out of Memory” errors](https://docs.digibee.com/documentation/connectors-and-triggers/connectors/structured-data/broken-reference).
{% endhint %}

## **Parameters**

Take a look at the configuration parameters for the connector. Parameters supported by [Double Braces expressions](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/overview) are marked with `(DB)`.

### **General tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Cache Name</strong> <code>(DB)</code></td><td>Defines the name of the cache.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Action</strong></td><td>Specifies the default cache action. Options: <code>Get</code>, <code>Put</code>.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Key</strong> <code>(DB)</code></td><td>Defines the key used to identify the cache data. Used in <code>Get</code> and <code>Put</code> actions.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Raw Mode</strong></td><td>When enabled, the <strong>Value</strong> parameter will be replaced by <strong>Body</strong>. Used in <code>Put</code> actions</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Body</strong></td><td>Defined the data associated with the specified key. This field supports multiple values and is used in <code>Put</code> actions.</td><td><code>{ }</code></td><td>JSON</td></tr><tr><td><strong>Value</strong></td><td>Defines the data associated with the specified key. Allows only one value and is used in <code>Put</code> actions when <strong>Raw Mode</strong> is disabled.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Eviction Type</strong></td><td>Determines how the cache evicts entries. Options: <code>size_based</code>, <code>time_based</code>.</td><td><code>size_based</code></td><td>String</td></tr><tr><td><strong>Initial Capacity (when <code>size_based</code> is selected)</strong></td><td>Sets the initial size for internal data structures. This doesn’t limit the cache's maximum size but helps optimize performance. If not specified, the default initial capacity is set to <code>16</code>. When provided, the specified value will replace the default.</td><td><code>0</code></td><td>Integer</td></tr><tr><td><strong>Maximum Size (when <code>size_based</code> is selected)</strong></td><td>Defines the maximum number of entries the cache can hold, measured by quantity rather than bytes. The cache may start evicting entries before reaching this limit or briefly exceed it during eviction. Setting this value to <code>0</code> disables caching, causing entries to be removed immediately after being loaded.</td><td><code>0</code></td><td>Integer</td></tr><tr><td><strong>Expire After Access Time (s) (when <code>time_based</code> is selected)</strong></td><td>Defines the expiration time after the data is accessed, in seconds.</td><td><code>300</code></td><td>Integer</td></tr><tr><td><strong>Expire After Write Time (s) (when <code>time_based</code> is selected)</strong></td><td>Defines the expiration time after the data is written, in seconds.</td><td><code>300</code></td><td>Integer</td></tr><tr><td><strong>Fail On Error</strong></td><td>If the option is activated, the pipeline's execution with an error will be interrupted. Otherwise, the pipeline execution proceeds, but the result will show a false value for the <code>"success"</code> property.</td><td>False</td><td>Boolean</td></tr></tbody></table>

### **Optional tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Create Cache If Not Exist</strong></td><td>Automatically creates a new Caffeine cache if none is configured or found in the registry. When disabled, trying to access a non-existent cache will result in an error response.</td><td>True</td><td>Boolean</td></tr><tr><td><strong>Do not display the result</strong></td><td>When enabled, it doesn’t indicate if the action had a result. This is more useful for <code>Get</code> operations.</td><td>True</td><td>Boolean</td></tr><tr><td><strong>Not showing success</strong></td><td>When enabled, it doesn’t indicate if the action succeeded. This is more useful for <code>Put</code> operations.</td><td>True</td><td>Boolean</td></tr></tbody></table>

### **Documentation tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Documentation</strong></td><td>Section for documenting any necessary information about the connector configuration and business rules.</td><td>N/A</td><td>String</td></tr></tbody></table>

## **Usage example**

In this example, we use two Caffeine Cache connectors in the flow: one at the beginning with the `Put` action and another at end of the flow with the `Get` action.

The first Caffeine Cache stores data in the cache for later use. Here’s how it is configured:

* **Cache Name:** `digibee`
* **Action:** `Put`
* **Key:** `token`
* **Raw Mode:** `Disabled`
* **Value:** `digibee_cache`
* **Eviction Type:** `size_based`
* **Initial Capacity:** `10`
* **Maximum Size:** `999`

When we execute only this connector, the following output confirms that the data has been successfully stored in the cache:

```json
{
  "success": true
}
```

At the end of the flow, we retrieve the stored data using another Caffeine Cache connector, now configured as follows:

* **Cache Name:** `digibee`
* **Action:** `Get`
* **Key:** `token`

When we execute the entire flow, we receive the following output, confirming the retrieval of the cached data:

```json
{
  "success": true,
  "body": {
    "result": "digibee_cache"
  }
}
```
