pub struct FifoCache<K, V> { /* private fields */ }Expand description
A FIFO cache with TTL support.
This cache maintains insertion order and evicts the oldest entries when the maximum size is reached. Entries also expire after the specified TTL.
Note that:
- reinserting an existing entry will not move it back to the front
- the maximum capacity may very briefly be exceeded by 1
Implementations§
Source§impl<K, V> FifoCache<K, V>
impl<K, V> FifoCache<K, V>
Sourcepub fn new(max_size: usize, default_ttl: Duration) -> Self
pub fn new(max_size: usize, default_ttl: Duration) -> Self
Creates a new FIFO cache with the specified maximum size and default TTL.
§Arguments
max_size- Maximum number of entries the cache can holddefault_ttl- Default time-to-live for cache entries
Examples found in repository?
5fn main() {
6 // Create a cache with capacity 1000 and 5-minute TTL
7 let mut cache = FifoCache::new(
8 1000,
9 #[cfg(feature = "ttl")]
10 Duration::from_secs(300)
11 );
12
13 // Insert some values
14 cache.insert("user:123", "John Doe");
15 cache.insert("user:456", "Jane Smith");
16 cache.insert("config:timeout", "30");
17
18 // Retrieve values
19 if let Some(name) = cache.get(&"user:123") {
20 println!("Found user: {}", name);
21 }
22
23 // Cache will automatically evict oldest entries when full
24 // and expire entries after 5 minutes
25
26 println!("Cache size: {}/{}", cache.len(), cache.max_size());
27
28 #[cfg(feature = "ttl")]
29 // Manual cleanup of expired entries
30 cache.cleanup_expired();
31}Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
Retrieves a value from the cache if it exists and hasn’t expired.
§Arguments
key- The key to look up
§Returns
Some(&V) if the key exists and hasn’t expired, None otherwise.
§Example
With TTL enabled:
use fifo_cache::FifoCache;
use std::time::Duration;
let mut cache = FifoCache::new(100, Duration::from_secs(60));
cache.insert("my_key", "my_value");
let value = cache.get(&"my_key");
assert_eq!(value, Some(&"my_value"));Without TTL:
use fifo_cache::FifoCache;
let mut cache = FifoCache::new(100);
cache.insert("my_key", "my_value");
assert_eq!(cache.get(&"my_key"), Some(&"my_value"));Examples found in repository?
5fn main() {
6 // Create a cache with capacity 1000 and 5-minute TTL
7 let mut cache = FifoCache::new(
8 1000,
9 #[cfg(feature = "ttl")]
10 Duration::from_secs(300)
11 );
12
13 // Insert some values
14 cache.insert("user:123", "John Doe");
15 cache.insert("user:456", "Jane Smith");
16 cache.insert("config:timeout", "30");
17
18 // Retrieve values
19 if let Some(name) = cache.get(&"user:123") {
20 println!("Found user: {}", name);
21 }
22
23 // Cache will automatically evict oldest entries when full
24 // and expire entries after 5 minutes
25
26 println!("Cache size: {}/{}", cache.len(), cache.max_size());
27
28 #[cfg(feature = "ttl")]
29 // Manual cleanup of expired entries
30 cache.cleanup_expired();
31}Sourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
Inserts a key-value pair into the cache.
If the key already exists, its value is updated and TTL is refreshed. If the cache is at capacity, the oldest entry is evicted.
§Arguments
key- The key to insertvalue- The value to associate with the key
Examples found in repository?
5fn main() {
6 // Create a cache with capacity 1000 and 5-minute TTL
7 let mut cache = FifoCache::new(
8 1000,
9 #[cfg(feature = "ttl")]
10 Duration::from_secs(300)
11 );
12
13 // Insert some values
14 cache.insert("user:123", "John Doe");
15 cache.insert("user:456", "Jane Smith");
16 cache.insert("config:timeout", "30");
17
18 // Retrieve values
19 if let Some(name) = cache.get(&"user:123") {
20 println!("Found user: {}", name);
21 }
22
23 // Cache will automatically evict oldest entries when full
24 // and expire entries after 5 minutes
25
26 println!("Cache size: {}/{}", cache.len(), cache.max_size());
27
28 #[cfg(feature = "ttl")]
29 // Manual cleanup of expired entries
30 cache.cleanup_expired();
31}Sourcepub fn insert_lazy<Kinto: Into<K>, Vinto: Into<V>>(
&mut self,
key: Kinto,
value: Vinto,
)
pub fn insert_lazy<Kinto: Into<K>, Vinto: Into<V>>( &mut self, key: Kinto, value: Vinto, )
Inserts a key-value pair into the cache using types that can be converted into the key and value types.
This is a convenience wrapper around insert that accepts any types implementing
Into<K> and Into<V>. Note that using only insert_lazy prevents type inference, so you’ll
need to explicitly specify the cache types:
use fifo_cache::FifoCache;
use std::time::Duration;
// With insert - types are inferred
let mut cache = FifoCache::new(100, Duration::from_secs(60));
cache.insert("key", "value"); // FifoCache<&str, &str>
// With insert_lazy - types must be specified
let mut cache: FifoCache<String, String> = FifoCache::new(100, Duration::from_secs(60));
cache.insert_lazy("key", "value"); // &str -> String conversionSourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the current number of entries in the cache.
Examples found in repository?
5fn main() {
6 // Create a cache with capacity 1000 and 5-minute TTL
7 let mut cache = FifoCache::new(
8 1000,
9 #[cfg(feature = "ttl")]
10 Duration::from_secs(300)
11 );
12
13 // Insert some values
14 cache.insert("user:123", "John Doe");
15 cache.insert("user:456", "Jane Smith");
16 cache.insert("config:timeout", "30");
17
18 // Retrieve values
19 if let Some(name) = cache.get(&"user:123") {
20 println!("Found user: {}", name);
21 }
22
23 // Cache will automatically evict oldest entries when full
24 // and expire entries after 5 minutes
25
26 println!("Cache size: {}/{}", cache.len(), cache.max_size());
27
28 #[cfg(feature = "ttl")]
29 // Manual cleanup of expired entries
30 cache.cleanup_expired();
31}Sourcepub fn cleanup_expired(&mut self)
pub fn cleanup_expired(&mut self)
Removes all expired entries from the cache.
Examples found in repository?
5fn main() {
6 // Create a cache with capacity 1000 and 5-minute TTL
7 let mut cache = FifoCache::new(
8 1000,
9 #[cfg(feature = "ttl")]
10 Duration::from_secs(300)
11 );
12
13 // Insert some values
14 cache.insert("user:123", "John Doe");
15 cache.insert("user:456", "Jane Smith");
16 cache.insert("config:timeout", "30");
17
18 // Retrieve values
19 if let Some(name) = cache.get(&"user:123") {
20 println!("Found user: {}", name);
21 }
22
23 // Cache will automatically evict oldest entries when full
24 // and expire entries after 5 minutes
25
26 println!("Cache size: {}/{}", cache.len(), cache.max_size());
27
28 #[cfg(feature = "ttl")]
29 // Manual cleanup of expired entries
30 cache.cleanup_expired();
31}Sourcepub fn max_size(&self) -> usize
pub fn max_size(&self) -> usize
Returns the maximum capacity of the cache.
Examples found in repository?
5fn main() {
6 // Create a cache with capacity 1000 and 5-minute TTL
7 let mut cache = FifoCache::new(
8 1000,
9 #[cfg(feature = "ttl")]
10 Duration::from_secs(300)
11 );
12
13 // Insert some values
14 cache.insert("user:123", "John Doe");
15 cache.insert("user:456", "Jane Smith");
16 cache.insert("config:timeout", "30");
17
18 // Retrieve values
19 if let Some(name) = cache.get(&"user:123") {
20 println!("Found user: {}", name);
21 }
22
23 // Cache will automatically evict oldest entries when full
24 // and expire entries after 5 minutes
25
26 println!("Cache size: {}/{}", cache.len(), cache.max_size());
27
28 #[cfg(feature = "ttl")]
29 // Manual cleanup of expired entries
30 cache.cleanup_expired();
31}Sourcepub fn set_max_size(&mut self, max_size: usize, prune: bool)
pub fn set_max_size(&mut self, max_size: usize, prune: bool)
Sets the maximum capacity of the cache.
§Arguments
max_size- The new maximum number of entries the cache can holdprune- Whether or not to immediately prune the excess number of entries, in case the update causes the cache to be above capacity
Sourcepub fn default_ttl(&self) -> Duration
pub fn default_ttl(&self) -> Duration
Returns the default TTL for cache entries.
Sourcepub fn set_default_ttl(&mut self, default_ttl: Duration)
pub fn set_default_ttl(&mut self, default_ttl: Duration)
Sets the default TTL for cache entries. Note that this will only affect entries that get inserted or updated after the change. Existing entries will keep their TTL until they expire.
§Arguments
default_ttl- The new default time-to-live for cache entries