---
schema_version: "1"
id: 53507e85131cf011cdd43bab9628651b133ed0bd4892c9e866bf8c3b57370cb5
title: Sharing Data Between Workers and Configuring Caching
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/pdk/1.9/policies-pdk-configure-features-caching.md
canonical_url: https://docs.mulesoft.com/pdk/1.9/policies-pdk-configure-features-caching
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 673d24910a0243aae73ab4584ef9e9282ff401ee56e82bbf8c688e6e1bbac36d
description: MuleSoft Documentation Site
release: "1.9"
latest: false
---
---
product: Omni Gateway Policy Development Kit (PDK)
version: 1.9
---

# Sharing Data Between Workers and Configuring Caching

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

> [!NOTE] To view an example policy project that uses caching, see
> [Data Caching Policy Example](https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.9.0/data-caching/README.md).

Envoy spawns many workers to increase the policy’s capability of handling requests. A single worker
handles all the stages of a specific request, including outgoing connections and the response flow.
This enables you to track internal status for a specific request, and to create a sequential
execution flow despite the underlying event-driven implementation.

Each worker processes a single request at a given time. To share a global state in a specific
worker, use a `RefCell` without any risk of concurrent modification.

Use the provided `Cache` mechanism to create a shared global state for all workers.

After injecting the cache builder, provide an ID for the cache and the maximum number of elements
the cache can support.

The following example configures a cache that can hold ten elements:

```Rust
#[entrypoint]
async fn configure(
    launcher: Launcher,
    cache_builder: CacheBuilder,
) -> Result<()> {
    let cache = cache_builder.new(String::from("caching")).max_entries(10);
    ...
```

The cache has the following interface:

```Rust
pub trait Cache {
    fn save(&self, key: &str, value: Vec<u8>) -> Result<(), CacheError>;
    fn get(&self, key: &str) -> Option<Vec<u8>>;
    fn delete(&self, key: &str) -> Option<Vec<u8>>;
    fn purge(&self);
}
```

> [!IMPORTANT] Because different workers write to the cache concurrently, there is no guarantee that
> the value overwritten when saving the cache is the same as when it was retrieved.

## See Also

- [Implementing Your Custom Policy Features in Rust](https://docs.mulesoft.com/pdk/1.9/policies-pdk-configure-features)
