本文整理了Java中javax.cache.Cache.get()
方法的一些代码示例,展示了Cache.get()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.get()
方法的具体详情如下:
包路径:javax.cache.Cache
类名称:Cache
方法名:get
[英]Gets an entry from the cache.
If the cache is configured to use read-through, and get would return null because the entry is missing from the cache, the Cache's CacheLoaderis called in an attempt to load the entry.
[中]从缓存中获取一个条目。
如果缓存配置为使用读通,get将返回null,因为缓存中缺少该项,则会调用缓存的CacheLoader以尝试加载该项。
代码示例来源:origin: apache/incubator-dubbo
@Override
public Object get(Object key) {
return store.get(key);
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public Object get(Object key) {
return store.get(key);
}
代码示例来源:origin: spring-projects/spring-framework
@Override
@Nullable
protected Object lookup(Object key) {
return this.cache.get(key);
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
public Object getFromCache(Object key, SharedSessionContractImplementor session) {
return underlyingCache.get( key );
}
代码示例来源:origin: org.springframework/spring-context-support
@Override
@Nullable
protected Object lookup(Object key) {
return this.cache.get(key);
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void test_getCacheMisses() throws Exception {
heapCache.get("key");
heapCache.get("key");
heapCache.get("key");
heapCache.get("key");
heapCache.get("key");
assertThat(heapStatistics.getCacheMisses(), is(5L));
}
代码示例来源:origin: requery/requery
@Override
public <T> T get(Class<T> type, Object key) {
Cache cache = getCache(type);
if (cache != null && cache.isClosed()) {
cache = null;
}
if (cache != null) {
SerializedEntity container = (SerializedEntity) cache.get(key);
if (container != null) {
return type.cast(container.getEntity());
}
}
return null;
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void test_getCacheHitPercentage() throws Exception {
heapCache.put("key", "value");
heapCache.get("key");
heapCache.get("key");
heapCache.get("key");
heapCache.get("nokey");
heapCache.get("nokey");
assertThat(heapStatistics.getCacheHitPercentage(), is(allOf(greaterThan(59f), lessThan(61f))));
}
代码示例来源:origin: ben-manes/caffeine
public void start() {
for (Integer i = 0; i < KEYS; i++) {
cache.put(i, Boolean.TRUE);
}
Runnable task = () -> {
for (int i = random.nextInt(); ; i++) {
Integer key = Math.abs(i % KEYS);
if (READ) {
requireNonNull(cache.get(key));
} else {
cache.put(key, Boolean.TRUE);
}
count.increment();
}
};
scheduleStatusTask();
for (int i = 0; i < THREADS; i++) {
executor.execute(task);
}
}
代码示例来源:origin: ben-manes/caffeine
@Test
public void sanity() {
CachingProvider cachingProvider = Caching.getCachingProvider(
"com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider",
getClass().getClassLoader());
Cache<String, Integer> cache = cachingProvider.getCacheManager()
.getCache("test-cache-2", String.class, Integer.class);
assertNull(cache.get("a"));
}
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimplePutIfAbsentWithLoaderAndWriter_existsInSor() throws Exception {
assertThat(testCache.containsKey(1), is(false));
assertThat(testCache.putIfAbsent(1, "one"), is(true));
assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
verifyZeroInteractions(cacheLoader);
verify(cacheWriter, times(1)).write(eq(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one")));
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimplePutIfAbsentWithLoaderAndWriter_absent() throws Exception {
assertThat(testCache.containsKey(1), is(false));
assertThat(testCache.putIfAbsent(1, "one"), is(true));
assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
verify(cacheLoader).load(1);
verify(cacheWriter, times(1)).write(eq(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one")));
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testJSR107DefaultSerializer() throws URISyntaxException {
Cache<Long, String> cache = cacheManager.getCache("cache", Long.class, String.class);
cache.put(19L, "foo");
assertThat(cache.get(19L), equalTo("foo"));
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testJSR107Serializer() {
Cache<Long, String> cache1 = cacheManager.getCache("cache1", Long.class, String.class);
cache1.put(17L, "foo");
assertThat(cache1.get(17L), equalTo("foo"));
}
}
代码示例来源:origin: resilience4j/resilience4j
@SuppressWarnings("unchecked")
@Test
public void testDecorateSupplierWithCache() {
javax.cache.Cache<String, String> cache = mock(javax.cache.Cache.class);
// Given the cache contains the key
given(cache.containsKey("testKey")).willReturn(true);
// Return the value from cache
given(cache.get("testKey")).willReturn("Hello from cache");
Function<String, String> cachedFunction = Decorators.ofSupplier(() -> "Hello world")
.withCache(Cache.of(cache))
.decorate();
String value = cachedFunction.apply("testKey");
assertThat(value).isEqualTo("Hello from cache");
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldConsumeOnCacheMissEvent() throws Throwable {
// Given the cache does not contain the key
given(cache.get("testKey")).willReturn(null);
Cache<String, String> cacheContext = Cache.of(cache);
cacheContext.getEventPublisher().onCacheMiss(event ->
logger.info(event.getEventType().toString()));
CheckedFunction1<String, String> cachedFunction = Cache.decorateCheckedSupplier(cacheContext, () -> "Hello world");
String value = cachedFunction.apply("testKey");
assertThat(value).isEqualTo("Hello world");
then(logger).should(times(1)).info("CACHE_MISS");
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldConsumeOnCacheHitEvent() throws Throwable {
// Given the cache does not contain the key
given(cache.get("testKey")).willReturn("Hello world");
Cache<String, String> cacheContext = Cache.of(cache);
cacheContext.getEventPublisher().onCacheHit(event ->
logger.info(event.getEventType().toString()));
CheckedFunction1<String, String> cachedFunction = Cache.decorateCheckedSupplier(cacheContext, () -> "Hello world");
String value = cachedFunction.apply("testKey");
assertThat(value).isEqualTo("Hello world");
then(logger).should(times(1)).info("CACHE_HIT");
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldConsumeOnErrorEvent() throws Throwable {
// Given the cache does not contain the key
given(cache.get("testKey")).willThrow(new WebServiceException("BLA"));
Cache<String, String> cacheContext = Cache.of(cache);
cacheContext.getEventPublisher().onError(event ->
logger.info(event.getEventType().toString()));
CheckedFunction1<String, String> cachedFunction = Cache.decorateCheckedSupplier(cacheContext, () -> "Hello world");
String value = cachedFunction.apply("testKey");
assertThat(value).isEqualTo("Hello world");
then(logger).should(times(1)).info("ERROR");
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testBasicCacheOperation() throws IOException, URISyntaxException {
Configuration config = new DefaultConfiguration(ResourceCombinationsTest.class.getClassLoader(),
new DefaultPersistenceConfiguration(diskPath.newFolder()));
try (CacheManager cacheManager = new EhcacheCachingProvider().getCacheManager(URI.create("dummy"), config)) {
Cache<String, String> cache = cacheManager.createCache("test", fromEhcacheCacheConfiguration(
newCacheConfigurationBuilder(String.class, String.class, resources)));
cache.put("foo", "bar");
assertThat(cache.get("foo"), is("bar"));
}
}
}
代码示例来源:origin: resilience4j/resilience4j
@SuppressWarnings("unchecked")
@Test
public void testDecorateCheckedSupplierWithCache() {
javax.cache.Cache<String, String> cache = mock(javax.cache.Cache.class);
// Given the cache contains the key
given(cache.containsKey("testKey")).willReturn(true);
// Return the value from cache
given(cache.get("testKey")).willReturn("Hello from cache");
CheckedFunction1<String, String> cachedFunction = Decorators.ofCheckedSupplier(() -> "Hello world")
.withCache(Cache.of(cache))
.decorate();
String value = Try.of(() -> cachedFunction.apply("testKey")).get();
assertThat(value).isEqualTo("Hello from cache");
}
内容来源于网络,如有侵权,请联系作者删除!