com.github.benmanes.caffeine.cache.Policy.Eviction Java Examples

The following examples show how to use com.github.benmanes.caffeine.cache.Policy.Eviction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
    maximumSize = { Maximum.ZERO, Maximum.ONE, Maximum.FULL },
    weigher = {CacheWeigher.DEFAULT, CacheWeigher.TEN})
public void evict(Cache<Integer, Integer> cache, CacheContext context,
    Eviction<Integer, Integer> eviction) {
  cache.putAll(context.absent());
  if (eviction.isWeighted()) {
    assertThat(eviction.weightedSize().getAsLong(), is(context.maximumWeight()));
  } else {
    assertThat(cache.estimatedSize(), is(context.maximumSize()));
  }
  int count = context.absentKeys().size();
  assertThat(context, hasEvictionCount(count));
  assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.SIZE));

  verifyWriter(context, (verifier, writer) -> {
    Map<Integer, Integer> all = new HashMap<>(context.original());
    all.putAll(context.absent());
    MapDifference<Integer, Integer> diff = Maps.difference(all, cache.asMap());
    verifier.deletedAll(diff.entriesOnlyOnLeft(), RemovalCause.SIZE);
  });
}
 
Example #2
Source File: EvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    removalListener = { Listener.DEFAULT, Listener.CONSUMING })
public void maximumSize_decrease(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  long newSize = context.maximumWeightOrSize() / 2;
  eviction.setMaximum(newSize);
  assertThat(eviction.getMaximum(), is(newSize));
  if (context.initialSize() > newSize) {
    if (context.isZeroWeighted()) {
      assertThat(cache.estimatedSize(), is(context.initialSize()));
      assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.SIZE));
    } else {
      assertThat(cache.estimatedSize(), is(newSize));
      assertThat(cache, hasRemovalNotifications(context, newSize, RemovalCause.SIZE));
    }
  }
}
 
Example #3
Source File: EvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void put_changeWeight(Cache<String, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  @SuppressWarnings({"unchecked", "rawtypes"})
  CacheWriter<String, List<Integer>> writer = (CacheWriter) context.cacheWriter();

  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  cache.put("a", asList(-1, -2, -3, -4));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(5L));

  verifyWriter(context, (verifier, ignored) -> {
    verify(writer).write("a", asList(1, 2, 3));
    verify(writer).write("b", asList(1));
    verify(writer).write("a", asList(-1, -2, -3, -4));
  });
}
 
Example #4
Source File: EvictionTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
@SuppressWarnings("FutureReturnValueIgnored")
public void put_asyncWeight(AsyncLoadingCache<Integer, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  AtomicBoolean ready = new AtomicBoolean();
  AtomicBoolean done = new AtomicBoolean();
  CompletableFuture<List<Integer>> valueFuture = CompletableFuture.supplyAsync(() -> {
    Awaits.await().untilTrue(ready);
    return ImmutableList.of(1, 2, 3, 4, 5);
  });
  valueFuture.whenComplete((r, e) -> done.set(true));

  cache.put(context.absentKey(), valueFuture);
  assertThat(eviction.weightedSize().getAsLong(), is(0L));
  assertThat(cache.synchronous().estimatedSize(), is(1L));

  ready.set(true);
  Awaits.await().untilTrue(done);
  Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(5L));
  Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(1L));
}
 
Example #5
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void invalidateAll(Cache<String, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  cache.invalidateAll();
  assertThat(cache.estimatedSize(), is(0L));
  assertThat(eviction.weightedSize().getAsLong(), is(0L));
}
 
Example #6
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
    maximumSize = Maximum.UNREACHABLE, weigher = CacheWeigher.VALUE)
public void weightOf(Cache<Integer, Integer> cache, CacheContext context,
    Eviction<Integer, Integer> eviction) {
  Integer key = 1;
  cache.put(key, 1);
  assertThat(eviction.weightOf(key).getAsInt(), is(1));

  cache.put(key, 2);
  assertThat(eviction.weightOf(key).getAsInt(), is(2));
}
 
Example #7
Source File: CaffeineCache.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void warm(SolrIndexSearcher searcher, SolrCache<K,V> old) {
  if (regenerator == null) {
    return;
  }
  
  long warmingStartTime = System.nanoTime();
  Map<K, V> hottest = Collections.emptyMap();
  CaffeineCache<K,V> other = (CaffeineCache<K,V>)old;

  // warm entries
  if (isAutowarmingOn()) {
    Eviction<K, V> policy = other.cache.policy().eviction().get();
    int size = autowarm.getWarmCount(other.cache.asMap().size());
    hottest = policy.hottest(size);
  }

  for (Entry<K, V> entry : hottest.entrySet()) {
    try {
      boolean continueRegen = regenerator.regenerateItem(
          searcher, this, old, entry.getKey(), entry.getValue());
      if (!continueRegen) {
        break;
      }
    }
    catch (Exception e) {
      SolrException.log(log, "Error during auto-warming of key:" + entry.getKey(), e);
    }
  }

  inserts.reset();
  priorStats = other.cache.stats().plus(other.priorStats);
  priorInserts = other.inserts.sum() + other.priorInserts;
  warmupTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - warmingStartTime, TimeUnit.NANOSECONDS);
}
 
Example #8
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine,
    maximumSize = Maximum.FULL, weigher = { CacheWeigher.DEFAULT, CacheWeigher.TEN },
    removalListener = { Listener.DEFAULT, Listener.CONSUMING })
public void maximumSize_decrease_min(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  eviction.setMaximum(0);
  assertThat(eviction.getMaximum(), is(0L));
  if (context.initialSize() > 0) {
    long expect = context.isZeroWeighted() ? context.initialSize() : 0;
    assertThat(cache.estimatedSize(), is(expect));
  }
  assertThat(cache, hasRemovalNotifications(context, context.initialSize(), RemovalCause.SIZE));
}
 
Example #9
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    removalListener = { Listener.DEFAULT, Listener.CONSUMING })
@Test(dataProvider = "caches", expectedExceptions = IllegalArgumentException.class)
public void maximumSize_decrease_negative(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  try {
    eviction.setMaximum(-1);
  } finally {
    assertThat(eviction.getMaximum(), is(context.maximumWeightOrSize()));
    assertThat(cache, hasRemovalNotifications(context, 0, RemovalCause.SIZE));
  }
}
 
Example #10
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void maximumSize_increase(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  eviction.setMaximum(2 * context.maximumWeightOrSize());
  assertThat(cache.estimatedSize(), is(context.initialSize()));
  assertThat(eviction.getMaximum(), is(2 * context.maximumWeightOrSize()));
}
 
Example #11
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine,
    maximumSize = Maximum.FULL, removalListener = Listener.REJECTING)
public void maximumSize_increase_max(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  eviction.setMaximum(Long.MAX_VALUE);
  assertThat(cache.estimatedSize(), is(context.initialSize()));
  assertThat(eviction.getMaximum(), is(Long.MAX_VALUE - Integer.MAX_VALUE)); // impl detail
}
 
Example #12
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
    initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL)
public void coldest_partial(CacheContext context, Eviction<Integer, Integer> eviction) {
  int count = (int) context.initialSize() / 2;
  assertThat(eviction.coldest(count).size(), is(count));
}
 
Example #13
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
    initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL,
    weigher = { CacheWeigher.DEFAULT, CacheWeigher.TEN },
    removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void coldest_order(CacheContext context, Eviction<Integer, Integer> eviction) {
  Set<Integer> keys = new LinkedHashSet<>(context.original().keySet());
  Set<Integer> coldest = new LinkedHashSet<>(eviction.coldest(Integer.MAX_VALUE).keySet());

  // Ignore the last key; hard to predict with W-TinyLFU
  keys.remove(context.lastKey());
  coldest.remove(context.lastKey());

  assertThat(coldest, contains(keys.toArray()));
}
 
Example #14
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, initialCapacity = InitialCapacity.EXCESSIVE,
    maximumSize = Maximum.FULL)
