Skip to main content

authkestra_engine/store/
mod.rs

1use async_trait::async_trait;
2use std::time::Duration;
3
4#[derive(Debug, thiserror::Error)]
5pub enum StoreError {
6    #[error("Internal store error: {0}")]
7    Internal(String),
8    #[error("Not found")]
9    NotFound,
10    #[error("Serialization error: {0}")]
11    Serialization(String),
12}
13
14#[async_trait]
15pub trait KvStore<T>: Send + Sync + 'static {
16    async fn get(&self, key: &str) -> Result<Option<T>, StoreError>;
17    async fn set(&self, key: &str, value: T, ttl: Duration) -> Result<(), StoreError>;
18    async fn delete(&self, key: &str) -> Result<(), StoreError>;
19}
20
21/// Backends that can atomically fetch-and-remove a value implement this.
22#[async_trait]
23pub trait AtomicConsume<T>: KvStore<T> {
24    async fn consume(&self, key: &str) -> Result<Option<T>, StoreError>;
25}
26
27/// Backends that can atomically write a value under a primary key while
28/// also maintaining a secondary lookup key implement this.
29#[async_trait]
30pub trait IndexedKvStore<T>: KvStore<T> {
31    async fn set_indexed(
32        &self,
33        primary_key: &str,
34        secondary_key: &str,
35        value: T,
36        ttl: Duration,
37    ) -> Result<(), StoreError>;
38    async fn get_by_index(&self, secondary_key: &str) -> Result<Option<T>, StoreError>;
39}
40
41#[cfg(feature = "memory")]
42pub mod memory;
43
44#[cfg(feature = "redis")]
45pub mod redis;
46
47#[cfg(any(
48    feature = "sql-postgres",
49    feature = "sql-sqlite",
50    feature = "sql-mysql"
51))]
52pub mod sql;