mod.rs

Overview

This file defines caching abstractions and implementations for a key-value storage system, providing an intermediary cache layer that wraps around a lower-level KeyValueStore. It includes a generic Cache trait, a Least Recently Used (LRU) sized cache implementation (LruSizedCache), and a composite store (CachedStore) that combines a cache and an underlying database store to improve read efficiency and reduce direct database hits.


Traits and Structs

Cache Trait

The Cache trait abstracts the behavior of a thread-safe cache for key-value pairs, where keys are Aerospike Key instances and values are ValueMaps (a map representation of bins and their values).

Methods

Usage Example

fn example(cache: &impl Cache, key: &Key, value: ValueMap) {
    cache.put(key, value.clone());
    if let Some(cached_value) = cache.get(key) {
        // Use cached_value
    }
    cache.invalidate(key);
}

LruSizedCache Struct

A thread-safe, fixed-size LRU cache implementation of the Cache trait. It internally uses cached::SizedCache wrapped in a parking_lot::Mutex for synchronization, and Arc for shared ownership.

Fields

Methods

Trait Implementation: Cache

Implementation Details


CachedStore<B, C> Struct

A generic, composable key-value store that integrates an underlying database store (B: KeyValueStore) with a cache layer (C: Cache). This store attempts to serve reads from the cache before falling back to the database, and writes update both layers.

Fields

Methods

Trait Implementation: KeyValueStore

Implements the core key-value operations with caching logic.

Implementation Details


Interaction with Other Modules


Visual Diagram

classDiagram
class Cache {
<<trait>>
+get()
+put()
+invalidate()
}
class LruSizedCache {
-cache: Arc<Mutex<SizedCache>>
+new()
+get()
+put()
+invalidate()
}
class KeyValueStore {
<<trait>>
+get()
+put()
+batch_get()
+db_reads() [debug only]
+db_writes() [debug only]
}
class CachedStore {
-db: KeyValueStore
-cache: Cache
+new()
+get()
+put()
+batch_get()
+db_reads() [debug only]
+db_writes() [debug only]
}
Cache <|.. LruSizedCache
KeyValueStore <|.. CachedStore
CachedStore --> Cache : uses
CachedStore --> KeyValueStore : uses

Key Algorithms and Implementation Notes


Usage Context

This module is intended to be used wherever key-value data retrieval and storage occurs, enhancing performance by avoiding repeated database access for frequently requested keys. It can be integrated into larger systems requiring fast data access and reliability with consistent caching semantics. The generic design allows it to wrap any KeyValueStore implementation and use any cache conforming to the Cache trait, promoting modularity and testability.

For further exploration of Aerospike key management and bin structures, see Aerospike Key Handling and Bin Data Structures. For details on caching strategies and concurrency, refer to Caching Strategies and Concurrency Primitives.