public void coldest_snapshot(Cache<Integer, Integer> cache, CacheContext context,
    Eviction<Integer, Integer> eviction) {
  Map<Integer, Integer> coldest = eviction.coldest(Integer.MAX_VALUE);
  cache.invalidateAll();
  assertThat(coldest, is(equalTo(context.original())));
}
 
Example #15
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
    initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL)
public void hottest_partial(CacheContext context, Eviction<Integer, Integer> eviction) {
  int count = (int) context.initialSize() / 2;
  assertThat(eviction.hottest(count).size(), is(count));
}
 
Example #16
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
    initialCapacity = InitialCapacity.EXCESSIVE, maximumSize = Maximum.FULL,
    removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void hottest_order(CacheContext context, Eviction<Integer, Integer> eviction) {
  Set<Integer> keys = new LinkedHashSet<>(context.original().keySet());
  Set<Integer> hottest = eviction.hottest(Integer.MAX_VALUE).keySet();
  Set<Integer> coldest = new LinkedHashSet<>(ImmutableList.copyOf(hottest).reverse());

  // Ignore the last key; hard to predict with W-TinyLFU
  keys.remove(context.lastKey());
  coldest.remove(context.lastKey());

  assertThat(coldest, contains(keys.toArray()));
}
 
Example #17
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, initialCapacity = InitialCapacity.EXCESSIVE,
    maximumSize = Maximum.FULL)
public void hottest_snapshot(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  Map<Integer, Integer> hottest = eviction.hottest(Integer.MAX_VALUE);
  cache.invalidateAll();
  assertThat(hottest, is(equalTo(context.original())));
}
 
Example #18
Source File: BoundedLocalCacheTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine,
    population = Population.EMPTY, maximumSize = Maximum.FULL)
public void drain_blocksOrderedMap(Cache<Integer, Integer> cache,
    CacheContext context, Eviction<Integer, Integer> eviction) {
  BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
  checkDrainBlocks(localCache, () -> eviction.coldest(((int) context.maximumSize())));
}
 
Example #19
Source File: CacheValidationListener.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Free memory by clearing unused resources after test execution. */
private void cleanUp(ITestResult testResult) {
  boolean briefParams = !detailedParams.get();

  if (testResult.isSuccess() && briefParams) {
    testResult.setParameters(EMPTY_PARAMS);
    testResult.setThrowable(null);
  }

  Object[] params = testResult.getParameters();
  for (int i = 0; i < params.length; i++) {
    Object param = params[i];
    if ((param instanceof AsyncCache<?, ?>) || (param instanceof Cache<?, ?>)
        || (param instanceof Map<?, ?>) || (param instanceof Eviction<?, ?>)
        || (param instanceof Expiration<?, ?>) || (param instanceof VarExpiration<?, ?>)
        || ((param instanceof CacheContext) && briefParams)) {
      params[i] = simpleNames.get(param.getClass(), key -> ((Class<?>) key).getSimpleName());
    } else if (param instanceof CacheContext) {
      params[i] = simpleNames.get(param.toString(), Object::toString);
    } else {
      params[i] = Objects.toString(param);
    }
  }

  /*
  // Enable in TestNG 7.0
  if ((testResult.getName() != null) && briefParams) {
    testResult.setTestName(simpleNames.get(testResult.getName(), Object::toString));
  }
  */

  CacheSpec.interner.get().clear();
}
 
Example #20
Source File: CaffeineTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void maximumWeight_small() {
  Caffeine<?, ?> builder = Caffeine.newBuilder()
      .maximumWeight(0).weigher(Weigher.singletonWeigher());
  assertThat(builder.weigher, is(Weigher.singletonWeigher()));
  assertThat(builder.maximumWeight, is(0L));
  Eviction<?, ?> eviction = builder.build().policy().eviction().get();
  assertThat(eviction.getMaximum(), is(0L));
  assertThat(eviction.isWeighted(), is(true));
}
 
