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§
Sourcefn push_data_ctx(
&self,
key: &str,
value: Value,
ttl: Option<Duration>,
) -> Result<(), DataError>
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.
Sourcefn get_data_ctx(&self, key: &str) -> Result<Option<Value>, DataError>
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.
Sourcefn get_data_entry_ctx(&self, key: &str) -> Result<Option<DataEntry>, DataError>
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.
Sourcefn remove_data_ctx(&self, key: &str) -> Result<bool, DataError>
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.
Sourcefn clear_data_ctx(&self) -> Result<(), DataError>
fn clear_data_ctx(&self) -> Result<(), DataError>
Clear all entries from the store.
Sourcefn list_data_ctx(&self) -> Result<Vec<DataEntry>, DataError>
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.
Sourcefn get_stats_ctx(&self) -> Result<DataStoreStats, DataError>
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".