pub struct NegativeCache { /* private fields */ }Expand description
Thread-safe negative cache for filesystem lookups.
Caches paths that are known to not exist, avoiding repeated system calls
for non-existent files. Uses lock-free concurrent access via DashMap.
When adaptive TTL is configured, different paths receive different cache
durations based on pattern matching. For example, lookups inside
/node_modules/ get a 30s TTL while source files default to 5s.
§Example
use std::time::Duration;
use std::path::PathBuf;
use arcbox_fs::cache::{NegativeCache, NegativeCacheConfig};
let config = NegativeCacheConfig {
max_entries: 1000,
timeout: Duration::from_millis(500),
adaptive_ttl: None,
};
let cache = NegativeCache::new(config);
// File lookup failed, add to negative cache
cache.insert(PathBuf::from("/app/node_modules/missing-package"));
// Later lookup - returns true without syscall
assert!(cache.contains(&PathBuf::from("/app/node_modules/missing-package")));
// File created - invalidate cache
cache.invalidate(&PathBuf::from("/app/node_modules/missing-package"));
assert!(!cache.contains(&PathBuf::from("/app/node_modules/missing-package")));Implementations§
Source§impl NegativeCache
impl NegativeCache
Sourcepub fn new(config: NegativeCacheConfig) -> Self
pub fn new(config: NegativeCacheConfig) -> Self
Creates a new negative cache with the given configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Creates a new negative cache with default configuration (adaptive TTL enabled).
Sourcepub fn contains(&self, path: &Path) -> bool
pub fn contains(&self, path: &Path) -> bool
Checks if the path is in the negative cache and not expired.
Returns true if the path was previously marked as non-existent
and the cache entry has not expired.
Sourcepub fn insert(&self, path: PathBuf)
pub fn insert(&self, path: PathBuf)
Adds a path to the negative cache.
The TTL is determined by adaptive TTL rules if configured, or by the
global timeout value otherwise.
If the cache is at capacity, expired entries are evicted first.
Sourcepub fn invalidate(&self, path: &Path)
pub fn invalidate(&self, path: &Path)
Invalidates a path in the negative cache.
This should be called when a file is created to ensure subsequent lookups don’t incorrectly return “not found” from the cache.
Also invalidates the parent directory path to handle cases where the parent’s directory listing was cached.
Sourcepub fn evict_expired(&self)
pub fn evict_expired(&self)
Removes all expired entries from the cache.
This is called automatically when the cache reaches capacity, but can also be called manually for maintenance.
Sourcepub fn stats(&self) -> NegativeCacheStats
pub fn stats(&self) -> NegativeCacheStats
Returns current cache statistics.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for NegativeCache
impl !RefUnwindSafe for NegativeCache
impl Send for NegativeCache
impl Sync for NegativeCache
impl Unpin for NegativeCache
impl UnsafeUnpin for NegativeCache
impl UnwindSafe for NegativeCache
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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