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.
§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),
};
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.
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.
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.