use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.
the class CacheMonitorWithMultiLevelCacheExample method main.
public static void main(String[] args) throws Exception {
DefaultCacheMonitor l1CacheMonitor = new DefaultCacheMonitor("OrderCache_L1");
DefaultCacheMonitor l2CacheMonitor = new DefaultCacheMonitor("OrderCache_L2");
DefaultCacheMonitor orderCacheMonitor = new DefaultCacheMonitor("OrderCache");
Cache<String, Integer> l1Cache = CaffeineCacheBuilder.createCaffeineCacheBuilder().limit(100).expireAfterWrite(200, TimeUnit.SECONDS).keyConvertor(FastjsonKeyConvertor.INSTANCE).addMonitor(l1CacheMonitor).buildCache();
Cache<String, Integer> l2Cache = CaffeineCacheBuilder.createCaffeineCacheBuilder().limit(100).expireAfterWrite(200, TimeUnit.SECONDS).keyConvertor(FastjsonKeyConvertor.INSTANCE).addMonitor(l2CacheMonitor).buildCache();
Cache<String, Integer> orderCache = MultiLevelCacheBuilder.createMultiLevelCacheBuilder().addCache(l1Cache, l2Cache).addMonitor(orderCacheMonitor).buildCache();
boolean verboseLog = true;
DefaultCacheMonitorManager statLogger = new DefaultCacheMonitorManager(1, TimeUnit.SECONDS, verboseLog);
statLogger.add(l1CacheMonitor, l2CacheMonitor, orderCacheMonitor);
statLogger.start();
Thread t = new Thread(() -> {
for (int i = 0; i < 100; i++) {
orderCache.put("20161111", 123456789);
orderCache.get("20161111");
orderCache.get("20161212");
orderCache.remove("20161111");
orderCache.remove("20161212");
orderCache.computeIfAbsent("20161111", (k) -> 100000);
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
});
t.start();
t.join();
statLogger.stop();
}
use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.
the class LoadingCacheTest method loadingCacheTestImpl.
private static void loadingCacheTestImpl(Cache cache, long waitMillis) throws Exception {
DefaultCacheMonitor monitor = new DefaultCacheMonitor("test");
cache.config().getMonitors().add(monitor);
Assert.assertEquals("LoadingCache_Key1_V0", cache.get("LoadingCache_Key1"));
//wait for async operations
Thread.sleep(waitMillis);
Assert.assertEquals(1, monitor.getCacheStat().getGetCount());
Assert.assertEquals(0, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(1, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(1, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(1, monitor.getCacheStat().getPutCount());
Assert.assertEquals("LoadingCache_Key1_V0", cache.get("LoadingCache_Key1"));
//wait for async operations
Thread.sleep(waitMillis);
Assert.assertEquals(2, monitor.getCacheStat().getGetCount());
Assert.assertEquals(1, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(1, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(1, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(1, monitor.getCacheStat().getPutCount());
Set<String> keys = new TreeSet<>();
keys.add("LoadingCache_Key1");
keys.add("LoadingCache_Key2");
keys.add("LoadingCache_Key3");
Map<Object, Object> map = cache.getAll(keys);
//wait for async operations
Thread.sleep(waitMillis);
Assert.assertEquals("LoadingCache_Key1_V0", map.get("LoadingCache_Key1"));
Assert.assertEquals("LoadingCache_Key2_V1", map.get("LoadingCache_Key2"));
Assert.assertEquals("LoadingCache_Key3_V2", map.get("LoadingCache_Key3"));
Assert.assertEquals(5, monitor.getCacheStat().getGetCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(3, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(3, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(3, monitor.getCacheStat().getPutCount());
cache.config().getMonitors().remove(monitor);
}
use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.
the class RefreshCacheTest method refreshCacheTest1.
private static void refreshCacheTest1(Cache cache) throws Exception {
DefaultCacheMonitor monitor = new DefaultCacheMonitor("test");
cache.config().getMonitors().add(monitor);
long refreshMillis = cache.config().getRefreshPolicy().getRefreshMillis();
Assert.assertEquals("refreshCacheTest1_K1_V0", cache.get("refreshCacheTest1_K1"));
Assert.assertEquals(1, monitor.getCacheStat().getGetCount());
Assert.assertEquals(0, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(1, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(1, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(1, monitor.getCacheStat().getPutCount());
Assert.assertEquals("refreshCacheTest1_K2_V1", cache.get("refreshCacheTest1_K2"));
Assert.assertEquals(2, monitor.getCacheStat().getGetCount());
Assert.assertEquals(0, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(2, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(2, monitor.getCacheStat().getPutCount());
Assert.assertEquals("refreshCacheTest1_K1_V0", cache.get("refreshCacheTest1_K1"));
Assert.assertEquals(3, monitor.getCacheStat().getGetCount());
Assert.assertEquals(1, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(2, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(2, monitor.getCacheStat().getPutCount());
Assert.assertEquals("refreshCacheTest1_K2_V1", cache.get("refreshCacheTest1_K2"));
Assert.assertEquals(4, monitor.getCacheStat().getGetCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(2, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(2, monitor.getCacheStat().getPutCount());
Thread.sleep((long) (1.5 * refreshMillis));
Assert.assertEquals(4, monitor.getCacheStat().getLoadCount());
Assert.assertNotEquals("refreshCacheTest1_K1_V0", cache.get("refreshCacheTest1_K1"));
Assert.assertEquals(5, monitor.getCacheStat().getGetCount());
Assert.assertEquals(3, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(4, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(4, monitor.getCacheStat().getPutCount());
Assert.assertNotEquals("refreshCacheTest1_K2_V1", cache.get("refreshCacheTest1_K2"));
Assert.assertEquals(6, monitor.getCacheStat().getGetCount());
Assert.assertEquals(4, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(2, monitor.getCacheStat().getGetMissCount());
Assert.assertEquals(4, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(4, monitor.getCacheStat().getPutCount());
cache.config().getMonitors().remove(monitor);
cache.close();
}
Aggregations