Skip to main content

pdk_cache_lib/
lib.rs

1// Copyright 2023 Salesforce, Inc. All rights reserved.
2pub mod builder;
3pub mod error;
4pub mod fifo_cache;
5
6#[cfg(test)]
7mod mocks;
8
9use self::error::CacheError;
10
11/// Represents a cache. It stores each element as a new entry.
12pub trait Cache {
13    /// Stores value in cache with provided key. Value should be already serialized.
14    ///
15    /// If it already exists, stored value will be overridden with the new param value.
16    /// Have in consideration that this might affect the Cache order, depending on the
17    /// eviction strategy (i.e. LIFO, FIFO or LRU caches). Currently, only FIFO cache is offered.
18    ///
19    /// If it is not possible to store the value in cache, it results in a [`CacheError`].
20    fn save(&self, key: &str, value: Vec<u8>) -> Result<(), CacheError>;
21
22    /// Returns stored value in cache for provided key.
23    ///
24    /// If it exists in the cache, returns [`Some`] with bytes slice value.
25    /// Otherwise, it returns [`None`].
26    fn get(&self, key: &str) -> Option<Vec<u8>>;
27
28    /// Removes an element from cache by key.
29    ///
30    /// Returns the deleted value if present, or [`None`] otherwise.
31    fn delete(&self, key: &str) -> Option<Vec<u8>>;
32
33    /// Clears all elements in cache.
34    fn purge(&self);
35}