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
PR Review: fix(cache): make in memory cache global
Summary
This PR converts two instance-level fields (InMemoryDriver.cache and CacheInner.in_flight) into process-global singletons backed by std::sync::OnceLock. It also adds an eviction listener for debug logging, a short-circuit in fetch_all_convert for empty cache key sets, and improves tracing instrumentation.
The motivation is sound: sharing one moka cache across all CacheInner instances ensures cache hits propagate regardless of which instance originally populated the cache. However there are two real issues that need addressing before merge.
Critical: max_capacity Is Silently Ignored After First Init
File:engine/packages/cache/src/driver.rs
OnceLock::get_or_init initialises the cache exactly once. Every subsequent call to InMemoryDriver::new(max_capacity) silently ignores the max_capacity argument. Looking at the callers:
If a test binary runs before production code bootstraps, the global cache is initialized with capacity 1,000 instead of 10,000. The max_capacity parameter is effectively meaningless after the first call, which is misleading and could mask capacity configuration bugs.
Recommendation: Either emit a tracing::warn! when a subsequent call provides a differing max_capacity value, or panic in debug builds on mismatch, or remove the parameter entirely and use a named constant or an environment variable.
Every test calls build_cache() which calls new_in_memory(1000, None). With the old design each call produced an independent, isolated cache. With the new design all tests in the same process share one CACHE and one IN_FLIGHT map. This means:
Values written by one test can be read by another; tests are no longer isolated.
IN_FLIGHT can retain stale broadcast::Sender entries left behind by a panicking test, potentially causing subsequent tests to hang.
Tests that assert a value is None become order-dependent and may spuriously fail if a parallel test inserted the same key.
The tests currently use distinct base_key strings per test, which provides some protection, but it is fragile. Any future test that reuses a key name will silently pick up cached data.
Recommendation: Either keep a per-instance cache in test builds via a #[cfg(test)] path, or add a comment in the test helper making the uniqueness requirement explicit, and consider adding a test-only clear() helper.
Minor: Stale In-Flight Entries After purge_local
When purge_local removes a key from the moka cache, the corresponding IN_FLIGHT entry is not removed and its broadcast::Sender is not signalled. This is pre-existing, but the move to a global IN_FLIGHT makes it more visible. Stale senders now persist for the lifetime of the process rather than being cleaned up when a CacheInner is dropped.
Minor: Panic Message in cache()
The message "should be initialized" is terse. Consider "CACHE accessed before InMemoryDriver::new was called" to aid debugging if it ever fires.
Positives
The short-circuit for empty succeeded_cache_keys is a clean, correct optimization.
Switching from skip(keys, getter, encoder, decoder) to skip_all, fields(?base_key) in the tracing annotation is a clear improvement.
scc::HashMap for IN_FLIGHT is the right choice per repo conventions (avoids Arc<Mutex<HashMap>>).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Please include a summary of the changes and the related issue. Please also include relevant motivation and context.
Type of change
How Has This Been Tested?
Please describe the tests that you ran to verify your changes.
Checklist: