pdk-cache-lib 0.0.0-alpha.3

PDK
Documentation
# PDK Cache Library   [![Latest Version]][crates.io] [![cache--lib msrv]][Rust 1.87]

<!-- Update links on release. -->
[Latest Version]: https://img.shields.io/badge/crates.io-v1.6.0-blue
[crates.io]: https://crates.io/crates/pdk-cache-lib
[cache--lib msrv]: https://img.shields.io/badge/pdk--cache--lib_msrv-1.87-lightgray
[Rust 1.87]: https://blog.rust-lang.org/2025/05/15/Rust-1.87.0

**pdk-cache-lib is a library that provides data caching functionality for building Flex Gateway custom policies.**

---

You may be looking for:

- [An overview of PDK]https://docs.mulesoft.com/pdk/latest/policies-pdk-overview
- [Caching Library]https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-caching
- [Injecting Parameters]https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-inject-parameters
- [Examples]https://github.com/mulesoft/pdk-custom-policy-examples/tree/main/data-caching
- [Release notes]https://docs.mulesoft.com/release-notes/pdk/pdk-release-notes

## cache-lib in action

```toml
[dependencies]

# This package is included as a transitive dependency of the pdk crate.
pdk = { version = "1.6"}
```

```rust
use anyhow::Result;
use pdk::cache::{Cache, CacheBuilder, CacheError};
use pdk::hl::*;

const MAX_CACHE_ENTRIES: usize = 100;

async fn request_filter(request_state: RequestState, cache:&dyn Cache) {
    let _headers_state = request_state.into_headers_state().await;

    // Save a value to the cache
    let _save_result: Result<(), CacheError> = cache.save("example-key", "example-value".as_bytes().to_vec());

    // Retrieve value from the cache.
    let _retrieved_value: Option<Vec<u8>> = cache.get("example-key");

    // Retrieve value from the cache.
    let _deleted_value: Option<Vec<u8>> = cache.delete("example-key");

    // remove all keys from the cache.
    cache.purge();

}

async fn response_filter(response_state: ResponseState, cache:&dyn Cache) {
    let _headers_state = response_state.into_headers_state().await;

    // Save a value to the cache
    let _save_result: Result<(), CacheError> = cache.save("example-key", "example-value".as_bytes().to_vec());

    // Retrieve value from the cache.
    let _retrieved_value: Option<Vec<u8>> = cache.get("example-key");

    // Retrieve value from the cache.
    let _deleted_value: Option<Vec<u8>> = cache.delete("example-key");

    // remove all keys from the cache.
    cache.purge();

}

#[entrypoint]
// Inject the CacheBuilder to the 'configure' function.
async fn configure(launcher: Launcher, cache_builder: CacheBuilder) -> Result<()> {

    let cache = cache_builder
        .new("example-id".to_string())
        .max_entries(MAX_CACHE_ENTRIES)
        // .shared() // if uncommented the cache won't be isolated to the current policy.
        .build();

    let filter = on_request(|rs| request_filter(rs, &cache))
        .on_response(|rs| response_filter(rs, &cache));
    launcher.launch(filter).await?;
    Ok(())
}
```

#### License
<sup>
Licensed under <a href="../LICENSE.txt">Salesforce Inc License</a>.
</sup>