actix_storage/provider/
expirystore.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use super::{Expiry, Store};
5use crate::error::Result;
6
7/// It is usefull for when store and expiry are implemented for the same struct,
8/// and should be implemented in those cases even if there can't be any optimization,
9/// as it will prevent some runtime checks for expiry validity.
10#[async_trait::async_trait]
11pub trait ExpiryStore: Store + Expiry + Send + Sync {
12    /// Set a key-value for a duration of time, if the key already exists, it should overwrite
13    /// both the value and the expiry for that key.
14    async fn set_expiring(
15        &self,
16        scope: Arc<[u8]>,
17        key: Arc<[u8]>,
18        value: Arc<[u8]>,
19        expire_in: Duration,
20    ) -> Result<()> {
21        self.set(scope.clone(), key.clone(), value).await?;
22        self.expire(scope, key, expire_in).await
23    }
24
25    /// Get the value and expiry for a key, it is possible to return None if the key doesn't exist,
26    /// or return None for the expiry if the key is persistent.
27    async fn get_expiring(
28        &self,
29        scope: Arc<[u8]>,
30        key: Arc<[u8]>,
31    ) -> Result<Option<(Arc<[u8]>, Option<Duration>)>> {
32        let val = self.get(scope.clone(), key.clone()).await?;
33        match val {
34            Some(val) => {
35                let expiry = self.expiry(scope, key).await?;
36                Ok(Some((val, expiry)))
37            }
38            None => Ok(None),
39        }
40    }
41}