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

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

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

Cache.getAllPresent介绍

[英]Returns a map of the values associated with the keys in this cache. The returned map will only contain entries which are already present in the cache.

Note that duplicate elements in keys, as determined by Object#equals, will be ignored.
[中]返回与此缓存中的键关联的值的映射。返回的映射将只包含缓存中已存在的条目。
请注意,由Object#equals确定的键中的重复元素将被忽略。

代码示例

代码示例来源:origin: alibaba/jetcache

@Override
public Map getAllValues(Collection keys) {
  return cache.getAllPresent(keys);
}

代码示例来源:origin: apache/incubator-druid

@Override
public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys)
{
 // The assumption here is that every value is accessed at least once. Materializing here ensures deserialize is only
 // called *once* per value.
 return ImmutableMap.copyOf(Maps.transformValues(cache.getAllPresent(keys), this::deserialize));
}

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

@Override
public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
 return ImmutableMap.copyOf(cache.getAllPresent(keys));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getAllPresent_iterable_empty(Cache<Integer, Integer> cache, CacheContext context) {
 Map<Integer, Integer> result = cache.getAllPresent(ImmutableList.of());
 assertThat(result.size(), is(0));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getAllPresent_ordered(Cache<Integer, Integer> cache, CacheContext context) {
 List<Integer> keys = new ArrayList<>(context.original().keySet());
 Collections.shuffle(keys);
 List<Integer> result = new ArrayList<>(cache.getAllPresent(keys).keySet());
 assertThat(result, is(equalTo(keys)));
}

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

Map<K, Expirable<V>> result = new HashMap<>(cache.getAllPresent(keys));

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

@Test(dataProvider = "caches")
@CacheSpec(population = Population.EMPTY, keys = ReferenceType.STRONG, writer = Writer.DISABLED)
public void getAllPresent_jdk8186171(Cache<Object, Integer> cache, CacheContext context) {
 class Key {
  @Override public int hashCode() {
   return 0; // to put keys in one bucket
  }
 }
 List<Key> keys = new ArrayList<>();
 for (int i = 0; i < Population.FULL.size(); i++) {
  keys.add(new Key());
 }
 Key key = Iterables.getLast(keys);
 Integer value = context.absentValue();
 cache.put(key, value);
 Map<Object, Integer> result = cache.getAllPresent(keys);
 assertThat(result.values(), not(hasItem(nullValue())));
 assertThat(result, is(equalTo(ImmutableMap.of(key, value))));
}

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

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

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

@CacheSpec
@CheckNoWriter
@Test(dataProvider = "caches", expectedExceptions = UnsupportedOperationException.class)
public void getAllPresent_immutable(Cache<Integer, Integer> cache, CacheContext context) {
 cache.getAllPresent(context.absentKeys()).clear();
}

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

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

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

@Test(dataProvider = "caches")
@CacheSpec(keys = ReferenceType.STRONG, values = {ReferenceType.WEAK, ReferenceType.SOFT},
  expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED,
  maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT,
  population = Population.FULL, stats = Stats.ENABLED, removalListener = Listener.CONSUMING)
public void getAllPresent(Cache<Integer, Integer> cache, CacheContext context) {
 Set<Integer> keys = context.firstMiddleLastKeys();
 context.clear();
 GcFinalization.awaitFullGc();
 assertThat(cache.getAllPresent(keys), is(emptyMap()));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getAllPresent_present_full(Cache<Integer, Integer> cache, CacheContext context) {
 Map<Integer, Integer> result = cache.getAllPresent(context.original().keySet());
 assertThat(result, is(equalTo(context.original())));
 assertThat(context, both(hasMissCount(0)).and(hasHitCount(result.size())));
 assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0)));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.PARTIAL, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getAllPresent_present_partial(Cache<Integer, Integer> cache, CacheContext context) {
 Map<Integer, Integer> expect = new HashMap<>();
 expect.put(context.firstKey(), context.original().get(context.firstKey()));
 expect.put(context.middleKey(), context.original().get(context.middleKey()));
 expect.put(context.lastKey(), context.original().get(context.lastKey()));
 Map<Integer, Integer> result = cache.getAllPresent(expect.keySet());
 assertThat(result, is(equalTo(expect)));
 assertThat(context, both(hasMissCount(0)).and(hasHitCount(expect.size())));
 assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0)));
}

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

