---
schema_version: "1"
id: a216980f55371bbb449e51eed4ba8dcaf1eb316b622e2b0585d8508487df14f8
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.7/policies-pdk-configure-features-caching.md
canonical_url: https://docs.mulesoft.com/pdk/1.7/policies-pdk-configure-features-caching
source_site: docs.mulesoft.com
content_type: documentation
content_hash: a55d83ddd5b382552cc35823bd3b10e957496e64f7941882a25777048144e4d8
description: MuleSoft Documentation Site
release: "1.7"
latest: false
---
---
product: Omni Gateway Policy Development Kit (PDK)
version: 1.7
---

# 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.6.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.7/policies-pdk-configure-features)
