-
-
Notifications
You must be signed in to change notification settings - Fork 90
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Provide a new iterator to iterate entries with their metadata such as last access time.
#311 (comment) by @peter-scholtens
Similar as mentioned in #235, I try to store and read-in my Cache content locally during a re-start of my server. However, this means the expiration state will be reset: the frequency is set to zero and the TTL/TTI maximized. Would it be possible to iterate with Expiration state? So this state information can be stored too.
We will do the followings:
- Add
iter_entries
method toCache
, which returns an iterator ofEntry<K, V>
. - Add
metadata
method toEntry<K, V>
, which returns a reference to a new struct&EntryMetadata
. EntryMetadata
will have the following information:last_modified
asstd::time::Instant
last_accessed
asstd::time::Instant
expiration_time
asOption<std::time::Instant>
- This value will be available only when the
Expiry
is set.
- This value will be available only when the
policy_weight
asu32
Example
// A new method of Cache that returns an iterator of Entry<K, V>.
for entry in cache.iter_entries() {
dbg!(entry.key()); // &K
dbg!(entry.value()); // &V
// A new method of Entry.
let md = entry.metadata(); // &EntryMetadata
dbg!(md.last_modified()); // std::time::Instant
dbg!(md.last_accessed()); // std::time::Instant
// This value will be available only when the Expiry is set.
dbg!(md.expiration_time()); // Option<std::time::Instant>
dbg!(md.policy_weight()); // u32
}
Note: If users want to save the entry metadata outside their application, they will need to convert std::time::Instant
to a wall-clock time such as time::OffsetDateTime
from time
crate. The followings will illustrate how to achieve it:
use std::time::Instant
use time::OffsetDateTime;
let md = entry.metadata();
let (now_instant, now_dt) = (Instant::now(), OffsetDateTime::now_utc());
let instant_to_dt = |instant: Instant| -> OffsetDateTime {
// The `instant` must be earlier than or equal to `now_instant`,
// otherwise this will panic.
let duration = now_instant - instant;
now_dt - time::Duration::seconds(duration.as_secs())
};
let last_modified_dt = instant_to_dt(md.last_modified()); // time::OffsetDateTime
peter-scholtens and qingxiang-jia
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request