@SuppressWarnings({ "rawtypes", "unchecked" }) private CacheStatistics getCacheStatistics(Cache cache, CacheManager cacheManager) { if (this.statisticsProviders != null) { for (CacheStatisticsProvider provider : this.statisticsProviders) { Class<?> cacheType = ResolvableType .forClass(CacheStatisticsProvider.class, provider.getClass()) .resolveGeneric(); if (cacheType.isInstance(cache)) { CacheStatistics statistics = provider.getCacheStatistics(cacheManager, cache); if (statistics != null) { return statistics; } } } } return null; }
@Bean public CacheStatisticsProvider<Cache> noOpCacheStatisticsProvider() { return new CacheStatisticsProvider<Cache>() { @Override public CacheStatistics getCacheStatistics(CacheManager cacheManager, Cache cache) { if (cacheManager instanceof NoOpCacheManager) { return NO_OP_STATS; } return null; } }; }
@Test public void basicJCacheCacheStatistics() { load(JCacheCacheConfig.class); CacheStatisticsProvider provider = this.context .getBean("jCacheCacheStatisticsProvider", CacheStatisticsProvider.class); doTestCoreStatistics(provider, false); }
@Test public void basicEhCacheCacheStatistics() { load(EhCacheConfig.class); CacheStatisticsProvider provider = this.context .getBean("ehCacheCacheStatisticsProvider", CacheStatisticsProvider.class); doTestCoreStatistics(provider, true); }
@Test public void basicHazelcastCacheStatistics() { load(HazelcastConfig.class); CacheStatisticsProvider provider = this.context.getBean( "hazelcastCacheStatisticsProvider", CacheStatisticsProvider.class); doTestCoreStatistics(provider, true); }
@Test public void basicInfinispanCacheStatistics() { load(InfinispanConfig.class); CacheStatisticsProvider provider = this.context.getBean( "infinispanCacheStatisticsProvider", CacheStatisticsProvider.class); doTestCoreStatistics(provider, true); }
@Test public void basicGuavaCacheStatistics() { load(GuavaConfig.class); CacheStatisticsProvider provider = this.context .getBean("guavaCacheStatisticsProvider", CacheStatisticsProvider.class); doTestCoreStatistics(provider, true); }
@Test public void baseCaffeineCacheStatistics() { load(CaffeineCacheConfig.class); CacheStatisticsProvider provider = this.context.getBean( "caffeineCacheStatisticsProvider", CacheStatisticsProvider.class); doTestCoreStatistics(provider, true); }
@Test public void concurrentMapCacheStatistics() { load(ConcurrentMapConfig.class); CacheStatisticsProvider provider = this.context.getBean( "concurrentMapCacheStatisticsProvider", CacheStatisticsProvider.class); Cache books = getCache("books"); CacheStatistics cacheStatistics = provider.getCacheStatistics(this.cacheManager, books); assertCoreStatistics(cacheStatistics, 0L, null, null); getOrCreate(books, "a", "b", "b", "a", "a"); CacheStatistics updatedCacheStatistics = provider .getCacheStatistics(this.cacheManager, books); assertCoreStatistics(updatedCacheStatistics, 2L, null, null); }
@Test public void noOpCacheStatistics() { load(NoOpCacheConfig.class); CacheStatisticsProvider provider = this.context .getBean("noOpCacheStatisticsProvider", CacheStatisticsProvider.class); Cache books = getCache("books"); CacheStatistics cacheStatistics = provider.getCacheStatistics(this.cacheManager, books); assertCoreStatistics(cacheStatistics, null, null, null); getOrCreate(books, "a", "b", "b", "a", "a"); CacheStatistics updatedCacheStatistics = provider .getCacheStatistics(this.cacheManager, books); assertCoreStatistics(updatedCacheStatistics, null, null, null); }
private void doTestCoreStatistics(CacheStatisticsProvider provider, boolean supportSize) { Cache books = getCache("books"); CacheStatistics cacheStatistics = provider.getCacheStatistics(this.cacheManager, books); assertCoreStatistics(cacheStatistics, (supportSize ? 0L : null), null, null); getOrCreate(books, "a", "b", "b", "a", "a", "a"); CacheStatistics updatedCacheStatistics = provider .getCacheStatistics(this.cacheManager, books); assertCoreStatistics(updatedCacheStatistics, (supportSize ? 2L : null), 0.66D, 0.33D); }
@Bean @ConditionalOnMissingBean @ConditionalOnBean(CacheStatisticsProvider.class) public CachePublicMetrics cachePublicMetrics() { return new CachePublicMetrics(); }