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

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