@@ -5,9 +5,9 @@ use lru::LruCache;
5
5
#[ cfg( feature = "ttl-cache" ) ]
6
6
use std:: collections:: VecDeque ;
7
7
#[ cfg( feature = "ttl-cache" ) ]
8
- use std:: time:: { SystemTime , Duration } ;
9
- #[ cfg( feature = "ttl-cache" ) ]
10
8
use std:: ops:: Add ;
9
+ #[ cfg( feature = "ttl-cache" ) ]
10
+ use tokio:: time:: { Instant , Duration } ;
11
11
12
12
pub trait CacheBacking < K , V >
13
13
where K : Eq + Hash + Sized + Clone + Send ,
88
88
#[ cfg( feature = "ttl-cache" ) ]
89
89
pub struct TtlCacheBacking < K , V > {
90
90
ttl : Duration ,
91
- expiry_queue : VecDeque < ( K , SystemTime ) > ,
92
- map : HashMap < K , ( V , SystemTime ) > ,
91
+ expiry_queue : VecDeque < ( K , Instant ) > ,
92
+ map : HashMap < K , ( V , Instant ) > ,
93
93
}
94
94
95
95
#[ cfg( feature = "ttl-cache" ) ]
@@ -111,7 +111,7 @@ impl<
111
111
112
112
fn set ( & mut self , key : K , value : V ) -> Option < V > {
113
113
self . remove_old ( ) ;
114
- let expiry = SystemTime :: now ( ) . add ( self . ttl ) ;
114
+ let expiry = Instant :: now ( ) . add ( self . ttl ) ;
115
115
let option = self . map . insert ( key. clone ( ) , ( value, expiry) ) ;
116
116
if option. is_some ( ) {
117
117
self . expiry_queue . retain ( |( vec_key, _) | vec_key. ne ( & key) ) ;
@@ -132,7 +132,7 @@ impl<
132
132
fn contains_key ( & self , key : & K ) -> bool {
133
133
// we cant clean old keys on this, since the self ref is not mutable :(
134
134
self . map . get ( key)
135
- . filter ( |( _, expiry) | SystemTime :: now ( ) . lt ( expiry) )
135
+ . filter ( |( _, expiry) | Instant :: now ( ) . lt ( expiry) )
136
136
. is_some ( )
137
137
}
138
138
@@ -165,7 +165,7 @@ impl<K: Hash + Sized + PartialEq + Eq, V> TtlCacheBacking<K, V> {
165
165
}
166
166
167
167
fn remove_old ( & mut self ) {
168
- let now = SystemTime :: now ( ) ;
168
+ let now = Instant :: now ( ) ;
169
169
while let Some ( ( key, expiry) ) = self . expiry_queue . pop_front ( ) {
170
170
if now. lt ( & expiry) {
171
171
self . expiry_queue . push_front ( ( key, expiry) ) ;
0 commit comments