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#[async_trait::async_trait]
20pub trait Store: Clone + Send + Sync {
21 type Error: Debug;
24
25 type Key: Send + Clone;
28
29 type Value: Value;
32
33 type Count: Send + Clone;
35
36 async fn incr_by(&self, key: Self::Key, val: Self::Count) -> Result<Self::Value, Self::Error>;
43
44 async fn incr(&self, key: Self::Key) -> Result<Self::Value, Self::Error>;
47
48 async fn del(&self, key: Self::Key) -> Result<Option<Self::Value>, Self::Error>;
52
53 async fn clear(&self) -> Result<(), Self::Error>;
61}
62
63pub trait Value: Send + Clone + Debug {
64 type Count: Send + PartialOrd + Clone;
66
67 fn count(&self) -> Self::Count;
69
70 fn create_date(&self) -> Option<DateTime<Utc>>;
72
73 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}