timed-map
Lightweight map implementation that supports expiring entries and fully
compatible with both std and no_std environments.
TimedMap allows storing key-value pairs with optional expiration times. Expiration is
handled by an implementation of the Clock trait, which abstracts time handling for
no_std environments.
When std feature is enabled (which is the default case), Clock trait is handled
automatically from the crate internals with std::time::SystemTime.
Basic Usage:
In std environments:
use ;
use Duration;
let mut map: = new;
map.insert_expirable;
assert_eq!;
assert!;
map.insert_constant;
assert_eq!;
assert!;
In no_std environments:
use Duration;
use ;
;
let clock = CustomClock;
let mut map = new;
map.insert_expirable;
assert_eq!;
assert!;
map.insert_constant;
assert_eq!;
assert!;
Advanced Usage & Tuning:
Customizing the Internal Map
By default, TimedMap uses BTreeMap to store data, but you can switch to FxHashMap or HashMap.
This is only available on std environments.
use ;
let mut map: = new_with_map_kind;
Manual Expiration Control
To have fully control over expired entries, use the *_unchecked functions and drop_expired_entries to handle expiration manually.
This can boost performance by running expiration logic only when it's necessary to maximize the performance.
let mut map: = new;
map.insert_expirable_unchecked;
assert_eq!;
map.insert_constant_unchecked;
assert_eq!;
map.drop_expired_entries;
Setting Expiration Check Frequency
In cases where inserts are frequent, expiration_tick_cap can be set to control how often expired entries are removed. For instance,
if there are 100 inserts per second, setting expiration_tick_cap to 100 will trigger the expiration check every 100 inserts which will
reduce the expiration logic overhead significantly.
use ;
let mut map: = new.expiration_tick_cap;