@Test(dataProvider = "caches")
@CacheSpec(population = { Population.PARTIAL, Population.FULL },
  mustExpireWithAnyOf = { AFTER_WRITE, VARIABLE }, expireAfterWrite = Expire.ONE_MINUTE,
  expiry = { CacheExpiry.DISABLED, CacheExpiry.WRITE }, expiryTime = Expire.ONE_MINUTE)
public void getAllPresent(Cache<Integer, Integer> cache, CacheContext context) {
 context.ticker().advance(30, TimeUnit.SECONDS);
 cache.getAllPresent(context.firstMiddleLastKeys());
 context.ticker().advance(45, TimeUnit.SECONDS);
 assertThat(cache.getAllPresent(context.firstMiddleLastKeys()).size(), is(0));
 cache.cleanUp();
 assertThat(cache.estimatedSize(), is(0L));
 long count = context.initialSize();
 assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED));
 verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getAllPresent_absent(Cache<Integer, Integer> cache, CacheContext context) {
 Map<Integer, Integer> result = cache.getAllPresent(context.absentKeys());
 assertThat(result.size(), is(0));
 int count = context.absentKeys().size();
 assertThat(context, both(hasMissCount(count)).and(hasHitCount(0)));
 assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0)));
}

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

@Test(dataProvider = "caches")
@CacheSpec(mustExpireWithAnyOf = { AFTER_ACCESS, VARIABLE },
  expiry = { CacheExpiry.DISABLED, CacheExpiry.ACCESS }, expiryTime = Expire.ONE_MINUTE,
  expireAfterAccess = Expire.ONE_MINUTE, population = { Population.PARTIAL, Population.FULL })
public void getAllPresent(Cache<Integer, Integer> cache, CacheContext context) {
 context.ticker().advance(30, TimeUnit.SECONDS);
 cache.getAllPresent(context.firstMiddleLastKeys());
 context.ticker().advance(45, TimeUnit.SECONDS);
 assertThat(cache.getAllPresent(context.firstMiddleLastKeys()).size(), is(3));
 cache.cleanUp();
 assertThat(cache.estimatedSize(), is(3L));
 long count = context.initialSize() - 3;
 assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.EXPIRED));
 verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.EXPIRED));
}

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

@CacheSpec(implementation = Implementation.Caffeine,
  population = Population.FULL, expiry = CacheExpiry.MOCKITO)
@Test(dataProvider = "caches", expectedExceptions = ExpirationException.class)
public void getAllPresent_expiryFails(Cache<Integer, Integer> cache, CacheContext context) {
 try {
  context.ticker().advance(1, TimeUnit.HOURS);
  when(context.expiry().expireAfterRead(any(), any(), anyLong(), anyLong()))
    .thenThrow(ExpirationException.class);
  cache.getAllPresent(context.firstMiddleLastKeys());
 } finally {
  context.ticker().advance(-1, TimeUnit.HOURS);
  assertThat(cache.asMap(), equalTo(context.original()));
 }
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getAllPresent_duplicates(Cache<Integer, Integer> cache, CacheContext context) {
 Iterable<Integer> keys = Iterables.concat(
   context.absentKeys(), context.absentKeys(),
   context.original().keySet(), context.original().keySet());
 Map<Integer, Integer> result = cache.getAllPresent(keys);
 int misses = context.absentKeys().size();
 int hits = context.original().keySet().size();
 if (context.isGuava()) {
  // does not filter duplicate queries
  misses += misses;
  hits += hits;
 }
 assertThat(result, is(equalTo(context.original())));
 assertThat(context, both(hasMissCount(misses)).and(hasHitCount(hits)));
 assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0)));
}

代码示例来源:origin: org.apache.druid/druid-server

@Override
public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys)
{
 // The assumption here is that every value is accessed at least once. Materializing here ensures deserialize is only
 // called *once* per value.
 return ImmutableMap.copyOf(Maps.transformValues(cache.getAllPresent(keys), this::deserialize));
}

代码示例来源:origin: org.apache.camel/camel-caffeine

@InvokeOnHeader(CaffeineConstants.ACTION_GET_ALL)
public void onGetAll(Message message) throws Exception {
  Object result = cache.getAllPresent(message.getHeader(CaffeineConstants.KEYS, Collections::emptySet, Set.class));
  setResult(message, true, result, null);
}

相关文章