refactor(cache): redesign cache system with structured keys and improved performance
- Add repo_path parameter to cached_response and cached_vec_response functions - Implement structured cache key format with namespace, repo_path, and request proto - Replace global cache with Moka in-memory cache using weight-based eviction - Set 256MB memory cap with 10-minute TTL and 2-minute TTI policy - Add metrics collection for cache operations and evictions - Implement efficient repo-scoped invalidation using key structure - Add detailed documentation comments explaining cache architecture - Remove outdated dependencies and update dependency versions - Add error handling for encoding failures in cache operations - Optimize Vec responses with length-delimited encoding and pre-allocation
This commit is contained in:
@@ -11,7 +11,10 @@ impl GitBare {
|
||||
let target_branch = request.branch.clone();
|
||||
crate::sanitize::validate_ref_name(&target_branch)?;
|
||||
let cp_revision = match request.commit.and_then(|s| s.selector) {
|
||||
Some(crate::pb::object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(crate::pb::object_selector::Selector::Oid(oid)) => {
|
||||
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
||||
oid.hex
|
||||
}
|
||||
Some(crate::pb::object_selector::Selector::Revision(name)) => {
|
||||
crate::sanitize::validate_revision(&name.revision)?;
|
||||
name.revision
|
||||
|
||||
@@ -110,7 +110,7 @@ impl GitBare {
|
||||
commits.push(read_commit_from_repo(self, &repo, id)?);
|
||||
}
|
||||
|
||||
let end = start_offset + page_ids.len();
|
||||
let end = start_offset.saturating_add(page_ids.len());
|
||||
let has_next = end < total;
|
||||
let page_info = crate::pb::PageInfo {
|
||||
next_page_token: if has_next {
|
||||
|
||||
+15
-2
@@ -29,6 +29,7 @@ impl GitBare {
|
||||
args.push(revision.to_string());
|
||||
|
||||
if !request.path.is_empty() {
|
||||
crate::sanitize::validate_file_path(&request.path)?;
|
||||
args.push("--".to_string());
|
||||
args.push(request.path.clone());
|
||||
}
|
||||
@@ -36,12 +37,18 @@ impl GitBare {
|
||||
let output = std::process::Command::new("git")
|
||||
.args(&args)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.output()
|
||||
.map_err(|e| crate::error::GitError::CommandFailed {
|
||||
status_code: None,
|
||||
stderr: e.to_string(),
|
||||
})?;
|
||||
if !output.status.success() {
|
||||
return Err(crate::error::GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let count = String::from_utf8_lossy(&output.stdout)
|
||||
.trim()
|
||||
@@ -69,12 +76,18 @@ impl GitBare {
|
||||
&format!("{}...{}", request.left, request.right),
|
||||
])
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.output()
|
||||
.map_err(|e| crate::error::GitError::CommandFailed {
|
||||
status_code: None,
|
||||
stderr: e.to_string(),
|
||||
})?;
|
||||
if !output.status.success() {
|
||||
return Err(crate::error::GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
||||
// Format: "<left_count>\t<right_count>"
|
||||
|
||||
@@ -16,7 +16,9 @@ impl GitBare {
|
||||
Some(object_selector::Selector::Revision(name)) => {
|
||||
crate::sanitize::validate_revision(&name.revision)?;
|
||||
}
|
||||
Some(object_selector::Selector::Oid(_)) => {} // OID is always safe
|
||||
Some(object_selector::Selector::Oid(oid)) => {
|
||||
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
||||
}
|
||||
None => {} // will use branch name, already validated
|
||||
}
|
||||
}
|
||||
@@ -30,7 +32,10 @@ impl GitBare {
|
||||
"creating commit"
|
||||
);
|
||||
let start_rev = match request.start_revision.clone().and_then(|s| s.selector) {
|
||||
Some(object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(object_selector::Selector::Oid(oid)) => {
|
||||
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
||||
oid.hex
|
||||
}
|
||||
Some(object_selector::Selector::Revision(name)) => name.revision,
|
||||
None => request.branch.clone(),
|
||||
};
|
||||
|
||||
@@ -6,7 +6,10 @@ impl GitBare {
|
||||
/// Find a single commit by revision.
|
||||
pub fn find_commit(&self, request: FindCommitRequest) -> GitResult<Commit> {
|
||||
let revision = match request.revision.and_then(|s| s.selector) {
|
||||
Some(object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(object_selector::Selector::Oid(oid)) => {
|
||||
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
||||
oid.hex
|
||||
}
|
||||
Some(object_selector::Selector::Revision(name)) => name.revision,
|
||||
None => "HEAD".to_string(),
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ impl GitBare {
|
||||
pub fn list_commits(&self, request: ListCommitsRequest) -> GitResult<ListCommitsResponse> {
|
||||
let revision = resolve_revision!(request.revision.clone());
|
||||
|
||||
let base_args = build_rev_list_args(self, &request, &revision);
|
||||
let base_args = build_rev_list_args(self, &request, &revision)?;
|
||||
|
||||
// 1. Get total count via rev-list --count (lightweight, no object parsing)
|
||||
let total = {
|
||||
@@ -87,7 +87,7 @@ impl GitBare {
|
||||
commits
|
||||
};
|
||||
|
||||
let end = start_offset + page_ids.len();
|
||||
let end = start_offset.saturating_add(page_ids.len());
|
||||
let has_next = end < total;
|
||||
let page_info = crate::pb::PageInfo {
|
||||
next_page_token: if has_next {
|
||||
@@ -106,7 +106,11 @@ impl GitBare {
|
||||
}
|
||||
}
|
||||
|
||||
fn build_rev_list_args(gb: &GitBare, request: &ListCommitsRequest, revision: &str) -> Vec<String> {
|
||||
fn build_rev_list_args(
|
||||
gb: &GitBare,
|
||||
request: &ListCommitsRequest,
|
||||
revision: &str,
|
||||
) -> GitResult<Vec<String>> {
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
gb.bare_dir.to_string_lossy().into_owned(),
|
||||
@@ -136,10 +140,11 @@ fn build_rev_list_args(gb: &GitBare, request: &ListCommitsRequest, revision: &st
|
||||
args.push(revision.to_string());
|
||||
}
|
||||
if !request.path.is_empty() {
|
||||
crate::sanitize::validate_file_path(&request.path)?;
|
||||
args.push("--".into());
|
||||
args.push(request.path.clone());
|
||||
}
|
||||
args
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
/// Read a single commit from an already-opened gix repo (no subprocess).
|
||||
|
||||
+29
-7
@@ -20,12 +20,13 @@ impl GitBare {
|
||||
} else {
|
||||
request.limit.min(200)
|
||||
};
|
||||
let max_count = request.offset.saturating_add(limit).min(10_000);
|
||||
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
self.bare_dir.to_string_lossy().into_owned(),
|
||||
"log".to_string(),
|
||||
format!("--max-count={}", limit),
|
||||
format!("--max-count={max_count}"),
|
||||
"--format=%H".to_string(),
|
||||
];
|
||||
|
||||
@@ -51,6 +52,12 @@ impl GitBare {
|
||||
status_code: None,
|
||||
stderr: e.to_string(),
|
||||
})?;
|
||||
if !output.status.success() {
|
||||
return Err(crate::error::GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let repo = self.gix_repo()?;
|
||||
@@ -94,7 +101,10 @@ impl GitBare {
|
||||
/// Get stats for a single commit.
|
||||
pub fn get_commit_stats(&self, request: GetCommitStatsRequest) -> GitResult<CommitStats> {
|
||||
let revision = match request.revision.and_then(|s| s.selector) {
|
||||
Some(object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(object_selector::Selector::Oid(oid)) => {
|
||||
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
||||
oid.hex
|
||||
}
|
||||
Some(object_selector::Selector::Revision(name)) => name.revision,
|
||||
None => "HEAD".to_string(),
|
||||
};
|
||||
@@ -109,12 +119,18 @@ impl GitBare {
|
||||
&format!("{revision}^!"),
|
||||
])
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.output()
|
||||
.map_err(|e| crate::error::GitError::CommandFailed {
|
||||
status_code: None,
|
||||
stderr: e.to_string(),
|
||||
})?;
|
||||
if !output.status.success() {
|
||||
return Err(crate::error::GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let mut additions = 0u32;
|
||||
@@ -125,12 +141,12 @@ impl GitBare {
|
||||
let parts: Vec<&str> = line.split('\t').collect();
|
||||
if parts.len() >= 2 {
|
||||
if let Ok(add) = parts[0].parse::<u32>() {
|
||||
additions += add;
|
||||
additions = additions.saturating_add(add);
|
||||
}
|
||||
if let Ok(del) = parts[1].parse::<u32>() {
|
||||
deletions += del;
|
||||
deletions = deletions.saturating_add(del);
|
||||
}
|
||||
changed_files += 1;
|
||||
changed_files = changed_files.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,12 +186,18 @@ impl GitBare {
|
||||
let output = std::process::Command::new("git")
|
||||
.args(&args)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.output()
|
||||
.map_err(|e| crate::error::GitError::CommandFailed {
|
||||
status_code: None,
|
||||
stderr: e.to_string(),
|
||||
})?;
|
||||
if !output.status.success() {
|
||||
return Err(crate::error::GitError::CommandFailed {
|
||||
status_code: output.status.code(),
|
||||
stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let hex = stdout.lines().next().unwrap_or("").trim().to_string();
|
||||
|
||||
@@ -8,7 +8,10 @@ impl GitBare {
|
||||
let target_branch = request.branch.clone();
|
||||
crate::sanitize::validate_ref_name(&target_branch)?;
|
||||
let revert_revision = match request.commit.and_then(|s| s.selector) {
|
||||
Some(crate::pb::object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(crate::pb::object_selector::Selector::Oid(oid)) => {
|
||||
crate::sanitize::validate_oid_hex(&oid.hex)?;
|
||||
oid.hex
|
||||
}
|
||||
Some(crate::pb::object_selector::Selector::Revision(name)) => {
|
||||
crate::sanitize::validate_revision(&name.revision)?;
|
||||
name.revision
|
||||
|
||||
Reference in New Issue
Block a user