Skip to content

Commit

Permalink
fix: don't scale in shards when capacity is not enough, just warn (#769)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx authored Oct 10, 2024
1 parent 192d5b8 commit 96581eb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 11 deletions.
11 changes: 2 additions & 9 deletions foyer-memory/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,13 @@ where
}

/// Build in-memory cache with the given configuration.
pub fn build(mut self) -> Cache<K, V, S> {
pub fn build(self) -> Cache<K, V, S> {
if self.capacity < self.shards {
tracing::warn!(
"The in-memory cache capacity({}) < shards({})",
"The in-memory cache capacity({}) < shards({}).",
self.capacity,
self.shards
);
self.shards = 1;
}

match self.eviction_config {
Expand Down Expand Up @@ -889,12 +888,6 @@ mod tests {
const OPS: usize = 10000;
const CONCURRENCY: usize = 8;

#[test]
fn test_not_enough_capacity() {
let cache: Cache<u64, u64> = CacheBuilder::new(4).with_shards(64).build();
assert_eq!(cache.shards(), 1);
}

fn fifo() -> Cache<u64, u64> {
CacheBuilder::new(CAPACITY)
.with_shards(SHARDS)
Expand Down
2 changes: 0 additions & 2 deletions foyer-memory/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,6 @@ where
S: HashBuilder,
{
pub fn new(config: GenericCacheConfig<K, V, E, S>) -> Self {
assert!(config.capacity >= config.shards);

let metrics = Arc::new(Metrics::new(&config.name));

let usages = (0..config.shards).map(|_| Arc::new(AtomicUsize::new(0))).collect_vec();
Expand Down

0 comments on commit 96581eb

Please sign in to comment.