Open tatsuya6502 opened 1 year ago
cache_loader.insert( my_entry.key, my_entry.value, None, // policy_weight, Some(dt_to_duration(my_entry.last_modified))), dt_to_duration(my_entry.last_accessed), None // expiration_time );
Rather than making CacheLoader::insert
method to directly take every metadata values like policy_weight
, make it to take an EntryMetadata
. Also, provide an easy way to build an EntryMetadata
, e.g. using a builder, parsing JSON, or deserialize from a binary (Vec<u8>
).
use moka::EntryMetadata;
// Builder
let metadata = EntryMetadata::builder()
// This takes std::time::Instant. Maybe provide an alternative method to take
// `time::OffsetDateTime` for convenience?
.last_modified(dt_to_instant(my_entry.last_modified))
.last_accessed(dt_to_instant(my_entry.last_accessed))
.build();
// Parse JSON. (Will require `serde`, `serde_json` and `time` crates)
let metadata = EntryMetadata::from_json(serde_json::json! {
"lastModified", "2023-12-23T09:55:06.000+08:00",
...
});
- When
finish
is called, it will do the followings:
- ...
- Now the cache state has been restored. Invoke
run_pending_tasks
(moka v0.12.x) several times to evict expired entries, and if the max capacity is exceeded, evict idle entries.
- If the eviction listener is set, it will be notified for evictions.
- Finally, return the
Cache
.
I will remove the step to invoke run_pending_tasks
. It does not seem right to call the eviction listener before giving the Cache
to the user code.
CC: @peter-scholtens
When #312 and #313 are implemented, we will have enough data to restore a cache to a previous state. Provide a way to restore cache from them.
Inputs:
CacheBuilder
with configurations such as:Output:
Cache
initialized with the given inputs.Example
How it will work
CacheBuilder
has following methods that returns aCacheLoader
:loader(self)
loader_with_hasher(self, BuildHasher)
loader_with_frequency_sketch(self, FrequencySketch, BuildHasher)
FrequencySketch
with theBuildHasher
.CacheLoader
has aCache
but it is yet private.insert
is called on theCacheLoader
, it will do the followings:Cache
.EntryInfo
from the given metadata.(Arc<K>, last_accessed)
to aVec
.(Arc<K>, last_modified)
to anotherVec
.finish
is called, it will do the followings:Vec
s bylast_accessed
andlast_modified
respectively.Vec
bylast_accessed
.Vec
bylast_modified
.run_pending_tasks
(moka v0.12.x) several times to evict expired entries, and if the max capacity is exceeded, evict idle entries.Cache
.