pdk_cache_lib/lib.rs
1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5//! Cache Library
6//!
7//! A caching library that provides a simple [`Cache`] trait and a FIFO-based
8//! in-memory implementation. It is designed to be injected and configured during the policy
9//! configuration phase and supports both policy-isolated and shared caches across policies.
10//!
11//! This library provides data storage functionality with support for:
12//!
13//! - FIFO eviction strategy
14//! - Builder pattern via [`builder::CacheBuilder`] and [`builder::CacheBuilderInstance`]
15//! - Policy-isolated caches by default; opt-in shared caches via [`builder::CacheBuilderInstance::shared()`]
16//! - Configurable maximum entries per cache
17//!
18
19pub mod builder;
20pub mod error;
21
22// The user is agnostic to the implementation of the cache since the builder returns a trait object.
23// this crate should have been pub(crate), too late to change it now, we just hide it from the doc.
24#[doc(hidden)]
25pub mod fifo_cache;
26
27#[cfg(test)]
28mod mocks;
29
30use self::error::CacheError;
31
32/// Represents a cache. It stores each element as a new entry.
33pub trait Cache {
34 /// Stores value in cache with provided key. Value should be already serialized.
35 ///
36 /// If it already exists, stored value will be overridden with the new param value.
37 /// Have in consideration that this might affect the Cache order, depending on the
38 /// eviction strategy (i.e. LIFO, FIFO or LRU caches). Currently, only FIFO cache is offered.
39 ///
40 /// If it is not possible to store the value in cache, it results in a [`CacheError`].
41 fn save(&self, key: &str, value: Vec<u8>) -> Result<(), CacheError>;
42
43 /// Returns stored value in cache for provided key.
44 ///
45 /// If it exists in the cache, returns [`Some`] with bytes slice value.
46 /// Otherwise, it returns [`None`].
47 fn get(&self, key: &str) -> Option<Vec<u8>>;
48
49 /// Removes an element from cache by key.
50 ///
51 /// Returns the deleted value if present, or [`None`] otherwise.
52 fn delete(&self, key: &str) -> Option<Vec<u8>>;
53
54 /// Clears all elements in cache.
55 fn purge(&self);
56}