1
+ use super :: CacheStore ;
1
2
use std:: ops:: Add ;
2
3
use std:: time:: { Duration , Instant } ;
3
4
5
+ use async_trait:: async_trait;
4
6
use moka:: future:: Cache ;
5
7
6
8
use crate :: protocol:: Message ;
7
9
8
10
#[ derive( Clone ) ]
9
- pub ( crate ) struct CacheStore {
11
+ pub ( crate ) struct InMemoryCache {
10
12
ttl : Duration ,
11
13
cache : Cache < Message , ( Instant , Message ) > ,
12
14
}
13
15
14
- impl CacheStore {
16
+ impl InMemoryCache {
15
17
pub ( crate ) fn builder ( ) -> CacheStoreBuilder {
16
18
CacheStoreBuilder {
17
19
ttl : Duration :: from_secs ( 3600 ) ,
18
20
capacity : 1000 ,
19
21
}
20
22
}
23
+ }
21
24
22
- pub ( crate ) async fn get ( & self , req : & Message ) -> Option < ( Instant , Message ) > {
25
+ #[ async_trait]
26
+ impl CacheStore for InMemoryCache {
27
+ async fn get ( & self , req : & Message ) -> Option < ( Instant , Message ) > {
23
28
self . cache . get ( req) . await
24
29
}
25
30
26
- pub ( crate ) async fn set ( & self , req : & Message , resp : & Message ) {
31
+ async fn set ( & self , req : & Message , resp : & Message ) {
27
32
let key = Clone :: clone ( req) ;
28
33
let val = Clone :: clone ( resp) ;
29
34
@@ -54,19 +59,20 @@ impl CacheStoreBuilder {
54
59
self
55
60
}
56
61
57
- pub ( crate ) fn build ( self ) -> CacheStore {
62
+ pub ( crate ) fn build ( self ) -> InMemoryCache {
58
63
let Self { ttl, capacity } = self ;
59
64
let cache = Cache :: builder ( ) . max_capacity ( capacity as u64 ) . build ( ) ;
60
65
61
- CacheStore { ttl, cache }
66
+ InMemoryCache { ttl, cache }
62
67
}
63
68
}
64
69
65
70
#[ cfg( test) ]
66
71
mod tests {
67
- use super :: * ;
68
72
use tokio:: time:: { sleep, Duration } ;
69
73
74
+ use super :: * ;
75
+
70
76
fn init ( ) {
71
77
pretty_env_logger:: try_init_timed ( ) . ok ( ) ;
72
78
}
@@ -88,7 +94,7 @@ mod tests {
88
94
Message :: from ( raw)
89
95
} ;
90
96
91
- let cs = CacheStore :: builder ( ) . ttl ( 2 ) . capacity ( 100 ) . build ( ) ;
97
+ let cs = InMemoryCache :: builder ( ) . ttl ( 2 ) . capacity ( 100 ) . build ( ) ;
92
98
assert ! ( cs. get( & req) . await . is_none( ) ) ;
93
99
94
100
cs. set ( & req, & res) . await ;
0 commit comments