pub trait Storage<K, V>: Send + Sync {
// Required methods
fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 K,
) -> Pin<Box<dyn Future<Output = AgentResult<Option<V>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn save<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 K,
value: &'life2 V,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait;
fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 K,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn list<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<K>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
Generic storage trait for key-value operations
This trait provides a simple interface for storing and retrieving arbitrary data by key. Implementations can use various backends such as in-memory maps, files, databases, etc.
§Type Parameters
K- Key type (must be able to be converted to/from String for serialization)V- Value type (must be serializable)
§Example
ⓘ
use mofa_kernel::storage::Storage;
struct MyStorage {
data: HashMap<String, Vec<u8>>,
}
#[async_trait]
impl Storage<String, Vec<u8>> for MyStorage {
async fn load(&self, key: &String) -> AgentResult<Option<Vec<u8>>> {
Ok(self.data.get(key).cloned())
}
async fn save(&self, key: &String, value: &Vec<u8>) -> AgentResult<()> {
self.data.insert(key.clone(), value.clone());
Ok(())
}
async fn delete(&self, key: &String) -> AgentResult<bool> {
Ok(self.data.remove(key).is_some())
}
async fn list(&self) -> AgentResult<Vec<String>> {
Ok(self.data.keys().cloned().collect())
}
}Required Methods§
Sourcefn load<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 K,
) -> Pin<Box<dyn Future<Output = AgentResult<Option<V>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 K,
) -> Pin<Box<dyn Future<Output = AgentResult<Option<V>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Load a value by key
Returns Ok(None) if the key doesn’t exist.
Sourcefn save<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 K,
value: &'life2 V,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn save<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 K,
value: &'life2 V,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Save a value by key
Creates a new entry or updates an existing one.
Sourcefn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 K,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 K,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Delete a value by key
Returns Ok(true) if the key existed and was deleted,
Ok(false) if the key didn’t exist.