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

Cache with max capacity of zero should not cache anything #230

Closed
tatsuya6502 opened this issue Feb 16, 2023 · 0 comments · Fixed by #238
Closed

Cache with max capacity of zero should not cache anything #230

tatsuya6502 opened this issue Feb 16, 2023 · 0 comments · Fixed by #238
Assignees
Labels
bug Something isn't working
Milestone

Comments

@tatsuya6502
Copy link
Member

tatsuya6502 commented Feb 16, 2023

Description

All Moka versions (up to v0.10.0) are affected by this issue.

When a cache is configured with the max capacity of zero, users would expect that it should not cache anything. However, the current cache implementations are to cache everything for a short period of time (up to 0.5 seconds). This is because these implementations temporary admit new entries no matter if the cache is full or not, and evict the entries later in batch.

This behavior is not intuitive, so it needs to be changed to not caching anything.

// Cargo.toml
// [dependencies]
// moka = { version = "0.10.0", features = ["future"] }
// tokio = { version = "1.25.0", features = ["rt-multi-thread", "time"] }

fn main() {
    sync_cache();

    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(future_cache());
}

fn sync_cache() {
    let cache = moka::sync::Cache::new(0);
    cache.insert(0, ());

    dbg!(cache.contains_key(&0));
    std::thread::sleep(std::time::Duration::from_millis(550));
    dbg!(cache.contains_key(&0));
}

async fn future_cache() {
    let cache = moka::future::Cache::new(0);
    cache.insert(1, ()).await;

    dbg!(cache.contains_key(&1));
    tokio::time::sleep(std::time::Duration::from_millis(550)).await;
    dbg!(cache.contains_key(&1));
}
$ cargo run
[src/main.rs:12] cache.contains_key(&0) = true
[src/main.rs:14] cache.contains_key(&0) = false
[src/main.rs:21] cache.contains_key(&1) = true
[src/main.rs:23] cache.contains_key(&1) = false

Workaround

Users can workaround this issue by setting time_to_live to ZERO. This will still cache new entries for a short period of time, but get* methods will never return the entries as they are already expired.

    let cache = moka::sync::Cache::builder()
        .max_capacity(0)
        .time_to_live(std::time::Duration::ZERO)
        .build();

CC: @Swatinem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant