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:
+18
-1
@@ -40,8 +40,22 @@ impl LocalSnapshotStorage {
|
||||
Self { base_dir }
|
||||
}
|
||||
|
||||
fn validate_snapshot_id(snapshot_id: &str) -> Result<(), String> {
|
||||
if snapshot_id.is_empty() {
|
||||
return Err("snapshot_id cannot be empty".into());
|
||||
}
|
||||
if snapshot_id.len() > 64
|
||||
|| !snapshot_id
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
|
||||
{
|
||||
return Err(format!("invalid snapshot_id: {snapshot_id}"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn snapshot_dir(&self, snapshot_id: &str) -> PathBuf {
|
||||
let prefix = &snapshot_id[..2.min(snapshot_id.len())];
|
||||
let prefix = snapshot_id.get(..2).unwrap_or(snapshot_id);
|
||||
self.base_dir.join(prefix).join(snapshot_id)
|
||||
}
|
||||
|
||||
@@ -62,6 +76,7 @@ impl SnapshotStorageBackend for LocalSnapshotStorage {
|
||||
head_oid: &str,
|
||||
data: &[u8],
|
||||
) -> Result<(), String> {
|
||||
Self::validate_snapshot_id(snapshot_id)?;
|
||||
let dir = self.snapshot_dir(snapshot_id);
|
||||
std::fs::create_dir_all(&dir).map_err(|e| format!("create dir: {e}"))?;
|
||||
|
||||
@@ -95,6 +110,7 @@ impl SnapshotStorageBackend for LocalSnapshotStorage {
|
||||
}
|
||||
|
||||
fn read_snapshot(&self, snapshot_id: &str) -> Result<Vec<u8>, String> {
|
||||
Self::validate_snapshot_id(snapshot_id)?;
|
||||
let path = self.data_path(snapshot_id);
|
||||
if !path.exists() {
|
||||
return Err(format!("snapshot not found: {snapshot_id}"));
|
||||
@@ -155,6 +171,7 @@ impl SnapshotStorageBackend for LocalSnapshotStorage {
|
||||
}
|
||||
|
||||
fn delete_snapshot(&self, snapshot_id: &str) -> Result<(), String> {
|
||||
Self::validate_snapshot_id(snapshot_id)?;
|
||||
let dir = self.snapshot_dir(snapshot_id);
|
||||
if !dir.exists() {
|
||||
return Err(format!("snapshot not found: {snapshot_id}"));
|
||||
|
||||
Reference in New Issue
Block a user