Skip to content

Commit

Permalink
chore: Enable quick_cache only with a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Aug 31, 2023
1 parent 2ee11bb commit 972b0a5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion car-mirror-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ anyhow = "1.0"
async-std = { version = "1.11", features = ["attributes"] }
async-trait = "0.1"
bytes = "1.4.0"
car-mirror = { path = "../car-mirror", version = "0.1", features = ["test_utils"] }
car-mirror = { path = "../car-mirror", version = "0.1", features = ["test_utils", "quick_cache"] }
libipld = "0.16.0"
wnfs-common = "0.1.23"

Expand Down
3 changes: 2 additions & 1 deletion car-mirror/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ iroh-car = "0.3.0"
libipld = "0.16.0"
libipld-core = "0.16.0"
proptest = { version = "1.1", optional = true }
quick_cache = "0.4.0"
quick_cache = { version = "0.4.0", optional = true }
roaring-graphs = { version = "0.12", optional = true }
serde = "1.0.183"
serde_ipld_dagcbor = "0.4.0"
Expand All @@ -53,6 +53,7 @@ test-strategy = "0.3"
[features]
default = []
test_utils = ["proptest", "roaring-graphs"]
quick_cache = ["dep:quick_cache"]

[package.metadata.docs.rs]
all-features = true
Expand Down
7 changes: 5 additions & 2 deletions car-mirror/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use wnfs_common::BlockStore;
/// At the moment, all caches are conceptually memoization tables, so you don't
/// necessarily need to think about being careful about cache eviction.
///
/// See `InMemoryCache` for a `quick_cache`-based implementation, and
/// `NoCache` for disabling the cache.
/// See `InMemoryCache` for a `quick_cache`-based implementation
/// (enable the `quick-cache` feature), and `NoCache` for disabling the cache.
#[async_trait(?Send)]
pub trait Cache {
/// This returns further references from the block referenced by given CID,
Expand Down Expand Up @@ -51,11 +51,13 @@ pub trait Cache {
/// A [quick-cache]-based implementation of a car mirror cache.
///
/// [quick-cache]: https://github.com/arthurprs/quick-cache/
#[cfg(feature = "quick_cache")]
#[derive(Debug)]
pub struct InMemoryCache {
references: quick_cache::sync::Cache<Cid, Vec<Cid>>,
}

#[cfg(feature = "quick_cache")]
impl InMemoryCache {
/// Create a new in-memory cache that approximately holds
/// cached references for `approx_capacity` CIDs.
Expand All @@ -82,6 +84,7 @@ impl InMemoryCache {
}
}

#[cfg(feature = "quick_cache")]
#[async_trait(?Send)]
impl Cache for InMemoryCache {
async fn get_references_cached(&self, cid: Cid) -> Result<Option<Vec<Cid>>> {
Expand Down

0 comments on commit 972b0a5

Please sign in to comment.