actix_storage/provider/
expiry.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::Result;
5
6/// Set of method for expiry providers to implement.
7///
8/// The expiry itself should guarantee that it's working on the correct storage
9/// and provide a hint that it's persistant or temprorary.
10#[async_trait::async_trait]
11pub trait Expiry: Send + Sync {
12    /// Remove all expiry requests from a key and make it persistent,
13    /// the persistenty can be overwriten by calling expire on the key.
14    async fn persist(&self, scope: Arc<[u8]>, key: Arc<[u8]>) -> Result<()>;
15
16    /// Sets an expiry for a key, the key may or may not be removed based on
17    /// implementation, but it should be guaranteed that it won't appear in
18    /// get based methods or contains checks after the period specified.
19    async fn expire(&self, scope: Arc<[u8]>, key: Arc<[u8]>, expire_in: Duration) -> Result<()>;
20
21    /// Gets expiry for a key, returning None means it doesn't have an expiry,
22    /// if the provider can't return an expiry, it should return an error instead.
23    /// The result of this function can have some error, but it should be documented.
24    async fn expiry(&self, scope: Arc<[u8]>, key: Arc<[u8]>) -> Result<Option<Duration>>;
25
26    /// Extend expiry for a key for another duration of time.
27    /// If the key doesn't have an expiry, it should be equivalent of calling expire.
28    async fn extend(&self, scope: Arc<[u8]>, key: Arc<[u8]>, expire_in: Duration) -> Result<()> {
29        let expiry = self.expiry(scope.clone(), key.clone()).await?;
30        self.expire(scope, key, expiry.unwrap_or_default() + expire_in)
31            .await
32    }
33
34    #[allow(unused_variables)]
35    /// A notification that should be implemented if expiry is a different entity that
36    /// store itself to remove expiry when set is called for a key.
37    async fn set_called(&self, key: Arc<[u8]>) {}
38}