mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-01 17:00:21 +08:00
refactor: 抽离 AI pipeline 与调度共享能力逻辑
This commit is contained in:
@@ -2,4 +2,4 @@ mod namespace;
|
||||
mod ttl_map;
|
||||
|
||||
pub use namespace::CacheKeyNamespace;
|
||||
pub use ttl_map::ExpiringMap;
|
||||
pub use ttl_map::{ExpiringMap, ExpiringMapFreshEntry};
|
||||
|
||||
@@ -14,6 +14,13 @@ pub struct ExpiringMap<K, V> {
|
||||
entries: Mutex<HashMap<K, TimedEntry<V>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExpiringMapFreshEntry<K, V> {
|
||||
pub key: K,
|
||||
pub value: V,
|
||||
pub age: Duration,
|
||||
}
|
||||
|
||||
impl<K, V> Default for ExpiringMap<K, V> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@@ -98,6 +105,22 @@ where
|
||||
pub fn contains_fresh(&self, key: &K, ttl: Duration) -> bool {
|
||||
self.get_fresh(key, ttl).is_some()
|
||||
}
|
||||
|
||||
pub fn snapshot_fresh(&self, ttl: Duration) -> Vec<ExpiringMapFreshEntry<K, V>> {
|
||||
let Ok(mut entries) = self.entries.lock() else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
prune_expired(&mut entries, ttl);
|
||||
entries
|
||||
.iter()
|
||||
.map(|(key, entry)| ExpiringMapFreshEntry {
|
||||
key: key.clone(),
|
||||
value: entry.value.clone(),
|
||||
age: entry.inserted_at.elapsed(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn prune_expired<K, V>(entries: &mut HashMap<K, TimedEntry<V>>, ttl: Duration)
|
||||
|
||||
Reference in New Issue
Block a user