Skip to content

Commit 600cb23

Browse files
committed
feat(evictors): Add Evictor trait and basic eviction algorithms
1 parent 556c467 commit 600cb23

File tree

5 files changed

+442
-0
lines changed

5 files changed

+442
-0
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bson = {version = "1.2.2", features = ["u2i"]}
2121
serde = {version = "1.0.125", features = ["derive"]}
2222
tokio = {version = "1.5.0", features = ["fs", "io-util"]}
2323
bytes = "1.0.1"
24+
async-trait = "0.1.50"
2425

2526
[dev-dependencies]
2627
tokio = {version = "1.5.0", features = ["full"]}

src/cache.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{ForcepError, MetaDb, Metadata, Result};
22
use bytes::Bytes;
33
use std::io;
44
use std::path;
5+
use std::result;
56
use tokio::fs as afs;
67

78
/// Creates a writeable and persistent temporary file in the path provided, returning the path and
@@ -337,6 +338,21 @@ impl Cache {
337338
pub fn metadata_iter(&self) -> impl Iterator<Item = Result<(Vec<u8>, Metadata)>> {
338339
self.meta.metadata_iter()
339340
}
341+
342+
/// Runs the specified eviction algorithm over this instance cache instance.
343+
///
344+
/// Eviction algorithms will remove items out of the cache until certain a condition has been
345+
/// met, usually a size requirement. See the [`evictors`] module for more information and
346+
/// examples.
347+
///
348+
/// [`evictors`]: crate::evictors
349+
#[inline]
350+
pub async fn evict_with<E>(&self, evictor: E) -> result::Result<(), E::Err>
351+
where
352+
E: crate::evictors::Evictor,
353+
{
354+
evictor.evict(self).await
355+
}
340356
}
341357

342358
#[cfg(test)]

0 commit comments

Comments
 (0)