private GuavaCacheManager createCacheManager() { GuavaCacheManager cacheManager = new GuavaCacheManager(); setCacheBuilder(cacheManager); if (this.cacheLoader != null) { cacheManager.setCacheLoader(this.cacheLoader); } return cacheManager; }
private void validateGuavaCacheWithStats() { GuavaCacheManager cacheManager = validateCacheManager(GuavaCacheManager.class); assertThat(cacheManager.getCacheNames(), containsInAnyOrder("foo", "bar")); assertThat(cacheManager.getCacheNames(), hasSize(2)); Cache foo = cacheManager.getCache("foo"); foo.get("1"); assertThat(((GuavaCache) foo).getNativeCache().stats().missCount(), equalTo(1L)); }
/* * Configure caching manager */ @Bean public CacheManager cacheManager() { CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(5, TimeUnit.MINUTES); GuavaCacheManager cacheManager = new GuavaCacheManager(); cacheManager.setCacheBuilder(builder); return cacheManager; }
@Bean public GuavaCacheManager cacheManager() { GuavaCacheManager cacheManager = createCacheManager(); List<String> cacheNames = this.cacheProperties.getCacheNames(); if (!CollectionUtils.isEmpty(cacheNames)) { cacheManager.setCacheNames(cacheNames); } return cacheManager; }
@Test public void guavaCacheExplicitWithCaches() { load(DefaultCacheConfiguration.class, "spring.cache.type=guava", "spring.cache.cacheNames=foo"); GuavaCacheManager cacheManager = validateCacheManager(GuavaCacheManager.class); Cache foo = cacheManager.getCache("foo"); foo.get("1"); // See next tests: no spec given so stats should be disabled assertThat(((GuavaCache) foo).getNativeCache().stats().missCount(), equalTo(0L)); }
private void setCacheBuilder(GuavaCacheManager cacheManager) { String specification = this.cacheProperties.getGuava().getSpec(); if (StringUtils.hasText(specification)) { cacheManager.setCacheSpecification(specification); } else if (this.cacheBuilderSpec != null) { cacheManager.setCacheBuilderSpec(this.cacheBuilderSpec); } else if (this.cacheBuilder != null) { cacheManager.setCacheBuilder(this.cacheBuilder); } }