StorageCacheExt

Trait StorageCacheExt 

Source
pub trait StorageCacheExt {
    // Required methods
    fn write_with_ttl<T: Serialize>(
        &self,
        key: &[u8],
        value: &T,
        ttl_secs: u64,
    ) -> Result<u64>;
    fn read_with_ttl<T: DeserializeOwned>(
        &self,
        key: &[u8],
    ) -> Result<Option<T>>;
}
Expand description

§Storage Utilities for Handling Auto-Evicting TTL Entries

Stores a timestamp (in seconds) before the actual value.

Note: Option types are safely handled by this without additional serialization as they are stored with the TTL value as well.

Required Methods§

Source

fn write_with_ttl<T: Serialize>( &self, key: &[u8], value: &T, ttl_secs: u64, ) -> Result<u64>

Writes a value with a TTL (Time-To-Live).

  • Stores the expiration timestamp as a binary prefix before the actual data.
  • If the key exists, it will be overwritten.
§Arguments
  • key: The binary key to store.
  • value: The value to be stored.
  • ttl_secs: The TTL in seconds (relative to current time).
§Returns
  • Ok(offset): The file offset where the data was written.
  • Err(std::io::Error): If the write operation fails.
Source

fn read_with_ttl<T: DeserializeOwned>(&self, key: &[u8]) -> Result<Option<T>>

Reads a value, checking TTL expiration.

  • ⚠️ Non Zero-Copy Warning: Requires deserialization.
  • If the TTL has expired, the key is automatically evicted, and None is returned.
  • If the key does not exist, returns Err(ErrorKind::NotFound).
  • If deserialization fails, returns Err(ErrorKind::InvalidData).
§Returns
  • Ok(Some(T)): If the TTL is still valid and the value is readable.
  • Ok(None): If the TTL has expired and the entry has been evicted.
  • Err(std::io::Error): If the key is missing or deserialization fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl StorageCacheExt for DataStore

Implements TTL-based caching for DataStore

Source§

fn write_with_ttl<T: Serialize>( &self, key: &[u8], value: &T, ttl_secs: u64, ) -> Result<u64>

Source§

fn read_with_ttl<T: DeserializeOwned>(&self, key: &[u8]) -> Result<Option<T>>

Implementors§