pdk-cache-lib 1.3.0-alpha.0

PDK Cache Library
Documentation
// Copyright 2023 Salesforce, Inc. All rights reserved.
pub mod builder;
pub mod error;
pub mod fifo_cache;

#[cfg(test)]
mod mocks;

use self::error::CacheError;

/// Represents a cache. It stores each element as a new entry.
pub trait Cache {
    /// Stores value in cache with provided key. Value should be already serialized.
    ///
    /// If it already exists, stored value will be overridden with the new param value.
    /// Have in consideration that this might affect the Cache order, depending on the
    /// eviction strategy (i.e. LIFO, FIFO or LRU caches). Currently, only FIFO cache is offered.
    ///
    /// If it is not possible to store the value in cache, it results in a [`CacheError`].
    fn save(&self, key: &str, value: Vec<u8>) -> Result<(), CacheError>;

    /// Returns stored value in cache for provided key.
    ///
    /// If it exists in the cache, returns [`Some`] with bytes slice value.
    /// Otherwise, it returns [`None`].
    fn get(&self, key: &str) -> Option<Vec<u8>>;

    /// Removes an element from cache by key.
    ///
    /// Returns the deleted value if present, or [`None`] otherwise.
    fn delete(&self, key: &str) -> Option<Vec<u8>>;

    /// Clears all elements in cache.
    fn purge(&self);
}