@Override public Object get(Object key) { return store.get(key); }
@Override public Object get(Object key) { return store.get(key); }
@Override @Nullable protected Object lookup(Object key) { return this.cache.get(key); }
@Override public Object getFromCache(Object key, SharedSessionContractImplementor session) { return underlyingCache.get( key ); }
@Override @Nullable protected Object lookup(Object key) { return this.cache.get(key); }
@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; }
@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)))); }
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); } }
@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")); } }
@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"))); }
@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"))); }
@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")); }
@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")); } }
@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"); }
@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"); }
@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"); }
@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"); }
@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")); } } }
@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"); }