ThreatFlux Cache
A flexible, async-first cache library for Rust with pluggable backends, multiple eviction policies, and advanced search capabilities.
Features
- Async-first design: Built on tokio for high-performance async operations
- Generic key-value storage: Works with any serializable types
- Multiple backends:
- In-memory storage (default)
- Filesystem persistence
- Easy to add custom backends
- Eviction policies:
- LRU (Least Recently Used)
- LFU (Least Frequently Used)
- FIFO (First In First Out)
- TTL (Time To Live)
- Manual only
- Advanced features:
- Entry metadata and custom attributes
- Search and query capabilities
- Compression support
- Metrics integration
- Automatic persistence
- Entry statistics and access tracking
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Feature Flags
default: Enables filesystem backend and JSON serializationfilesystem-backend: Filesystem storage supportjson-serialization: JSON format supportbincode-serialization: Bincode format supportcompression: Compression support for stored valuesopenapi: OpenAPI schema generationmetrics: Prometheus metrics integrationtracing: Tracing supportfull: All features enabled
Quick Start
Basic Usage
use *;
use ;
async
With Filesystem Persistence
use *;
let config = default
.with_persistence
.with_eviction_policy;
let backend = new.await?;
let cache: = new.await?;
Custom Metadata
use ;
let metadata = BasicMetadata ;
let entry = with_metadata;
cache.add_entry.await?;
Search Capabilities
use SearchQuery;
// Search by pattern and category
let query = new
.with_pattern
.with_category
.with_access_count_range;
let results = cache.search.await;
for entry in results
Migration from file-scanner
If you're migrating from file-scanner's built-in cache, see the examples/file_scanner_migration.rs for a complete migration guide. The library provides an adapter pattern to maintain API compatibility while gaining the benefits of the new cache system.
Configuration Options
let config = default
// Capacity settings
.with_max_entries_per_key
.with_max_total_entries
// Eviction policy
.with_eviction_policy
// Persistence
.with_persistence
// TTL for all entries
.with_default_ttl
// Enable compression
.with_compression;
Custom Storage Backend
Implement the StorageBackend trait to create custom storage solutions:
use async_trait;
use ;
;
Performance Considerations
- The cache uses
Arc<RwLock<HashMap>>for thread-safe concurrent access - Batch operations are preferred for bulk updates
- Filesystem backend saves are throttled using a semaphore
- Consider compression for large values to reduce I/O
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.