934858bebf
- 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
56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
/// The null OID representing "no object" in git protocol.
|
|
pub const ZERO_OID: &str = "0000000000000000000000000000000000000000";
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
|
|
pub struct ObjectId(pub String);
|
|
|
|
impl ObjectId {
|
|
pub fn new(hex: impl AsRef<str>) -> Self {
|
|
Self(hex.as_ref().to_lowercase())
|
|
}
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
impl std::fmt::Display for ObjectId {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}", self.0)
|
|
}
|
|
}
|
|
|
|
impl AsRef<str> for ObjectId {
|
|
fn as_ref(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, crate::error::GitError> {
|
|
let hex = hex.trim();
|
|
if !hex.len().is_multiple_of(2) {
|
|
return Err(crate::error::GitError::InvalidOid(
|
|
"hex oid has odd length".into(),
|
|
));
|
|
}
|
|
|
|
hex.as_bytes()
|
|
.chunks_exact(2)
|
|
.map(|pair| {
|
|
let part = std::str::from_utf8(pair)
|
|
.map_err(|e| crate::error::GitError::InvalidOid(e.to_string()))?;
|
|
u8::from_str_radix(part, 16)
|
|
.map_err(|e| crate::error::GitError::InvalidOid(e.to_string()))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
impl TryFrom<&ObjectId> for gix::hash::ObjectId {
|
|
type Error = crate::error::GitError;
|
|
|
|
fn try_from(id: &ObjectId) -> Result<Self, Self::Error> {
|
|
gix::hash::ObjectId::from_hex(id.as_str().as_bytes())
|
|
.map_err(|e| crate::error::GitError::InvalidOid(format!("invalid hex oid: {e}")))
|
|
}
|
|
}
|