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
+10
View File
@@ -12,11 +12,21 @@ impl GitBare {
return Ok(FindMergeBaseResponse::default());
}
const MAX_MERGE_BASE_REVISIONS: usize = 64;
if request.revisions.len() > MAX_MERGE_BASE_REVISIONS {
return Err(crate::error::GitError::InvalidArgument(format!(
"too many revisions (max {MAX_MERGE_BASE_REVISIONS})"
)));
}
let revisions: Vec<String> = request
.revisions
.iter()
.map(|b| String::from_utf8_lossy(b).to_string())
.collect();
for revision in &revisions {
crate::sanitize::validate_revision(revision)?;
}
if revisions.len() < 2 {
return Ok(FindMergeBaseResponse {
+25 -13
View File
@@ -12,6 +12,7 @@ include!(concat!(env!("OUT_DIR"), "/linguist_generated.rs"));
/// Default max file size for line counting (512 KB).
const DEFAULT_MAX_FILE_SIZE: u32 = 512 * 1024;
const MAX_TREE_WALK_DEPTH: usize = 256;
/// Look up a language by file extension (case-insensitive, includes leading dot).
fn lookup_by_extension(ext: &str) -> Option<(&'static str, &'static str)> {
@@ -122,7 +123,10 @@ impl GitBare {
) -> GitResult<GetLanguageStatsResponse> {
let repo = self.gix_repo()?;
let revision = match request.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)) => {
crate::sanitize::validate_revision(&name.revision)?;
name.revision
@@ -144,6 +148,7 @@ impl GitBare {
// If path is specified, descend into subdirectory
if !request.path.is_empty() {
crate::sanitize::validate_file_path(&request.path)?;
let entry = tree
.lookup_entry_by_path(&request.path)?
.ok_or_else(|| GitError::NotFound(request.path.clone()))?;
@@ -166,7 +171,7 @@ impl GitBare {
total_bytes: &mut total_bytes,
total_lines: &mut total_lines,
};
self.walk_tree(&repo, &tree, &prefix, &mut ctx)?;
self.walk_tree(&repo, &tree, &prefix, 0, &mut ctx)?;
// Resolve groups: merge child language stats into parent group
tracing::info!(
@@ -185,9 +190,9 @@ impl GitBare {
lang_type: s.lang_type.clone(),
..Default::default()
});
entry.file_count += s.file_count;
entry.bytes += s.bytes;
entry.lines += s.lines;
entry.file_count = entry.file_count.saturating_add(s.file_count);
entry.bytes = entry.bytes.saturating_add(s.bytes);
entry.lines = entry.lines.saturating_add(s.lines);
// Keep the lang_type from the parent (or first encountered)
if entry.lang_type.is_empty() {
entry.lang_type = s.lang_type;
@@ -233,8 +238,15 @@ impl GitBare {
_repo: &gix::Repository,
tree: &gix::Tree<'_>,
prefix: &str,
depth: usize,
ctx: &mut WalkContext<'_>,
) -> GitResult<()> {
if depth > MAX_TREE_WALK_DEPTH {
return Err(GitError::InvalidArgument(format!(
"tree depth exceeds maximum of {MAX_TREE_WALK_DEPTH}"
)));
}
for entry in tree.iter() {
let entry = entry?;
let name = String::from_utf8_lossy(entry.filename()).into_owned();
@@ -250,7 +262,7 @@ impl GitBare {
.object()?
.try_into_tree()
.map_err(|e| GitError::Gix(e.to_string()))?;
self.walk_tree(_repo, &child_tree, &path, ctx)?;
self.walk_tree(_repo, &child_tree, &path, depth + 1, ctx)?;
}
EntryKind::Blob | EntryKind::BlobExecutable => {
let blob = entry
@@ -277,15 +289,15 @@ impl GitBare {
let lang_key = lang_name.to_string();
// Count code lines only for non-binary files within size limit
let lines = if !is_binary && (size as u32) <= ctx.max_file_size {
let lines = if !is_binary && size <= u64::from(ctx.max_file_size) {
count_code_lines(data)
} else {
0
};
*ctx.total_files += 1;
*ctx.total_bytes += size;
*ctx.total_lines += lines;
*ctx.total_files = ctx.total_files.saturating_add(1);
*ctx.total_bytes = ctx.total_bytes.saturating_add(size);
*ctx.total_lines = ctx.total_lines.saturating_add(lines);
let s = ctx
.stats
@@ -294,9 +306,9 @@ impl GitBare {
lang_type: lang_type.to_string(),
..Default::default()
});
s.file_count += 1;
s.bytes += size;
s.lines += lines;
s.file_count = s.file_count.saturating_add(1);
s.bytes = s.bytes.saturating_add(size);
s.lines = s.lines.saturating_add(lines);
}
_ => {} // Skip symlinks, submodules
}
+20 -1
View File
@@ -9,9 +9,16 @@ impl GitBare {
return Ok(ObjectsSizeResponse::default());
}
const MAX_OBJECTS_SIZE_OIDS: usize = 10_000;
if request.oids.len() > MAX_OBJECTS_SIZE_OIDS {
return Err(crate::error::GitError::InvalidArgument(format!(
"too many oids (max {MAX_OBJECTS_SIZE_OIDS})"
)));
}
let mut input = String::new();
for oid in &request.oids {
crate::sanitize::validate_revision(oid)?;
crate::sanitize::validate_oid_hex(oid)?;
input.push_str(oid);
input.push('\n');
}
@@ -49,6 +56,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 mut sizes = Vec::new();
@@ -81,6 +94,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 size = stdout
+6
View File
@@ -29,6 +29,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 mut changes = Vec::new();
+26 -5
View File
@@ -2,23 +2,29 @@ use crate::bare::GitBare;
use crate::error::GitResult;
use crate::pb::*;
const MAX_SEARCH_RESULTS: u32 = 1000;
impl GitBare {
/// Search file contents with a regex pattern.
pub fn search_files_by_content(
&self,
request: SearchFilesByContentRequest,
) -> GitResult<SearchFilesByContentResponse> {
crate::sanitize::validate_revision(&request.revision)?;
let revision = if request.revision.is_empty() {
"HEAD"
} else {
&request.revision
};
crate::sanitize::validate_revision(revision)?;
if request.query.is_empty() {
return Err(crate::error::GitError::InvalidArgument(
"search query cannot be empty".into(),
));
}
let max_results = if request.max_results == 0 {
100
} else {
request.max_results
request.max_results.min(MAX_SEARCH_RESULTS)
};
let mut args = vec![
@@ -51,6 +57,12 @@ impl GitBare {
})?;
// git grep returns exit code 1 when no matches found — that's not an error
if !output.status.success() && output.status.code() != Some(1) {
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 results = Vec::new();
@@ -59,13 +71,16 @@ impl GitBare {
if let Some((path_and_rest, matched)) = line.rsplit_once(':') {
let prefix_parts: Vec<&str> = path_and_rest.rsplitn(3, ':').collect();
if prefix_parts.len() >= 3
&& let Ok(line_num) = prefix_parts[0].parse::<u32>()
&& let Ok(line_num) = prefix_parts[1].parse::<u32>()
{
results.push(SearchResult {
path: prefix_parts[2].to_string(),
line: line_num,
matched_text: matched.to_string(),
});
if results.len() >= max_results as usize {
break;
}
}
}
}
@@ -88,7 +103,7 @@ impl GitBare {
let max_results = if request.max_results == 0 {
100
} else {
request.max_results
request.max_results.min(MAX_SEARCH_RESULTS)
};
let mut args = vec![
@@ -113,6 +128,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 mut results = Vec::new();