1
+ //! A simple cache system for heimdall-rs
2
+ //! Stores objects in ~/.bifrost/cache as bincode serialized files
3
+ //! Objects are stored with an expiry time, and are deleted if they are expired
4
+
1
5
use clap:: Parser ;
2
6
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
3
7
#[ allow( deprecated) ]
@@ -17,6 +21,7 @@ pub(crate) mod util;
17
21
override_usage = "heimdall cache <SUBCOMMAND>"
18
22
) ]
19
23
pub struct CacheArgs {
24
+ /// Cache subcommand
20
25
#[ clap( subcommand) ]
21
26
pub sub : Subcommands ,
22
27
}
@@ -33,12 +38,15 @@ pub struct NoArguments {}
33
38
) ]
34
39
#[ allow( clippy:: large_enum_variant) ]
35
40
pub enum Subcommands {
41
+ /// Clear the cache, removing all objects
36
42
#[ clap( name = "clean" , about = "Removes all cached objects in ~/.bifrost/cache" ) ]
37
43
Clean ( NoArguments ) ,
38
44
45
+ /// List all cached objects
39
46
#[ clap( name = "ls" , about = "Lists all cached objects in ~/.bifrost/cache" ) ]
40
47
Ls ( NoArguments ) ,
41
48
49
+ /// Print the size of the cache in ~/.bifrost/cache
42
50
#[ clap( name = "size" , about = "Prints the size of the cache in ~/.bifrost/cache" ) ]
43
51
Size ( NoArguments ) ,
44
52
}
@@ -47,7 +55,9 @@ pub enum Subcommands {
47
55
/// The expiry time is a unix timestamp
48
56
#[ derive( Debug , Clone , Deserialize , Serialize ) ]
49
57
pub struct Cache < T > {
58
+ /// The value stored in the cache
50
59
pub value : T ,
60
+ /// The expiry time of the cache object
51
61
pub expiry : u64 ,
52
62
}
53
63
@@ -215,7 +225,8 @@ pub fn delete_cache(key: &str) -> Result<(), Error> {
215
225
#[ allow( deprecated) ]
216
226
pub fn read_cache < T > ( key : & str ) -> Result < Option < T > , Error >
217
227
where
218
- T : ' static + DeserializeOwned , {
228
+ T : ' static + DeserializeOwned ,
229
+ {
219
230
let home = home_dir ( ) . ok_or ( Error :: Generic (
220
231
"failed to get home directory. does your os support `std::env::home_dir()`?" . to_string ( ) ,
221
232
) ) ?;
@@ -238,8 +249,8 @@ where
238
249
. map_err ( |e| Error :: Generic ( format ! ( "failed to deserialize cache object: {:?}" , e) ) ) ?;
239
250
240
251
// check if the cache has expired, if so, delete it and return None
241
- if cache. expiry <
242
- std:: time:: SystemTime :: now ( )
252
+ if cache. expiry
253
+ < std:: time:: SystemTime :: now ( )
243
254
. duration_since ( std:: time:: UNIX_EPOCH )
244
255
. map_err ( |e| Error :: Generic ( format ! ( "failed to get current time: {:?}" , e) ) ) ?
245
256
. as_secs ( )
@@ -266,7 +277,8 @@ where
266
277
#[ allow( deprecated) ]
267
278
pub fn store_cache < T > ( key : & str , value : T , expiry : Option < u64 > ) -> Result < ( ) , Error >
268
279
where
269
- T : Serialize , {
280
+ T : Serialize ,
281
+ {
270
282
let home = home_dir ( ) . ok_or ( Error :: Generic (
271
283
"failed to get home directory. does your os support `std::env::home_dir()`?" . to_string ( ) ,
272
284
) ) ?;
@@ -278,8 +290,8 @@ where
278
290
std:: time:: SystemTime :: now ( )
279
291
. duration_since ( std:: time:: UNIX_EPOCH )
280
292
. map_err ( |e| Error :: Generic ( format ! ( "failed to get current time: {:?}" , e) ) ) ?
281
- . as_secs ( ) +
282
- 60 * 60 * 24 * 90 ,
293
+ . as_secs ( )
294
+ + 60 * 60 * 24 * 90 ,
283
295
) ;
284
296
285
297
let cache = Cache { value, expiry } ;
@@ -304,7 +316,8 @@ pub async fn with_cache<T, F, Fut>(key: &str, func: F) -> eyre::Result<T>
304
316
where
305
317
T : ' static + Serialize + DeserializeOwned + Send + Sync ,
306
318
F : FnOnce ( ) -> Fut + Send ,
307
- Fut : std:: future:: Future < Output = Result < T , eyre:: Report > > + Send , {
319
+ Fut : std:: future:: Future < Output = Result < T , eyre:: Report > > + Send ,
320
+ {
308
321
// Try to read from cache
309
322
match read_cache :: < T > ( key) {
310
323
Ok ( Some ( cached_value) ) => {
0 commit comments