forked from web3infra-foundation/mega
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.rs
More file actions
74 lines (61 loc) · 2.15 KB
/
cache.rs
File metadata and controls
74 lines (61 loc) · 2.15 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::sync::Arc;
use common::errors::MegaError;
use git_internal::{
hash::ObjectHash,
internal::object::{commit::Commit, tree::Tree},
};
use redis::{AsyncCommands, aio::ConnectionManager};
#[derive(Clone)]
pub struct GitObjectCache {
pub connection: ConnectionManager,
pub prefix: String,
}
const DEFAULT_EXPIRY_SECONDS: u64 = 60 * 60 * 24 * 7; // 7 days
impl GitObjectCache {
pub async fn get_tree<F, Fut>(
&self,
oid: ObjectHash,
fetch_tree: F,
) -> Result<Arc<Tree>, MegaError>
where
F: Fn(ObjectHash) -> Fut,
Fut: Future<Output = Result<Tree, MegaError>>,
{
let key = format!("{}:tree:{}", self.prefix, oid);
let mut conn = self.connection.clone();
if let Ok(data) = conn.get::<_, Vec<u8>>(&key).await
&& !data.is_empty()
&& let Ok((tree, _)) = bincode::decode_from_slice(&data, bincode::config::standard())
{
return Ok(Arc::new(tree));
}
let tree_raw = fetch_tree(oid).await?;
let tree = Arc::new(tree_raw);
let serialized = bincode::encode_to_vec(tree.as_ref(), bincode::config::standard())?;
let _: () = conn.set_ex(key, serialized, DEFAULT_EXPIRY_SECONDS).await?;
Ok(tree)
}
pub async fn get_commit<F, Fut>(
&self,
oid: ObjectHash,
fetch_commit: F,
) -> Result<Arc<Commit>, MegaError>
where
F: Fn(ObjectHash) -> Fut,
Fut: Future<Output = Result<Commit, MegaError>>,
{
let mut conn = self.connection.clone();
let key = format!("{}:commit:{}", self.prefix, oid);
if let Ok(data) = conn.get::<_, Vec<u8>>(&key).await
&& !data.is_empty()
&& let Ok((commit, _)) = bincode::decode_from_slice(&data, bincode::config::standard())
{
return Ok(Arc::new(commit));
}
let commit_raw = fetch_commit(oid).await?;
let commit = Arc::new(commit_raw);
let serialized = bincode::encode_to_vec(commit.as_ref(), bincode::config::standard())?;
let _: () = conn.set_ex(key, serialized, DEFAULT_EXPIRY_SECONDS).await?;
Ok(commit)
}
}