actix_rl/store/
mod.rs

1#![allow(unused_imports)]
2
3pub mod mem_store;
4#[cfg(feature = "redis-store")]
5pub mod redis_store;
6
7use std::fmt::Debug;
8use std::ops::Deref;
9use std::sync::Arc;
10use chrono::{DateTime, Utc};
11
12/// [Store] indicates the location and method of caching,
13/// such as storing in memory ([MemStore]) in the form of a HashMap
14/// or in Redis in the form of key-value pairs.
15///
16/// All methods are implemented in an async manner.
17///
18/// [Store] can be [Clone], use [Arc] to store it.
19#[async_trait::async_trait]
20pub trait Store: Clone + Send + Sync {
21    /// [Error] represents possible errors that may
22    /// occur during the execution of a function.
23    type Error: Debug;
24
25    /// [Key] is used to denote the index type
26    /// in key-value pairs.
27    type Key: Send + Clone;
28
29    /// [Value] is a structure used to represent values,
30    /// such as the current count and the initial time.
31    type Value: Value;
32
33    /// Alias of [Value::Count].
34    type Count: Send + Clone;
35
36    /// The [incr_by] function takes a [Key] and
37    /// an unsigned integer, indicating the amount
38    /// by which the index should be incremented.
39    ///
40    /// The function returns the result of
41    /// the incremented count.
42    async fn incr_by(&self, key: Self::Key, val: Self::Count) -> Result<Self::Value, Self::Error>;
43
44    /// The [incr] function is a wrapper for the [incr_by] function,
45    /// with val = 1.
46    async fn incr(&self, key: Self::Key) -> Result<Self::Value, Self::Error>;
47
48    /// The [del] function deletes the storage of
49    /// the index [Key] and returns the count result
50    /// before deletion.
51    async fn del(&self, key: Self::Key) -> Result<Option<Self::Value>, Self::Error>;
52
53    /// The [clear] function clears all cached data.
54    ///
55    /// This function is not mandatory;
56    /// if [Store] does not need to clear cached data
57    /// (for example, when stored in Redis or a
58    /// relational database, bulk clearing of data
59    /// is slow and unnecessary), the function can do nothing.
60    async fn clear(&self) -> Result<(), Self::Error>;
61}
62
63pub trait Value: Send + Clone + Debug {
64    /// [Count] is the type of the counter, such as [u32].
65    type Count: Send + PartialOrd + Clone;
66
67    /// Return the count value from the counter.
68    fn count(&self) -> Self::Count;
69
70    /// Return the time of first creation.
71    fn create_date(&self) -> Option<DateTime<Utc>>;
72
73    /// Return the expiration time.
74    fn expire_date(&self) -> Option<DateTime<Utc>>;
75}
76
77#[async_trait::async_trait]
78impl<T: Store> Store for Arc<T> {
79    type Error = <T as Store>::Error;
80    type Key = <T as Store>::Key;
81    type Value = <T as Store>::Value;
82    type Count = <T as Store>::Count;
83
84    async fn incr_by(&self, key: Self::Key, val: Self::Count) -> Result<Self::Value, Self::Error> {
85        self.deref().incr_by(key, val).await
86    }
87
88    async fn incr(&self, key: Self::Key) -> Result<Self::Value, Self::Error> {
89        self.deref().incr(key).await
90    }
91
92    async fn del(&self, key: Self::Key) -> Result<Option<Self::Value>, Self::Error> {
93        self.deref().del(key).await
94    }
95
96    async fn clear(&self) -> Result<(), Self::Error> {
97        self.deref().clear().await
98    }
99}
100
101#[async_trait::async_trait]
102impl<'a, T: Store> Store for &'a T {
103    type Error = T::Error;
104    type Key = T::Key;
105    type Value = T::Value;
106    type Count = T::Count;
107
108    async fn incr_by(&self, key: Self::Key, val: Self::Count) -> Result<Self::Value, Self::Error> {
109        (*self).incr_by(key, val).await
110    }
111
112    async fn incr(&self, key: Self::Key) -> Result<Self::Value, Self::Error> {
113        (*self).incr(key).await
114    }
115
116    async fn del(&self, key: Self::Key) -> Result<Option<Self::Value>, Self::Error> {
117        (*self).del(key).await
118    }
119
120    async fn clear(&self) -> Result<(), Self::Error> {
121        (*self).clear().await
122    }
123}