本文整理了Java中com.github.benmanes.caffeine.cache.Cache.getIfPresent()
方法的一些代码示例,展示了Cache.getIfPresent()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.getIfPresent()
方法的具体详情如下:
包路径:com.github.benmanes.caffeine.cache.Cache
类名称:Cache
方法名:getIfPresent
[英]Returns the value associated with the key in this cache, or null if there is no cached value for the key.
[中]返回与此缓存中的键关联的值,如果没有该键的缓存值,则返回null。
代码示例来源:origin: spring-projects/spring-framework
@Override
@Nullable
protected Object lookup(Object key) {
return this.cache.getIfPresent(key);
}
代码示例来源:origin: apache/incubator-druid
@Override
public byte[] get(NamedKey key)
{
return deserialize(cache.getIfPresent(key));
}
代码示例来源:origin: ben-manes/caffeine
@Override @Nullable
public V getIfPresent(Object key) {
return cache.getIfPresent(key);
}
代码示例来源:origin: dropwizard/dropwizard
/**
* Gets the human friendly location of where the violation was raised.
*/
public static String getMessage(ConstraintViolation<?> v, Invocable invocable) {
final Pair<Path, ? extends ConstraintDescriptor<?>> of =
Pair.of(v.getPropertyPath(), v.getConstraintDescriptor());
final String cachePrefix = PREFIX_CACHE.getIfPresent(of);
if (cachePrefix == null) {
final String prefix = calculatePrefix(v, invocable);
PREFIX_CACHE.put(of, prefix);
return prefix + v.getMessage();
}
return cachePrefix + v.getMessage();
}
代码示例来源:origin: ben-manes/caffeine
@Nullable
protected static Expirable<Integer> getExpirable(
CacheProxy<Integer, Integer> cache, Integer key) {
return cache.cache.getIfPresent(key);
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(keys = ReferenceType.WEAK, population = Population.FULL)
public void identity_keys(Cache<Integer, Integer> cache, CacheContext context) {
@SuppressWarnings("deprecation")
Integer key = new Integer(context.firstKey());
assertThat(cache.getIfPresent(key), is(nullValue()));
}
代码示例来源:origin: ben-manes/caffeine
@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING },
population = { Population.SINGLETON, Population.PARTIAL, Population.FULL })
public void getIfPresent_present(Cache<Integer, Integer> cache, CacheContext context) {
assertThat(cache.getIfPresent(context.firstKey()), is(not(nullValue())));
assertThat(cache.getIfPresent(context.middleKey()), is(not(nullValue())));
assertThat(cache.getIfPresent(context.lastKey()), is(not(nullValue())));
assertThat(context, both(hasMissCount(0)).and(hasHitCount(3)));
assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0)));
}
代码示例来源: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 getIfPresent(Cache<Integer, Integer> cache, CacheContext context) {
Integer key = context.firstKey();
context.clear();
GcFinalization.awaitFullGc();
assertThat(cache.getIfPresent(key), is(nullValue()));
}
代码示例来源:origin: ben-manes/caffeine
@Override
public void record(long key) {
Object value = cache.getIfPresent(key);
if (value == null) {
if (cache.estimatedSize() == maximumSize) {
policyStats.recordEviction();
}
cache.put(key, key);
policyStats.recordMiss();
} else {
policyStats.recordHit();
}
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE)
public void putIfAbsent_insert(Cache<Integer, Integer> cache, CacheContext context,
VarExpiration<Integer, Integer> expireAfterVar) {
Integer key = context.absentKey();
Integer value = context.absentValue();
assertThat(expireAfterVar.putIfAbsent(key, value, Duration.ofMinutes(2L)), is(true));
assertThat(cache.getIfPresent(key), is(value));
assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(2L))));
context.ticker().advance(90, TimeUnit.SECONDS);
cache.cleanUp();
assertThat(cache.estimatedSize(), is(1L));
}
代码示例来源:origin: ben-manes/caffeine
@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void getIfPresent_nullKey(Cache<Integer, Integer> cache, CacheContext context) {
cache.getIfPresent(null);
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE)
public void putIfAbsent_present(Cache<Integer, Integer> cache, CacheContext context,
VarExpiration<Integer, Integer> expireAfterVar) {
Integer key = context.firstKey();
Integer value = context.absentValue();
assertThat(expireAfterVar.putIfAbsent(key, value, Duration.ofMinutes(2L)), is(false));
assertThat(cache.getIfPresent(key), is(context.original().get(key)));
assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(1L))));
context.ticker().advance(90, TimeUnit.SECONDS);
cache.cleanUp();
assertThat(cache.estimatedSize(), is(0L));
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE)
public void put_insert(Cache<Integer, Integer> cache, CacheContext context,
VarExpiration<Integer, Integer> expireAfterVar) {
Integer key = context.absentKey();
Integer value = context.absentValue();
expireAfterVar.put(key, value, Duration.ofMinutes(2L));
assertThat(cache.getIfPresent(key), is(value));
assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(2L))));
context.ticker().advance(90, TimeUnit.SECONDS);
cache.cleanUp();
assertThat(cache.estimatedSize(), is(1L));
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
expiry = CacheExpiry.WRITE, expiryTime = Expire.ONE_MINUTE)
public void put_replace(Cache<Integer, Integer> cache, CacheContext context,
VarExpiration<Integer, Integer> expireAfterVar) {
Integer key = context.firstKey();
Integer value = context.absentValue();
expireAfterVar.put(key, value, Duration.ofMinutes(2L));
assertThat(cache.getIfPresent(key), is(value));
assertThat(expireAfterVar.getExpiresAfter(key), is(Optional.of(Duration.ofMinutes(2L))));
context.ticker().advance(90, TimeUnit.SECONDS);
cache.cleanUp();
assertThat(cache.estimatedSize(), is(1L));
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_insert(Cache<Integer, Integer> cache, CacheContext context) {
cache.put(context.absentKey(), context.absentValue());
assertThat(cache.estimatedSize(), is(context.initialSize() + 1));
assertThat(cache.getIfPresent(context.absentKey()), is(context.absentValue()));
}
代码示例来源:origin: ben-manes/caffeine
@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL })
public void put_replace_sameValue(Cache<Integer, Integer> cache, CacheContext context) {
for (Integer key : context.firstMiddleLastKeys()) {
Integer value = context.original().get(key);
cache.put(key, value);
assertThat(cache.getIfPresent(key), is(value));
}
assertThat(cache.estimatedSize(), is(context.initialSize()));
int count = context.firstMiddleLastKeys().size();
if (context.isGuava() || context.isAsync()) {
assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.REPLACED));
}
}
代码示例来源:origin: ben-manes/caffeine
@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void getIfPresent_absent(Cache<Integer, Integer> cache, CacheContext context) {
assertThat(cache.getIfPresent(context.absentKey()), is(nullValue()));
assertThat(context, both(hasMissCount(1)).and(hasHitCount(0)));
assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(0)));
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL })
public void put_replace_differentValue(Cache<Integer, Integer> cache, CacheContext context) {
for (Integer key : context.firstMiddleLastKeys()) {
cache.put(key, context.absentValue());
assertThat(cache.getIfPresent(key), is(context.absentValue()));
verifyWriter(context, (verifier, writer) -> {
verifier.wrote(key, context.absentValue());
});
}
assertThat(cache.estimatedSize(), is(context.initialSize()));
int count = context.firstMiddleLastKeys().size();
assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.REPLACED));
}
代码示例来源:origin: ben-manes/caffeine
@CacheSpec(implementation = Implementation.Caffeine,
population = Population.FULL, expiry = CacheExpiry.MOCKITO)
@Test(dataProvider = "caches", expectedExceptions = ExpirationException.class)
public void getIfPresent_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.getIfPresent(context.firstKey());
} finally {
context.ticker().advance(-1, TimeUnit.HOURS);
assertThat(cache.asMap(), equalTo(context.original()));
}
}
代码示例来源:origin: ben-manes/caffeine
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_insert(AsyncCache<Integer, Integer> cache, CacheContext context) {
CompletableFuture<Integer> value = CompletableFuture.completedFuture(context.absentValue());
cache.put(context.absentKey(), value);
assertThat(cache.synchronous().estimatedSize(), is(context.initialSize() + 1));
assertThat(context, both(hasMissCount(0)).and(hasHitCount(0)));
assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0)));
assertThat(cache.synchronous().getIfPresent(context.absentKey()), is(context.absentValue()));
}
内容来源于网络,如有侵权,请联系作者删除!