Skip to main content

aurelin_framework/cache/
store.rs

1use std::time::Duration;
2
3use async_trait::async_trait;
4
5use super::Error;
6
7/// Driver contract used by Aurelin's typed cache API.
8#[async_trait]
9pub trait Store: Send + Sync + 'static {
10    /// Retrieve raw encoded data.
11    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error>;
12
13    /// Store raw encoded data with an optional lifetime.
14    async fn put(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>) -> Result<(), Error>;
15
16    /// Store data only when the key is absent.
17    async fn add(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>) -> Result<bool, Error>;
18
19    /// Remove and return raw encoded data atomically.
20    async fn pull(&self, key: &str) -> Result<Option<Vec<u8>>, Error>;
21
22    /// Atomically mutate an integer while retaining an existing lifetime.
23    async fn increment(&self, key: &str, amount: i64) -> Result<i64, Error>;
24
25    /// Replace the lifetime of an existing item.
26    async fn touch(&self, key: &str, ttl: Duration) -> Result<bool, Error>;
27
28    /// Remove one item, returning whether it existed.
29    async fn forget(&self, key: &str) -> Result<bool, Error>;
30
31    /// Remove all items from the selected backend, regardless of key prefix.
32    async fn flush(&self) -> Result<(), Error>;
33
34    /// Atomically acquire a lock for an owner.
35    async fn acquire_lock(&self, key: &str, owner: &str, ttl: Option<Duration>) -> Result<bool, Error>;
36
37    /// Atomically release a lock only when its owner matches.
38    async fn release_lock(&self, key: &str, owner: &str) -> Result<bool, Error>;
39
40    /// Release a lock without checking its owner.
41    async fn force_release_lock(&self, key: &str) -> Result<(), Error>;
42
43    /// Atomically replace the lifetime only when the owner matches.
44    async fn refresh_lock(&self, key: &str, owner: &str, ttl: Option<Duration>) -> Result<bool, Error>;
45
46    /// Return the current lock owner, if the lock exists.
47    async fn lock_owner(&self, key: &str) -> Result<Option<String>, Error>;
48}