Search in sources :

Example 1 with DefaultCacheMonitor

use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.

the class CacheContext method buildCache.

protected Cache buildCache(CacheAnnoConfig cacheAnnoConfig, String area, String fullCacheName) {
    Cache cache;
    if (cacheAnnoConfig.getCacheType() == CacheType.LOCAL) {
        cache = buildLocal(cacheAnnoConfig, area);
    } else if (cacheAnnoConfig.getCacheType() == CacheType.REMOTE) {
        cache = buildRemote(cacheAnnoConfig, area, fullCacheName);
    } else {
        Cache local = buildLocal(cacheAnnoConfig, area);
        Cache remote = buildRemote(cacheAnnoConfig, area, fullCacheName);
        if (defaultCacheMonitorManager != null) {
            DefaultCacheMonitor localMonitor = new DefaultCacheMonitor(fullCacheName + "_local");
            local.config().getMonitors().add(localMonitor);
            DefaultCacheMonitor remoteMonitor = new DefaultCacheMonitor(fullCacheName + "_remote");
            remote.config().getMonitors().add(remoteMonitor);
            defaultCacheMonitorManager.add(localMonitor, remoteMonitor);
        }
        cache = MultiLevelCacheBuilder.createMultiLevelCacheBuilder().expireAfterWrite(local.config().getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS).addCache(local, remote).buildCache();
    }
    cache = new RefreshCache(cache);
    if (defaultCacheMonitorManager != null) {
        DefaultCacheMonitor monitor = new DefaultCacheMonitor(fullCacheName);
        cache.config().getMonitors().add(monitor);
        defaultCacheMonitorManager.add(monitor);
    }
    return cache;
}
Also used : RefreshCache(com.alicp.jetcache.RefreshCache) DefaultCacheMonitor(com.alicp.jetcache.support.DefaultCacheMonitor) RefreshCache(com.alicp.jetcache.RefreshCache) Cache(com.alicp.jetcache.Cache) EnableCache(com.alicp.jetcache.anno.EnableCache)

Example 2 with DefaultCacheMonitor

use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.

the class LoadingCacheTest method nullValueTest.

private static void nullValueTest(Cache cache, long waitMillis) throws Exception {
    DefaultCacheMonitor monitor = new DefaultCacheMonitor("test");
    cache.config().getMonitors().add(monitor);
    cache.config().setLoader((key) -> null);
    Assert.assertNull(cache.get("nullValueTest_K1"));
    //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(0, monitor.getCacheStat().getPutCount());
    cache.config().setCacheNullValue(true);
    Assert.assertNull(cache.get("nullValueTest_K1"));
    //wait for async operations
    Thread.sleep(waitMillis);
    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(1, monitor.getCacheStat().getPutCount());
    Assert.assertNull(cache.get("nullValueTest_K1"));
    //wait for async operations
    Thread.sleep(waitMillis);
    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(1, monitor.getCacheStat().getPutCount());
    cache.config().getMonitors().remove(monitor);
}
Also used : DefaultCacheMonitor(com.alicp.jetcache.support.DefaultCacheMonitor)

Example 3 with DefaultCacheMonitor

use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.

the class MultiLevelCacheTest method doMonitoredTest.

private void doMonitoredTest(int expireMillis, Runnable test) {
    initL1L2(expireMillis);
    DefaultCacheMonitor m1 = new DefaultCacheMonitor("l1");
    DefaultCacheMonitor m1_again = new DefaultCacheMonitor("l1_monitor_again");
    DefaultCacheMonitor m2 = new DefaultCacheMonitor("l2");
    DefaultCacheMonitor mc = new DefaultCacheMonitor("mc");
    l1Cache = new MonitoredCache(l1Cache, m1);
    l1Cache = new MonitoredCache(l1Cache, m1_again);
    l2Cache = new MonitoredCache(l2Cache, m2);
    cache = new MultiLevelCache<>(l1Cache, l2Cache);
    cache = new MonitoredCache<>(cache, mc);
    DefaultCacheMonitorManager logger = new DefaultCacheMonitorManager(1, TimeUnit.SECONDS, true);
    logger.add(m1, m1_again, m2, mc);
    logger.start();
    test.run();
    logger.stop();
}
Also used : DefaultCacheMonitorManager(com.alicp.jetcache.support.DefaultCacheMonitorManager) DefaultCacheMonitor(com.alicp.jetcache.support.DefaultCacheMonitor)

Example 4 with DefaultCacheMonitor

use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.

the class RefreshCacheTest method refreshCacheTest2.

private static void refreshCacheTest2(Cache cache) throws Exception {
    DefaultCacheMonitor monitor = new DefaultCacheMonitor("test");
    cache.config().getMonitors().add(monitor);
    long refreshMillis = cache.config().getRefreshPolicy().getRefreshMillis();
    long stopRefresh = cache.config().getRefreshPolicy().getStopRefreshAfterLastAccessMillis();
    Assert.assertEquals("refreshCacheTest2_K1_V0", cache.get("refreshCacheTest2_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("refreshCacheTest2_K2_V1", cache.get("refreshCacheTest2_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());
    long totalSleepTime = 0;
    while (true) {
        long sleepTime = stopRefresh / 2;
        Thread.sleep(sleepTime);
        totalSleepTime += sleepTime;
        cache.get("refreshCacheTest2_K1");
        if (totalSleepTime > 1.3 * refreshMillis) {
            break;
        }
    }
    //stop refresh
    cache.config().setRefreshPolicy(null);
    Assert.assertEquals(3, monitor.getCacheStat().getLoadCount());
    long missCount = monitor.getCacheStat().getGetMissCount();
    long hitCount = monitor.getCacheStat().getGetHitCount();
    Assert.assertNotEquals("refreshCacheTest2_K1_V0", cache.get("refreshCacheTest2_K1"));
    Assert.assertEquals(hitCount + 1, monitor.getCacheStat().getGetHitCount());
    Assert.assertEquals(missCount, monitor.getCacheStat().getGetMissCount());
    Assert.assertEquals(3, monitor.getCacheStat().getLoadCount());
    Assert.assertEquals(3, monitor.getCacheStat().getPutCount());
    // refresh task stopped, but it is not expires
    Assert.assertEquals("refreshCacheTest2_K2_V1", cache.get("refreshCacheTest2_K2"));
    Assert.assertEquals(hitCount + 2, monitor.getCacheStat().getGetHitCount());
    Assert.assertEquals(missCount, monitor.getCacheStat().getGetMissCount());
    Assert.assertEquals(3, monitor.getCacheStat().getLoadCount());
    Assert.assertEquals(3, monitor.getCacheStat().getPutCount());
    cache.config().getMonitors().remove(monitor);
    cache.close();
}
Also used : DefaultCacheMonitor(com.alicp.jetcache.support.DefaultCacheMonitor)

Example 5 with DefaultCacheMonitor

use of com.alicp.jetcache.support.DefaultCacheMonitor in project jetcache by alibaba.

the class CacheMonitorExample method main.

public static void main(String[] args) throws Exception {
    DefaultCacheMonitor orderCacheMonitor = new DefaultCacheMonitor("OrderCache");
    Cache<String, Integer> cache = CaffeineCacheBuilder.createCaffeineCacheBuilder().limit(100).expireAfterWrite(200, TimeUnit.SECONDS).keyConvertor(FastjsonKeyConvertor.INSTANCE).addMonitor(orderCacheMonitor).buildCache();
    boolean verboseLog = false;
    DefaultCacheMonitorManager statLogger = new DefaultCacheMonitorManager(1, TimeUnit.SECONDS, verboseLog);
    statLogger.add(orderCacheMonitor);
    statLogger.start();
    Thread t = new Thread(() -> {
        for (int i = 0; i < 100; i++) {
            cache.put("20161111", 123456789);
            cache.get("20161111");
            cache.get("20161212");
            cache.remove("20161111");
            cache.remove("20161212");
            try {
                Thread.sleep(100);
            } catch (Exception e) {
            }
        }
    });
    t.start();
    t.join();
    statLogger.stop();
}
Also used : DefaultCacheMonitorManager(com.alicp.jetcache.support.DefaultCacheMonitorManager) DefaultCacheMonitor(com.alicp.jetcache.support.DefaultCacheMonitor)

Aggregations

DefaultCacheMonitor (com.alicp.jetcache.support.DefaultCacheMonitor)8 DefaultCacheMonitorManager (com.alicp.jetcache.support.DefaultCacheMonitorManager)3 Cache (com.alicp.jetcache.Cache)1 RefreshCache (com.alicp.jetcache.RefreshCache)1 EnableCache (com.alicp.jetcache.anno.EnableCache)1 TreeSet (java.util.TreeSet)1