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
+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
}