Example #21
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.ZERO,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
@SuppressWarnings("FutureReturnValueIgnored")
public void evict_zero_async(AsyncLoadingCache<Integer, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  AtomicBoolean ready = new AtomicBoolean();
  AtomicBoolean done = new AtomicBoolean();
  CompletableFuture<List<Integer>> valueFuture = CompletableFuture.supplyAsync(() -> {
    Awaits.await().untilTrue(ready);
    return ImmutableList.of(1, 2, 3, 4, 5);
  });
  valueFuture.whenComplete((r, e) -> done.set(true));

  cache.put(context.absentKey(), valueFuture);
  assertThat(eviction.weightedSize().getAsLong(), is(0L));
  assertThat(cache.synchronous().estimatedSize(), is(1L));

  ready.set(true);
  Awaits.await().untilTrue(done);
  Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(0L));
  Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(0L));

  assertThat(context, hasRemovalNotifications(context, 1, RemovalCause.SIZE));
  verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.SIZE));
}
 
Example #22
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void put(Cache<String, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  cache.put("a", asList(1, 2, 3));
  assertThat(cache.estimatedSize(), is(1L));
  assertThat(eviction.weightedSize().getAsLong(), is(3L));
}
 
Example #23
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void put_sameWeight(Cache<String, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  cache.put("a", asList(-1, -2, -3));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(4L));
}
 
Example #24
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replace_sameWeight(Cache<String, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  cache.asMap().replace("a", asList(-1, -2, -3));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(4L));
}
 
Example #25
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void removeConditionally_fails(
    Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  assertThat(cache.asMap().remove("a", asList(-1, -2, -3)), is(false));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(4L));
}
 
Example #26
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.TEN,
    weigher = CacheWeigher.VALUE, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG,
    removalListener = Listener.CONSUMING)
@SuppressWarnings("FutureReturnValueIgnored")
public void evict_weighted_async(AsyncLoadingCache<Integer, Integer> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  AtomicBoolean ready = new AtomicBoolean();
  AtomicBoolean done = new AtomicBoolean();
  CompletableFuture<Integer> valueFuture = CompletableFuture.supplyAsync(() -> {
    Awaits.await().untilTrue(ready);
    return 6;
  });
  valueFuture.whenComplete((r, e) -> done.set(true));

  cache.put(5, CompletableFuture.completedFuture(5));
  cache.put(4, CompletableFuture.completedFuture(4));
  cache.put(6, valueFuture);
  assertThat(eviction.weightedSize().getAsLong(), is(9L));
  assertThat(cache.synchronous().estimatedSize(), is(3L));

  ready.set(true);
  Awaits.await().untilTrue(done);
  Awaits.await().until(context::consumedNotifications, hasSize(1));
  Awaits.await().until(() -> cache.synchronous().estimatedSize(), is(2L));
  Awaits.await().until(() -> eviction.weightedSize().getAsLong(), is(10L));

  assertThat(context, hasEvictionWeight(5L));
  assertThat(context, hasRemovalNotifications(context, 1, RemovalCause.SIZE));
  verifyWriter(context, (verifier, writer) -> verifier.deletions(1, RemovalCause.SIZE));
}
 
Example #27
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replace_changeWeight(Cache<String, List<Integer>> cache,
    CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  cache.asMap().replace("a", asList(-1, -2, -3, -4));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(5L));
}
 
Example #28
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replaceConditionally_sameWeight(
    Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  assertThat(cache.asMap().replace("a", asList(1, 2, 3), asList(4, 5, 6)), is(true));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(4L));
}
 
Example #29
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replaceConditionally_changeWeight(
    Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  cache.asMap().replace("a", asList(1, 2, 3), asList(-1, -2, -3, -4));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(5L));
}
 
Example #30
Source File: EvictionTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
    weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
    keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replaceConditionally_fails(
    Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
  cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));

  assertThat(cache.asMap().replace("a", asList(1), asList(4, 5)), is(false));
  assertThat(cache.estimatedSize(), is(2L));
  assertThat(eviction.weightedSize().getAsLong(), is(4L));
}