private void addMetrics(Collection<Metric<?>> metrics, String cacheName, List<CacheManagerBean> cacheManagerBeans) { for (CacheManagerBean cacheManagerBean : cacheManagerBeans) { CacheManager cacheManager = cacheManagerBean.getCacheManager(); Cache cache = cacheManager.getCache(cacheName); CacheStatistics statistics = getCacheStatistics(cache, cacheManager); if (statistics != null) { String prefix = cacheName; if (cacheManagerBeans.size() > 1) { prefix = cacheManagerBean.getBeanName() + "_" + prefix; } prefix = "cache." + prefix + (prefix.endsWith(".") ? "" : "."); metrics.addAll(statistics.toMetrics(prefix)); } } }
@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; }
@Override public CacheStatistics getCacheStatistics(final CacheManager cacheManager, final RedissonCache cache) { final DefaultCacheStatistics defaultCacheStatistics = new DefaultCacheStatistics(); defaultCacheStatistics.setSize((long) cache.getNativeCache().size()); defaultCacheStatistics.setGetCacheCounts(cache.getCacheHits(), cache.getCacheMisses()); return defaultCacheStatistics; }
@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 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); }
private void assertCoreStatistics(CacheStatistics metrics, Long size, Double hitRatio, Double missRatio) { assertThat(metrics).isNotNull(); assertThat(metrics.getSize()).isEqualTo(size); checkRatio("Wrong hit ratio for metrics " + metrics, hitRatio, metrics.getHitRatio()); checkRatio("Wrong miss ratio for metrics " + metrics, missRatio, metrics.getMissRatio()); }
@Override public CacheStatistics getCacheStatistics(CacheManager cacheManager, RedisCache cache) { DefaultCacheStatistics statistics = new DefaultCacheStatistics(); // @see http://redis.io/commands/INFO RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory); try { Properties props = connection.info(); Long hitCount = Long.parseLong(props.getProperty("keyspace_hits")); Long missCount = Long.parseLong(props.getProperty("keyspace_misses")); statistics.setGetCacheCounts(hitCount, missCount); } finally { RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory); } return statistics; }
private void assertCoreStatistics(CacheStatistics metrics, Long size, Double hitRatio, Double missRatio) { assertNotNull("Cache metrics must not be null", metrics); assertEquals("Wrong size for metrics " + metrics, size, metrics.getSize()); checkRatio("Wrong hit ratio for metrics " + metrics, hitRatio, metrics.getHitRatio()); checkRatio("Wrong miss ratio for metrics " + metrics, missRatio, metrics.getMissRatio()); }