Skip to content

Commit

Permalink
Remove the thread pool from future::Cache
Browse files Browse the repository at this point in the history
Update the doc.
  • Loading branch information
tatsuya6502 committed Aug 20, 2023
1 parent 6e7e912 commit 16b4f89
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
23 changes: 19 additions & 4 deletions src/future/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,16 @@ impl<K, V, C> CacheBuilder<K, V, C> {
///
/// See [this example][example] for a usage of eviction listener.
///
/// If you want to use an asynchronous listener, use
/// [`async_eviction_listener`](#method.async_eviction_listener) instead.
/// # Sync or Async Eviction Listener
///
/// The closure can be either synchronous or asynchronous, and `CacheBuilder`
/// provides two methods for setting the eviction listener closure:
///
/// - If you do not need to `.await` anything in the eviction listener, use this
/// `eviction_listener` method.
/// - If you need to `.await` something in the eviction listener, use
/// [`async_eviction_listener`](#method.async_eviction_listener) method
/// instead.
///
/// # Panics
///
Expand Down Expand Up @@ -297,8 +305,15 @@ impl<K, V, C> CacheBuilder<K, V, C> {
///
/// See [this example][example] for a usage of asynchronous eviction listener.
///
/// If you want to use a synchronous listener, use
/// [`eviction_listener`](#method.eviction_listener) instead.
/// # Sync or Async Eviction Listener
///
/// The closure can be either synchronous or asynchronous, and `CacheBuilder`
/// provides two methods for setting the eviction listener closure:
///
/// - If you do not need to `.await` anything in the eviction listener, use
/// [`eviction_listener`](#method.eviction_listener) method instead.
/// - If you need to `.await` something in the eviction listener, use
/// this method.
///
/// # Panics
///
Expand Down
40 changes: 13 additions & 27 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,9 @@ use std::{
///
/// # Example: `insert`, `get` and `invalidate`
///
/// Cache entries are manually added using an insert method, and are stored in the
/// cache until either evicted or manually invalidated:
///
/// - Inside an async context (`async fn` or `async` block), use
/// [`insert`](#method.insert), [`get_with`](#method.get_with) or
/// [`invalidate`](#method.invalidate) methods for updating the cache and `await`
/// them.
/// - Outside any async context, use [`blocking`](#method.blocking) method to access
/// blocking version of [`insert`](./struct.BlockingOp.html#method.insert) or
/// [`invalidate`](struct.BlockingOp.html#method.invalidate) methods.
/// Cache entries are manually added using [`insert`](#method.insert) of
/// [`get_with`](#method.get_with) method, and are stored in the cache until either
/// evicted or manually invalidated:
///
/// Here's an example of reading and updating a cache by using multiple asynchronous
/// tasks with [Tokio][tokio-crate] runtime:
Expand Down Expand Up @@ -107,15 +100,13 @@ use std::{
/// tokio::spawn(async move {
/// // Insert 64 entries. (NUM_KEYS_PER_TASK = 64)
/// for key in start..end {
/// // insert() is an async method, so await it.
/// my_cache.insert(key, value(key)).await;
/// // get() returns Option<String>, a clone of the stored value.
/// assert_eq!(my_cache.get(&key).await, Some(value(key)));
/// }
///
/// // Invalidate every 4 element of the inserted entries.
/// for key in (start..end).step_by(4) {
/// // invalidate() is an async method, so await it.
/// my_cache.invalidate(&key).await;
/// }
/// })
Expand Down Expand Up @@ -638,18 +629,12 @@ use std::{
/// ## Delivery Modes for eviction listener
///
/// The [`DeliveryMode`][delivery-mode] specifies how and when an eviction
/// notification should be delivered to an eviction listener. Currently, the
/// `future::Cache` supports only one delivery mode: `Queued` mode.
/// notification should be delivered to an eviction listener.
///
/// A future version of `future::Cache` will support `Immediate` mode, which will be
/// easier to use in many use cases than queued mode. Unlike the `future::Cache`,
/// the `sync::Cache` already supports it.
/// The `future::Cache` supports the following delivery mode:
///
/// Once `future::Cache` supports the immediate mode, the `eviction_listener` and
/// `eviction_listener_with_conf` methods will be added to the
/// `future::CacheBuilder`. The former will use the immediate mode, and the latter
/// will take a custom configurations to specify the queued mode. The current method
/// `eviction_listener` will be deprecated.
/// - From v0.12.0, it only supports `Immediate` mode.
/// - Up to v0.11.x, it only supported `Queued` modes.
///
/// For more details about the delivery modes, see [this section][sync-delivery-modes]
/// of `sync::Cache` documentation.
Expand Down Expand Up @@ -730,8 +715,8 @@ impl<K, V, S> Cache<K, V, S> {
///
/// The value returned is _an estimate_; the actual count may differ if there are
/// concurrent insertions or removals, or if some entries are pending removal due
/// to expiration. This inaccuracy can be mitigated by performing a `sync()`
/// first.
/// to expiration. This inaccuracy can be mitigated by calling
/// `run_pending_tasks` first.
///
/// # Example
///
Expand Down Expand Up @@ -775,8 +760,9 @@ impl<K, V, S> Cache<K, V, S> {
///
/// The value returned is _an estimate_; the actual size may differ if there are
/// concurrent insertions or removals, or if some entries are pending removal due
/// to expiration. This inaccuracy can be mitigated by performing a `sync()`
/// first. See [`entry_count`](#method.entry_count) for a sample code.
/// to expiration. This inaccuracy can be mitigated by calling
/// `run_pending_tasks` first. See [`entry_count`](#method.entry_count) for a
/// sample code.
pub fn weighted_size(&self) -> u64 {
self.base.weighted_size()
}
Expand Down Expand Up @@ -2345,7 +2331,7 @@ mod tests {

// This test is for https://github.com/moka-rs/moka/issues/155
#[tokio::test]
async fn invalidate_all_without_sync() {
async fn invalidate_all_without_running_pending_tasks() {
let cache = Cache::new(1024);

assert_eq!(cache.get(&0).await, None);
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
//! - The numbers of read/write recordings reach to the configured amounts.
//! - Or, the certain time past from the last draining.
//!
//! **TODO** We do not use the worker threads anymore.
//! **TODO (v0.12.0 release)**: Update the following section as we do not use the
//! worker threads anymore.
//!
//! In a `Cache`, this draining and batch application is handled by a single worker
//! thread. So under heavy concurrent operations from clients, draining may not be
Expand Down

0 comments on commit 16b4f89

Please sign in to comment.