forked from messense/aliyundrive-webdav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.rs
50 lines (41 loc) · 1.29 KB
/
cache.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::path::Path;
use std::time::Duration;
use moka::future::Cache as MokaCache;
use tracing::trace;
use crate::drive::AliyunFile;
#[derive(Clone)]
pub struct Cache {
inner: MokaCache<String, Vec<AliyunFile>>,
}
impl Cache {
pub fn new(max_capacity: u64, ttl: u64) -> Self {
let inner = MokaCache::builder()
.max_capacity(max_capacity)
.time_to_live(Duration::from_secs(ttl))
.build();
Self { inner }
}
#[allow(clippy::ptr_arg)]
pub fn get(&self, key: &String) -> Option<Vec<AliyunFile>> {
trace!(key = %key, "cache: get");
self.inner.get(key)
}
pub async fn insert(&self, key: String, value: Vec<AliyunFile>) {
trace!(key = %key, "cache: insert");
self.inner.insert(key, value).await;
}
pub async fn invalidate(&self, path: &Path) {
let key = path.to_string_lossy().into_owned();
trace!(path = %path.display(), key = %key, "cache: invalidate");
self.inner.invalidate(&key).await;
}
pub async fn invalidate_parent(&self, path: &Path) {
if let Some(parent) = path.parent() {
self.invalidate(parent).await;
}
}
pub fn invalidate_all(&self) {
trace!("cache: invalidate all");
self.inner.invalidate_all();
}
}