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:
xphoniex
2023-07-25 13:10:58 +02:00
committed by Alexis Sellier
parent b1f174817c
commit 8a61aece01
8 changed files with 169 additions and 88 deletions
+12
View File
@@ -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))
}