pub trait Cache<K, V>:
Debug
+ Send
+ Sync {
// Required methods
fn insert(&self, key: K, value: V) -> Box<dyn CacheEntry<V>>;
fn get(&self, key: &K) -> Option<Box<dyn CacheEntry<V>>>;
fn remove(&self, key: &K);
fn new_id(&self) -> u64;
}
Expand description
A cache that stores mappings from keys to values.
§Concurrency
Implementations of this trait must be thread-safe.
Required Methods§
Sourcefn insert(&self, key: K, value: V) -> Box<dyn CacheEntry<V>>
fn insert(&self, key: K, value: V) -> Box<dyn CacheEntry<V>>
Insert the key-value pair into the cache.
If the key already existed in the cache, no update is performed.
Returns a CacheEntry
for the inserted value.
Sourcefn get(&self, key: &K) -> Option<Box<dyn CacheEntry<V>>>
fn get(&self, key: &K) -> Option<Box<dyn CacheEntry<V>>>
Get the cached value for the given key. Returns None
if the key is not in the cache.
Sourcefn new_id(&self) -> u64
fn new_id(&self) -> u64
A numeric ID for different clients of the cache.
A cache may be used by different clients that are sharing the same cache. This ID is used to partition the key space between the clients. Typically a client will allocate a new ID at startup and prepend the ID to its cache keys.