/** * Test statistics enabling/disabling/clearing * * @throws InterruptedException */ @Test public void testCacheUsageStatistics() throws InterruptedException { // Set size so the second element overflows to disk. Cache cache = new Cache("test", 1, true, false, 5, 2); manager.addCache(cache); // add as a listener AnotherStatistics anotherStats = new AnotherStatistics(); cache.registerCacheUsageListener(anotherStats); cache.setStatisticsEnabled(true); doTestCacheUsageStatistics(cache, true, anotherStats); // test enable/disable statistics cache.setStatisticsEnabled(false); doTestCacheUsageStatistics(cache, false, anotherStats); // remove the listener cache.removeCacheUsageListener(anotherStats); // enable statistics but don't check stats as no longer a listener cache.setStatisticsEnabled(true); doTestCacheUsageStatistics(cache, false, anotherStats); assertEquals( Statistics.STATISTICS_ACCURACY_BEST_EFFORT, cache.getLiveCacheStatistics().getStatisticsAccuracy()); assertEquals("Best Effort", cache.getLiveCacheStatistics().getStatisticsAccuracyDescription()); }
@PostConstruct public void initialize() { stats = new Stats(); boolean cacheExists = cacheManager.cacheExists(getName()); if (cacheExists) { if (failOnDuplicateCache) { throw new RuntimeException("A previous cache with name [" + getName() + "] exists."); } else { log.warn("skip duplicate cache " + getName()); ehcache = cacheManager.getCache(getName()); } } if (!cacheExists) { ehcache = new Cache( getName(), getMaxElementsInMemory(), getMemoryStoreEvictionPolicy(), isOverflowToDisk(), getDiskStorePath(), isEternal(), getTimeToLiveSeconds(), getTimeToIdleSeconds(), isDiskPersistent(), getDiskExpiryThreadIntervalSeconds(), null); cacheManager.addCache(ehcache); ehcache.setStatisticsEnabled(statisticsEnabled); } }
@Before public void initialiseCache() { MockitoAnnotations.initMocks(this); topUpStatusCache = cacheManager.getCache("topUpStatusCache"); topUpStatusCache.removeAll(); topUpStatusCache.clearStatistics(); topUpStatusCache.setStatisticsEnabled(true); underTest = new TopUpResultService( topUpStatusCache, dailyAwardPromotionService, queuePublishingService); }
/** * Test average get time * * @throws InterruptedException */ @Test public void testAverageGetTime() throws InterruptedException { Cache cache = new Cache("test", 0, true, false, 5, 2); manager.addCache(cache); // add as a listener AnotherStatistics anotherStats = new AnotherStatistics(); cache.registerCacheUsageListener(anotherStats); cache.setStatisticsEnabled(true); doTestAverageGetTime(cache, true, anotherStats); // test enable/disable statistics cache.setStatisticsEnabled(false); doTestAverageGetTime(cache, false, anotherStats); // remove the listener cache.removeCacheUsageListener(anotherStats); // enable statistics but don't check stats as no longer a listener cache.setStatisticsEnabled(true); doTestAverageGetTime(cache, false, anotherStats); }
@Test public void getTopUpResultShouldReturnCachedResultIfPlayerHasCacheEntry() { topUpStatusCache.setStatisticsEnabled(true); long cacheHitsPreRequest = topUpStatusCache.getStatistics().getCacheHits(); final TopUpResult expectedTopUpResult = new TopUpResult(PLAYER_ID, ACKNOWLEDGED, new DateTime()); Element element = new Element( PLAYER_ID, new TopUpResultService.TopUpStatusCacheEntry( ACKNOWLEDGED, expectedTopUpResult.getLastTopUpDate())); topUpStatusCache.put(element); final TopUpResult actualTopUpResult = underTest.getTopUpResult(PLAYER_ID, WEB); assertThat(topUpStatusCache.getStatistics().getCacheHits(), is(cacheHitsPreRequest + 1)); assertThat(actualTopUpResult, is(expectedTopUpResult)); verify(dailyAwardPromotionService, never()).getTopUpResult(PLAYER_ID, WEB); }
public ResourceCache() { CacheManager cacheManager = CacheManager.create(); cache = cacheManager.getCache("resource_cache"); cache.setStatisticsEnabled(ENABLE_STATISTICS); }
public void setStatisticsEnabled(boolean arg0) { cache.setStatisticsEnabled(arg0); }