pub struct RecentCaptureChecker {
cache: RwLock<LruCache<String, CacheEntry>>,
ttl: Duration,
}Expand description
Checker for recently captured content.
§How it works
- Maintains an LRU cache mapping content hashes to capture info
- When checking, looks up the hash in the cache
- Returns a match if found and not expired (within TTL window)
- Automatically evicts expired entries and maintains LRU ordering
§Thread Safety
Uses RwLock for interior mutability, allowing concurrent reads
and exclusive writes. Safe for use across async tasks.
§Lock Poisoning
Lock poisoning is handled with fail-open semantics: if the lock is
poisoned (due to a panic in another thread), operations return None
(for checks) or silently skip (for records). This is intentional:
- Deduplication is a performance optimization, not a correctness requirement
- Failing to detect a duplicate just means we capture twice (safe)
- Blocking all captures due to a transient panic would be worse
§Example
use subcog::services::deduplication::RecentCaptureChecker;
use std::time::Duration;
let checker = RecentCaptureChecker::new(1000, Duration::from_secs(300));
// Record a capture
checker.record("content", MemoryId::new("id1"), Namespace::Decisions, "project");
// Check if same content was recently captured
let result = checker.check("content", Namespace::Decisions);
assert!(result.is_some());Fields§
§cache: RwLock<LruCache<String, CacheEntry>>LRU cache mapping content hash to capture entry.
Uses RwLock for thread-safe interior mutability.
ttl: DurationTime-to-live for cache entries.
Implementations§
Source§impl RecentCaptureChecker
impl RecentCaptureChecker
Sourcepub fn default_settings() -> Self
pub fn default_settings() -> Self
Creates a checker with default settings.
Default: 1000 entries, 5 minute TTL.
Sourcepub fn check(
&self,
content: &str,
namespace: Namespace,
) -> Option<(MemoryId, String)>
pub fn check( &self, content: &str, namespace: Namespace, ) -> Option<(MemoryId, String)>
Checks if content was recently captured in the given namespace.
§Arguments
content- The content to checknamespace- The namespace to check within
§Returns
Returns Some((MemoryId, URN)) if content was recently captured,
None otherwise.
§Example
let result = checker.check("content", Namespace::Decisions);
match result {
Some((id, urn)) => println!("Recently captured: {}", urn),
None => println!("Not recently captured"),
}Sourcepub fn record(
&self,
content: &str,
memory_id: &MemoryId,
namespace: Namespace,
domain: &str,
)
pub fn record( &self, content: &str, memory_id: &MemoryId, namespace: Namespace, domain: &str, )
Records a successful capture for future duplicate detection.
§Arguments
content- The captured contentmemory_id- The ID assigned to the captured memorynamespace- The namespace the content was captured todomain- The domain the content was captured to
§Example
checker.record(
"Use PostgreSQL for storage",
MemoryId::new("mem-123"),
Namespace::Decisions,
"project",
);Sourcepub fn record_by_hash(
&self,
content_hash: &str,
memory_id: &MemoryId,
namespace: Namespace,
domain: &str,
)
pub fn record_by_hash( &self, content_hash: &str, memory_id: &MemoryId, namespace: Namespace, domain: &str, )
Records a capture using just the content hash.
Useful when the hash has already been computed.
§Arguments
content_hash- The pre-computed content hashmemory_id- The ID assigned to the captured memorynamespace- The namespace the content was captured todomain- The domain the content was captured to
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for RecentCaptureChecker
impl RefUnwindSafe for RecentCaptureChecker
impl Send for RecentCaptureChecker
impl Sync for RecentCaptureChecker
impl Unpin for RecentCaptureChecker
impl UnwindSafe for RecentCaptureChecker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].