com.github.benmanes.caffeine.cache.Cache.putAll()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.6k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中com.github.benmanes.caffeine.cache.Cache.putAll()方法的一些代码示例,展示了Cache.putAll()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.putAll()方法的具体详情如下:
包路径:com.github.benmanes.caffeine.cache.Cache
类名称:Cache
方法名:putAll

Cache.putAll介绍

[英]Copies all of the mappings from the specified map to the cache. The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
[中]将所有映射从指定映射复制到缓存。对于指定映射中从键k到值v的每个映射,此调用的效果相当于在该映射上调用put(k,v)一次。如果在操作进行过程中修改了指定的映射,则此操作的行为未定义。

代码示例

代码示例来源:origin: ben-manes/caffeine

@Override
public void putAll(Map<? extends K,? extends V> m) {
 cache.putAll(m);
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void remove(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"), is(asList(1, 2, 3)));
 assertThat(cache.estimatedSize(), is(1L));
 assertThat(eviction.weightedSize().getAsLong(), is(1L));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@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(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(true));
 assertThat(cache.estimatedSize(), is(1L));
 assertThat(eviction.weightedSize().getAsLong(), is(1L));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@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));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void putAll_insert(Cache<Integer, Integer> cache, CacheContext context) {
 int startKey = context.original().size() + 1;
 Map<Integer, Integer> entries = IntStream
   .range(startKey, 100 + startKey).boxed()
   .collect(Collectors.toMap(Function.identity(), key -> -key));
 cache.putAll(entries);
 assertThat(cache.estimatedSize(), is(100 + context.initialSize()));
 verifyWriter(context, (verifier, writer) -> {
  verifier.wroteAll(entries);
 });
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void putAll_empty(Cache<Integer, Integer> cache, CacheContext context) {
 cache.putAll(new HashMap<>());
 assertThat(cache.estimatedSize(), is(context.initialSize()));
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void putAll_null(Cache<Integer, Integer> cache, CacheContext context) {
 cache.putAll(null);
}

代码示例来源:origin: ben-manes/caffeine

@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));
 });
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.CONSUMING })
public void putAll_replace(Cache<Integer, Integer> cache, CacheContext context) {
 Map<Integer, Integer> entries = new HashMap<>(context.original());
 entries.replaceAll((key, value) -> value + 1);
 cache.putAll(entries);
 assertThat(cache.asMap(), is(equalTo(entries)));
 assertThat(cache, hasRemovalNotifications(context, entries.size(), RemovalCause.REPLACED));
 verifyWriter(context, (verifier, writer) -> {
  verifier.wroteAll(entries);
 });
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = WriteException.class)
@CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG,
  compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING)
public void putAll_insert_writerFails(Cache<Integer, Integer> cache, CacheContext context) {
 try {
  cache.putAll(context.absent());
 } finally {
  assertThat(cache.asMap(), equalTo(context.original()));
 }
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoStats
@Test(dataProvider = "caches", expectedExceptions = WriteException.class)
@CacheSpec(implementation = Implementation.Caffeine, keys = ReferenceType.STRONG,
  population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  compute = Compute.SYNC, writer = Writer.EXCEPTIONAL, removalListener = Listener.REJECTING)
public void putAll_replace_writerFails(Cache<Integer, Integer> cache, CacheContext context) {
 try {
  cache.putAll(ImmutableMap.of(context.middleKey(), context.absentValue()));
 } finally {
  assertThat(cache.asMap(), equalTo(context.original()));
 }
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches", expectedExceptions = DeleteException.class)
@CacheSpec(keys = ReferenceType.STRONG, values = {ReferenceType.WEAK, ReferenceType.SOFT},
  implementation = Implementation.Caffeine, expireAfterAccess = Expire.DISABLED,
  expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED,
  weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED,
  compute = Compute.SYNC, removalListener = Listener.CONSUMING, writer = Writer.EXCEPTIONAL)
public void putAll_writerFails(Cache<Integer, Integer> cache, CacheContext context) {
 Integer key = context.firstKey();
 try {
  context.clear();
  GcFinalization.awaitFullGc();
  cache.putAll(ImmutableMap.of(key, context.absentValue()));
 } finally {
  context.disableRejectingCacheWriter();
  assertThat(cache.asMap().isEmpty(), is(false));
 }
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(population = Population.FULL, expiryTime = Expire.ONE_MINUTE,
  mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE },
  expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS },
  expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
  expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE})
public void putAll_insert(Cache<Integer, Integer> cache, CacheContext context) {
 context.ticker().advance(1, TimeUnit.MINUTES);
 cache.putAll(ImmutableMap.of(context.firstKey(), context.absentValue(),
   context.middleKey(), context.absentValue(), context.lastKey(), context.absentValue()));
 long count = context.initialSize();
 assertThat(cache.estimatedSize(), is(3L));
 assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED));
 verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED));
}

相关文章