A distributed cache implementation based on meta-service, providing reliable resource management and data synchronization across distributed systems.
- Automatic Synchronization: Background watcher task keeps local cache in sync with meta-service
- Concurrency Control: Two-level concurrency control mechanism for safe access
- Event-based Updates: Real-time updates through meta-service watch API
- Safe Reconnection: Automatic recovery from connection failures with state consistency
See https://github.com/databendlabs/sub-cache/ for details about Usage, Internal types and Concurrency Control.
<prefix>/foo
<prefix>/..
<prefix>/..
<prefix>: User-defined string to identify a cache instance
let client = MetaGrpcClient::try_create(/*..*/);
let cache = Cache::new(
client,
"your/cache/key/space/in/meta/service",
"your-app-name-for-logging",
).await;
// Access cached data
cache.try_access(|c: &CacheData| {
println!("last-seq:{}", c.last_seq);
println!("all data: {:?}", c.data);
}).await?;
// Get a specific value
let value = cache.try_get("key").await?;
// List all entries under a prefix
let entries = cache.try_list_dir("prefix").await?;- Creates instance with prefix and context
- Spawns background watcher task; Establishes watch stream to meta-service
- Fetches initial data; Waits for full initialization
- Maintains continuous sync
Initialization completes when cache receives full data copy from meta-service, ensuring consistent view.
The cache implements robust error handling:
- Connection failures are automatically retried in the background
- Background watcher task automatically recovers from errors
- Users are shielded from transient errors through the abstraction
- The cache ensures data consistency by tracking sequence numbers
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.