> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/cache-object-store-events.md).

# Cache Object Store Events

These events occur when individual cache elements are manipulated inside a cache store. They are announced on the **cache provider's interceptor pool**, meaning listeners must be registered directly on a specific cache provider instance — not on the global interceptor pool.

> **Note:** `afterCacheElementInsert` and `afterCacheElementUpdated` are mutually exclusive — a single `set()` call fires only one of them depending on whether the key already existed.

| Event Name                  | Cancellable | Description                                                              |
| --------------------------- | :---------: | ------------------------------------------------------------------------ |
| `afterCacheElementInsert`   |      No     | Fired after a **new** cache key is inserted for the first time.          |
| `beforeCacheElementRemoved` |      No     | Fired before a cache element is removed, allowing pre-removal reactions. |
| `afterCacheElementRemoved`  |      No     | Fired after a cache element removal attempt completes.                   |
| `afterCacheElementUpdated`  |      No     | Fired after an **existing** cache key's value is replaced.               |

* [`afterCacheElementInsert`](#aftercacheelementinsert)
* [`beforeCacheElementRemoved`](#beforecacheelementremoved)
* [`afterCacheElementRemoved`](#aftercacheelementremoved)
* [`afterCacheElementUpdated`](#aftercacheelementupdated)

## afterCacheElementInsert

Fired after a brand-new key-value pair is stored in the cache (i.e., the key did not previously exist).

### Data Structure

| Data Key | Type             | Description                              |
| -------- | ---------------- | ---------------------------------------- |
| `cache`  | `ICacheProvider` | The cache provider the entry belongs to. |
| `key`    | `Key`            | The cache key that was inserted.         |
| `entry`  | `ICacheEntry`    | The newly created cache entry object.    |

### Example

```groovy
class myListener{
	function afterCacheElementInsert( struct data ){
		var cache = data.cache;
		var key   = data.key;
		var entry = data.entry;
		// React to the new cache entry
	}
}
```

## beforeCacheElementRemoved

Fired before a cache element is removed from the store. Use this for pre-removal reactions such as cleanup, logging, or dependent invalidations.

### Data Structure

| Data Key | Type             | Description                              |
| -------- | ---------------- | ---------------------------------------- |
| `cache`  | `ICacheProvider` | The cache provider the entry belongs to. |
| `key`    | `Key`            | The cache key about to be removed.       |
| `entry`  | `ICacheEntry`    | The cache entry about to be removed.     |

### Example

```groovy
class myListener{
	function beforeCacheElementRemoved( struct data ){
		var cache = data.cache;
		var key   = data.key;
		var entry = data.entry;
		// Perform pre-removal cleanup or logging
	}
}
```

## afterCacheElementRemoved

Fired after a cache element removal attempt completes, whether or not the key existed.

### Data Structure

| Data Key | Type             | Description                              |
| -------- | ---------------- | ---------------------------------------- |
| `cache`  | `ICacheProvider` | The cache provider the entry belongs to. |
| `key`    | `Key`            | The cache key that was removed.          |
| `entry`  | `ICacheEntry`    | The cache entry that was removed.        |

### Example

```groovy
class myListener{
	function afterCacheElementRemoved( struct data ){
		var cache = data.cache;
		var key   = data.key;
		var entry = data.entry;
		// React to the completed removal
	}
}
```

## afterCacheElementUpdated

Fired after an existing cache key's value has been replaced with a new value.

### Data Structure

| Data Key | Type             | Description                              |
| -------- | ---------------- | ---------------------------------------- |
| `cache`  | `ICacheProvider` | The cache provider the entry belongs to. |
| `key`    | `Key`            | The cache key that was updated.          |
| `entry`  | `ICacheEntry`    | The updated cache entry object.          |

### Example

```groovy
class myListener{
	function afterCacheElementUpdated( struct data ){
		var cache = data.cache;
		var key   = data.key;
		var entry = data.entry;
		// React to the updated cache entry
	}
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/cache-object-store-events.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
