You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"] }fnmain(){sync_cache();let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(future_cache());}fnsync_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));}asyncfnfuture_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));}
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();
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.
Workaround
Users can workaround this issue by setting
time_to_live
toZERO
. This will still cache new entries for a short period of time, butget*
methods will never return the entries as they are already expired.CC: @Swatinem
The text was updated successfully, but these errors were encountered: