mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-28 21:50:26 +08:00
httpd: Add a cache for /tree
This improves response time of: - GET /api/v1/projects/<RID>/tree/<OID>/ from 205.71ms down to 376.4µs - GET /api/v1/projects/<RID>/tree/<OID>/.nix from 111.06ms down to 351.70µs Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
@@ -309,12 +309,24 @@ async fn tree_handler(
|
||||
State(ctx): State<Context>,
|
||||
Path((project, sha, path)): Path<(Id, Oid, String)>,
|
||||
) -> impl IntoResponse {
|
||||
if let Some(ref cache) = ctx.cache {
|
||||
let cache = &mut cache.tree.lock().await;
|
||||
if let Some(response) = cache.get(&(project, sha, path.clone())) {
|
||||
return Ok::<_, Error>(Json(response.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
let storage = &ctx.profile.storage;
|
||||
let repo = Repository::open(paths::repository(storage, &project))?;
|
||||
let tree = repo.tree(sha, &path)?;
|
||||
let stats = repo.stats_from(&sha)?;
|
||||
let response = api::json::tree(&tree, &path, &stats);
|
||||
|
||||
if let Some(cache) = ctx.cache {
|
||||
let cache = &mut cache.tree.lock().await;
|
||||
cache.put((project, sha, path.clone()), response.clone());
|
||||
}
|
||||
|
||||
Ok::<_, Error>(Json(response))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user