Skip to content
Clone
ed3111f
82 lines 1.7 KB Rust
use std::time::Duration;
use cache::CacheClient;
use serde::{Serialize, de::DeserializeOwned};
use crate::share::errors::GitError;
pub const REV_TTL: Duration = Duration::from_secs(60);
pub(crate) fn sanitize_key(k: &str) -> String {
let mut out = String::with_capacity(k.len());
for c in k.chars() {
if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | ':') {
out.push(c);
} else {
out.push('_');
}
}
out
}
fn oid_key(kind: &str, oid: &str) -> String {
sanitize_key(&format!("object.{kind}.{oid}"))
}
fn rev_key(module: &str, func: &str, key_part: &str, tip: &str) -> String {
sanitize_key(&format!("{module}.{func}.{key_part}:{tip}"))
}
pub async fn cache_oid_get<V>(
cache: &CacheClient,
kind: &str,
oid: &str,
) -> Result<Option<V>, GitError>
where
V: DeserializeOwned,
{
Ok(cache.get(&oid_key(kind, oid)).await?)
}
pub async fn cache_oid_put<V>(
cache: &CacheClient,
kind: &str,
oid: &str,
v: &V,
) -> Result<(), GitError>
where
V: Serialize,
{
cache.set(&oid_key(kind, oid), v, None).await?;
Ok(())
}
pub async fn cache_rev_get<V>(
cache: &CacheClient,
module: &str,
func: &str,
key_part: &str,
tip: &str,
) -> Result<Option<V>, GitError>
where
V: DeserializeOwned,
{
Ok(cache.get(&rev_key(module, func, key_part, tip)).await?)
}
pub async fn cache_rev_put<V>(
cache: &CacheClient,
module: &str,
func: &str,
key_part: &str,
tip: &str,
v: &V,
) -> Result<(), GitError>
where
V: Serialize,
{
cache
.set(&rev_key(module, func, key_part, tip), v, Some(REV_TTL))
.await?;
Ok(())
}