Skip to main content

DataApi

Trait DataApi 

Source
pub trait DataApi {
    // Required methods
    fn push_data_ctx(
        &self,
        key: &str,
        value: Value,
        ttl: Option<Duration>,
    ) -> Result<(), DataError>;
    fn get_data_ctx(&self, key: &str) -> Result<Option<Value>, DataError>;
    fn get_data_entry_ctx(
        &self,
        key: &str,
    ) -> Result<Option<DataEntry>, DataError>;
    fn remove_data_ctx(&self, key: &str) -> Result<bool, DataError>;
    fn clear_data_ctx(&self) -> Result<(), DataError>;
    fn list_data_ctx(&self) -> Result<Vec<DataEntry>, DataError>;
    fn get_stats_ctx(&self) -> Result<DataStoreStats, DataError>;
}
Expand description

Trait defining the public API for data store operations.

This trait provides a consistent interface for pushing, retrieving, and managing data in the store. All operations are thread-safe.

The Cedarling struct implements this trait, providing access to the data store through the main application instance.

§Example

use cedarling::{Cedarling, DataApi, DataError};
use serde_json::json;
use std::time::Duration;

fn use_data_api(cedarling: &Cedarling) -> Result<(), DataError> {
    // Push data with a 5-minute TTL
    cedarling.push_data_ctx(
        "user_roles",
        json!(["admin", "editor"]),
        Some(Duration::from_secs(300)),
    )?;

    // Retrieve data
    if let Some(roles) = cedarling.get_data_ctx("user_roles")? {
        println!("User roles: {}", roles);
    }

    // List all entries with metadata
    for entry in cedarling.list_data_ctx()? {
        println!("Key: {}, Type: {:?}", entry.key, entry.data_type);
    }

    // Get store statistics
    let stats = cedarling.get_stats_ctx()?;
    println!("Entries: {}/{}", stats.entry_count, stats.max_entries);

    // Remove data
    cedarling.remove_data_ctx("user_roles")?;

    // Clear all data
    cedarling.clear_data_ctx()?;

    Ok(())
}

Required Methods§

Source

fn push_data_ctx( &self, key: &str, value: Value, ttl: Option<Duration>, ) -> Result<(), DataError>

Push a value into the store with an optional TTL.

If the key already exists, the value will be replaced. If TTL is not provided, the default TTL from configuration is used.

Source

fn get_data_ctx(&self, key: &str) -> Result<Option<Value>, DataError>

Get a value from the store by key.

Returns Ok(None) if the key doesn’t exist or the entry has expired. If metrics are enabled, increments the access count for the entry.

Source

fn get_data_entry_ctx(&self, key: &str) -> Result<Option<DataEntry>, DataError>

Get a data entry with full metadata by key.

Returns Ok(None) if the key doesn’t exist or the entry has expired. Includes metadata like creation time, expiration, access count, and type.

Source

fn remove_data_ctx(&self, key: &str) -> Result<bool, DataError>

Remove a value from the store by key.

Returns Ok(true) if the key existed and was removed, Ok(false) otherwise.

Source

fn clear_data_ctx(&self) -> Result<(), DataError>

Clear all entries from the store.

Source

fn list_data_ctx(&self) -> Result<Vec<DataEntry>, DataError>

List all entries with their metadata.

Returns a vector of DataEntry containing key, value, type, and timing metadata.

Source

fn get_stats_ctx(&self) -> Result<DataStoreStats, DataError>

Get statistics about the data store.

Returns current entry count, capacity limits, and configuration state.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§