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:
zhenyi
2026-06-12 12:53:23 +08:00
parent a40da90ef9
commit 934858bebf
82 changed files with 1273 additions and 4969 deletions
+34 -3
View File
@@ -2,6 +2,8 @@ use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::*;
const MAX_RAW_DIFF_OUTPUT_BYTES: usize = 64 * 1024 * 1024;
impl GitBare {
/// Stream raw diff output.
pub fn raw_diff(&self, request: RawDiffRequest) -> GitResult<Vec<RawDiffResponse>> {
@@ -16,6 +18,7 @@ impl GitBare {
"diff".to_string(),
];
let mut pathspecs = Vec::new();
// Apply options if present
if let Some(ref opts) = request.options {
if opts.recursive {
@@ -26,14 +29,18 @@ impl GitBare {
} else {
args.push("--no-binary".to_string());
}
for ps in &opts.pathspec {
args.push("--".to_string());
args.push(ps.clone());
for pathspec in &opts.pathspec {
crate::sanitize::validate_file_path(pathspec)?;
pathspecs.push(pathspec.clone());
}
}
args.push(base.clone());
args.push(head.clone());
if !pathspecs.is_empty() {
args.push("--".to_string());
args.extend(pathspecs);
}
let output = std::process::Command::new("git")
.args(&args)
@@ -44,6 +51,18 @@ 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(),
});
}
if output.stdout.len() > MAX_RAW_DIFF_OUTPUT_BYTES {
return Err(crate::error::GitError::InvalidArgument(format!(
"raw diff output too large (max {MAX_RAW_DIFF_OUTPUT_BYTES} bytes)"
)));
}
// Chunk the output for streaming
const CHUNK_SIZE: usize = 32768;
@@ -78,6 +97,18 @@ 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(),
});
}
if output.stdout.len() > MAX_RAW_DIFF_OUTPUT_BYTES {
return Err(crate::error::GitError::InvalidArgument(format!(
"raw patch output too large (max {MAX_RAW_DIFF_OUTPUT_BYTES} bytes)"
)));
}
const CHUNK_SIZE: usize = 32768;
let data = output.stdout;