Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a crate feature sync for enabling and disabling sync caches #143

Merged
merged 7 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
--env RUSTFLAGS='--cfg circleci' \
xd009642/tarpaulin \
cargo tarpaulin -v \
--features 'future, dash' \
--features 'sync, future, dash' \
--ciserver circle-ci \
--coveralls ${COVERALLS_TOKEN} \
--timeout 600 \
Expand Down
23 changes: 15 additions & 8 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,32 @@ jobs:
cargo update -p dashmap --precise 5.2.0
cargo update -p hashbrown --precise 0.11.2

- name: Build (no features)
- name: Run tests (debug, sync feature)
uses: actions-rs/cargo@v1
with:
command: build
command: test
args: --features sync

- name: Run tests (release, sync feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --release --features sync

- name: Run tests (release, no features)
- name: Run tests (future feature, but no sync feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --release
args: --no-default-features --features 'future, atomic64, quanta'

- name: Run tests (future feature)
- name: Run tests (future and sync features)
uses: actions-rs/cargo@v1
with:
command: test
args: --features future
args: --features 'future, sync'

- name: Run tests (dash feature)
- name: Run tests (dash feature, but no sync feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --features dash
args: --no-default-features --features 'dash, atomic64, quanta'
20 changes: 13 additions & 7 deletions .github/workflows/CIQuantaDisabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,31 @@ jobs:
cargo update -p dashmap --precise 5.2.0
cargo update -p hashbrown --precise 0.11.2

- name: Build (no quanta feature)
- name: Run tests (debug, but no quanta feature)
uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features --features atomic64
command: test
args: --no-default-features --features 'sync, atomic64'

- name: Run tests (release, no quanta feature)
- name: Run tests (release, but no quanta feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --release --no-default-features --features atomic64
args: --release --no-default-features --features 'sync, atomic64'

- name: Run tests (future feature, but no quanta feature)
- name: Run tests (future feature, but no quanta and sync features)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features 'future, atomic64'

- name: Run tests (dash feature, but no quanta feature)
- name: Run tests (future feature, but no quanta feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features 'sync, future, atomic64'

- name: Run tests (dash feature, but no quanta and sync features)
uses: actions-rs/cargo@v1
with:
command: test
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/LinuxCrossCompileTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ jobs:
with:
command: clean

- name: Run tests (no features)
- name: Run tests (sync feature)
uses: actions-rs/cargo@v1
with:
use-cross: true
command: test
args: --release --target ${{ matrix.platform.target }} ${{ matrix.platform.cargo-opts }}
args: --release --features sync --target ${{ matrix.platform.target }} ${{ matrix.platform.cargo-opts }}

- name: Run tests (future feature)
uses: actions-rs/cargo@v1
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/Skeptic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --release --features 'future, dash'
args: --release --features 'sync, future, dash'
env:
RUSTFLAGS: '--cfg skeptic'

- name: Run tests (future and dash, without atomic64)
- name: Run tests (sync, future and dash, without atomic64 and quanta)
uses: actions-rs/cargo@v1
with:
command: test
args: --release --no-default-features --features 'future, dash'
args: --release --no-default-features --features 'sync, future, dash'
env:
RUSTFLAGS: '--cfg skeptic'

- name: Run compile error tests (future and dash features, trybuild)
- name: Run compile error tests (sync, future and dash features, trybuild)
uses: actions-rs/cargo@v1
if: ${{ matrix.rust == 'stable' }}
with:
command: test
args: ui_trybuild --release --features 'future, dash'
args: ui_trybuild --release --features 'sync, future, dash'
env:
RUSTFLAGS: '--cfg trybuild'
18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ exclude = [".circleci", ".devcontainer", ".github", ".gitpod.yml", ".vscode"]
build = "build.rs"

[features]
default = ["atomic64", "quanta"]
default = ["sync", "atomic64", "quanta"]

# This feature is enabled by default. Disable it when you do not need
# `moka::sync::{Cache, SegmentedCache}`
sync = ["crossbeam-epoch", "thiserror", "uuid"]

# Enable this feature to use `moka::future::Cache`.
future = ["async-io", "async-lock", "futures-util"]
future = ["crossbeam-epoch", "thiserror", "uuid", "async-io", "async-lock", "futures-util"]

# Enable this feature to use **experimental** `moka::dash::Cache`.
# Please note that the APIs for this feature will be frequently changed in next
Expand All @@ -39,23 +43,23 @@ unstable-debug-counters = ["future"]

[dependencies]
crossbeam-channel = "0.5.4"
crossbeam-epoch = "0.8.2"
crossbeam-utils = "0.8"
num_cpus = "1.13"
once_cell = "1.7"
parking_lot = "0.12"
scheduled-thread-pool = "0.2.5"
scheduled-thread-pool = "0.2.6"
smallvec = "1.8"
tagptr = "0.2"
thiserror = "1.0"
uuid = { version = "0.8", features = ["v4"] }

# Opt-out serde and stable_deref_trait features
# https://github.com/Manishearth/triomphe/pull/5
triomphe = { version = "0.1", default-features = false }

# Optional dependencies (quanta, enabled by default)
# Optional dependencies (enabled by default)
crossbeam-epoch = { version = "0.8.2", optional = true }
quanta = { version = "0.10.0", optional = true }
thiserror = { version = "1.0", optional = true }
uuid = { version = "0.8", features = ["v4"], optional = true }

# Optional dependencies (dashmap)
dashmap = { version = "5.2", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::convert::TryInto;

// #[cfg(any(feature = "sync", feature = "future", feature = "dash"))]
#[cfg(any(feature = "sync", feature = "future", feature = "dash"))]
pub(crate) mod concurrent;

// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
pub(crate) mod error;

pub(crate) mod builder_utils;
Expand Down
2 changes: 1 addition & 1 deletion src/common/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<K> KeyDate<K> {
&self.key
}

// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
pub(crate) fn last_modified(&self) -> Option<Instant> {
self.entry_info.last_modified()
}
Expand Down
8 changes: 4 additions & 4 deletions src/common/concurrent/thread_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use std::{collections::HashMap, sync::Arc};

static REGISTRY: Lazy<ThreadPoolRegistry> = Lazy::new(ThreadPoolRegistry::default);

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
// #[cfg_attr(any(feature = "sync", feature = "future"), derive(Debug))]
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
#[cfg_attr(any(feature = "sync", feature = "future"), derive(Debug))]
pub(crate) enum PoolName {
Housekeeper,
// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
Invalidator,
}

impl PoolName {
fn thread_name_template(&self) -> &'static str {
match self {
PoolName::Housekeeper => "moka-housekeeper-{}",
// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
PoolName::Invalidator => "moka-invalidator-{}",
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<T> Deque<T> {
self.region
}

// #[cfg(any(test, feature = "sync", feature = "future"))]
#[cfg(any(test, feature = "sync", feature = "future"))]
pub(crate) fn len(&self) -> usize {
self.len
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) mod clock;
pub(crate) use clock::Clock;

#[cfg(test)]
// #[cfg(all(test, feature = "sync"))]
#[cfg(all(test, feature = "sync"))]
pub(crate) use clock::Mock;

/// a wrapper type over Instant to force checked additions and prevent
Expand Down
3 changes: 1 addition & 2 deletions src/common/time/clock_quanta.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub(crate) type Clock = quanta::Clock;
pub(crate) type Instant = quanta::Instant;

#[cfg(test)]
// #[cfg(all(test, feature = "sync"))]
#[cfg(all(test, feature = "sync"))]
pub(crate) type Mock = quanta::Mock;
30 changes: 30 additions & 0 deletions src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//!
//! To use this module, enable a crate feature called "future".

use std::{hash::Hash, sync::Arc};

mod builder;
mod cache;
mod value_initializer;
Expand All @@ -12,6 +14,34 @@ pub use {
cache::{BlockingOp, Cache},
};

/// The type of the unique ID to identify a predicate used by
/// [`Cache#invalidate_entries_if`][invalidate-if] method.
///
/// A `PredicateId` is a `String` of UUID (version 4).
///
/// [invalidate-if]: ./struct.Cache.html#method.invalidate_entries_if
pub type PredicateId = String;

pub struct Iter<'i, K, V>(crate::sync_base::iter::Iter<'i, K, V>);

impl<'i, K, V> Iter<'i, K, V> {
pub(crate) fn new(inner: crate::sync_base::iter::Iter<'i, K, V>) -> Self {
Self(inner)
}
}

impl<'i, K, V> Iterator for Iter<'i, K, V>
where
K: Eq + Hash + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
{
type Item = (Arc<K>, V);

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

/// Provides extra methods that will be useful for testing.
pub trait ConcurrentCacheExt<K, V> {
/// Performs any pending maintenance operations needed by the cache.
Expand Down
8 changes: 4 additions & 4 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::{
value_initializer::{InitResult, ValueInitializer},
CacheBuilder, ConcurrentCacheExt,
CacheBuilder, ConcurrentCacheExt, Iter, PredicateId,
};
use crate::{
common::concurrent::{
constants::{MAX_SYNC_REPEATS, WRITE_RETRY_INTERVAL_MICROS},
housekeeper::InnerSync,
Weigher, WriteOp,
},
sync::{Iter, PredicateId},
sync_base::base_cache::{BaseCache, HouseKeeperArc},
Policy, PredicateError,
};
Expand Down Expand Up @@ -881,9 +880,10 @@ where
/// ```
///
pub fn iter(&self) -> Iter<'_, K, V> {
use crate::sync_base::iter::ScanningGet;
use crate::sync_base::iter::{Iter as InnerIter, ScanningGet};

Iter::with_single_cache_segment(&self.base, self.base.num_cht_segments())
let inner = InnerIter::with_single_cache_segment(&self.base, self.base.num_cht_segments());
Iter::new(inner)
}

/// Returns a `BlockingOp` for this cache. It provides blocking
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub(crate) mod common;
pub(crate) mod policy;
pub mod unsync;

// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
pub(crate) mod cht;

#[cfg(feature = "dash")]
Expand All @@ -173,13 +173,14 @@ pub mod dash;
#[cfg_attr(docsrs, doc(cfg(feature = "future")))]
pub mod future;

// #[cfg(feature = "sync")]
#[cfg(feature = "sync")]
#[cfg_attr(docsrs, doc(cfg(feature = "sync")))]
pub mod sync;

// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
pub(crate) mod sync_base;

// #[cfg(any(feature = "sync", feature = "future"))]
#[cfg(any(feature = "sync", feature = "future"))]
pub use common::error::PredicateError;

pub use policy::Policy;
Expand Down
4 changes: 2 additions & 2 deletions src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Policy {
self.max_capacity
}

// #[cfg(feature = "sync")]
#[cfg(feature = "sync")]
pub(crate) fn set_max_capacity(&mut self, capacity: Option<u64>) {
self.max_capacity = capacity;
}
Expand All @@ -39,7 +39,7 @@ impl Policy {
self.num_segments
}

// #[cfg(feature = "sync")]
#[cfg(feature = "sync")]
pub(crate) fn set_num_segments(&mut self, num: usize) {
self.num_segments = num;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sync_base/base_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
invalidator::{GetOrRemoveEntry, InvalidationResult, Invalidator, KeyDateLite, PredicateFun},
iter::ScanningGet,
PredicateId,
};

use crate::{
Expand All @@ -23,7 +24,6 @@ use crate::{
time::{CheckedTimeOps, Clock, Instant},
CacheRegion,
},
sync::PredicateId,
Policy, PredicateError,
};

Expand Down
2 changes: 1 addition & 1 deletion src/sync_base/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'i, K, V> Iter<'i, K, V> {
}
}

// #[cfg(feature = "sync")]
#[cfg(feature = "sync")]
pub(crate) fn with_multiple_cache_segments(
cache_segments: Box<[&'i dyn ScanningGet<K, V>]>,
num_cht_segments: usize,
Expand Down