Search in sources :

Example 11 with Cache

use of com.alicp.jetcache.Cache in project jetcache by alibaba.

the class DefaultCacheMonitorTest method testWithoutLogger.

@Test
public void testWithoutLogger() {
    Cache cache = createCache();
    testMonitor(cache);
}
Also used : Cache(com.alicp.jetcache.Cache) MultiLevelCache(com.alicp.jetcache.MultiLevelCache) Test(org.junit.Test)

Example 12 with Cache

use of com.alicp.jetcache.Cache in project jetcache by alibaba.

the class DefaultCacheMonitorTest method testWithManager.

@Test
public void testWithManager() throws Exception {
    Cache c1 = createCache();
    DefaultCacheMonitor m1 = new DefaultCacheMonitor("cache1");
    Cache c2 = createCache();
    DefaultCacheMonitor m2 = new DefaultCacheMonitor("cache2");
    c1.config().getMonitors().add(m1);
    c2.config().getMonitors().add(m2);
    DefaultMetricsManager manager = new DefaultMetricsManager(10, TimeUnit.SECONDS, true);
    manager.start();
    manager.add(m1, m2);
    basetest(c1, m1);
    basetest(c2, m2);
    Cache mc = new MultiLevelCache(c1, c2);
    DefaultCacheMonitor mcm = new DefaultCacheMonitor("multiCache");
    mc.config().getMonitors().add(mcm);
    manager.add(mcm);
    basetest(mc, mcm);
    manager.cmd.run();
    manager.stop();
// Thread.sleep(10000);
}
Also used : MultiLevelCache(com.alicp.jetcache.MultiLevelCache) Cache(com.alicp.jetcache.Cache) MultiLevelCache(com.alicp.jetcache.MultiLevelCache) Test(org.junit.Test)

Example 13 with Cache

use of com.alicp.jetcache.Cache in project jetcache by alibaba.

the class DefaultCacheMonitorManager method addCacheUpdateMonitor.

protected void addCacheUpdateMonitor(String area, String cacheName, Cache cache) {
    if (cacheMessagePublisher != null) {
        CacheMonitor monitor = event -> {
            if (event instanceof CachePutEvent) {
                CacheMessage m = new CacheMessage();
                CachePutEvent e = (CachePutEvent) event;
                m.setType(CacheMessage.TYPE_PUT);
                m.setKeys(new Object[] { e.getKey() });
                cacheMessagePublisher.publish(area, cacheName, m);
            } else if (event instanceof CacheRemoveEvent) {
                CacheMessage m = new CacheMessage();
                CacheRemoveEvent e = (CacheRemoveEvent) event;
                m.setType(CacheMessage.TYPE_REMOVE);
                m.setKeys(new Object[] { e.getKey() });
                cacheMessagePublisher.publish(area, cacheName, m);
            } else if (event instanceof CachePutAllEvent) {
                CacheMessage m = new CacheMessage();
                CachePutAllEvent e = (CachePutAllEvent) event;
                m.setType(CacheMessage.TYPE_PUT_ALL);
                if (e.getMap() != null) {
                    m.setKeys(e.getMap().keySet().toArray());
                }
                cacheMessagePublisher.publish(area, cacheName, m);
            } else if (event instanceof CacheRemoveAllEvent) {
                CacheMessage m = new CacheMessage();
                CacheRemoveAllEvent e = (CacheRemoveAllEvent) event;
                m.setType(CacheMessage.TYPE_REMOVE_ALL);
                if (e.getKeys() != null) {
                    m.setKeys(e.getKeys().toArray());
                }
                cacheMessagePublisher.publish(area, cacheName, m);
            }
        };
        cache.config().getMonitors().add(monitor);
    }
}
Also used : Cache(com.alicp.jetcache.Cache) CacheRemoveEvent(com.alicp.jetcache.event.CacheRemoveEvent) Resource(javax.annotation.Resource) Autowired(org.springframework.beans.factory.annotation.Autowired) CachePutAllEvent(com.alicp.jetcache.event.CachePutAllEvent) CacheUtil(com.alicp.jetcache.CacheUtil) CachePutEvent(com.alicp.jetcache.event.CachePutEvent) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CacheMonitor(com.alicp.jetcache.CacheMonitor) MultiLevelCache(com.alicp.jetcache.MultiLevelCache) CacheRemoveAllEvent(com.alicp.jetcache.event.CacheRemoveAllEvent) com.alicp.jetcache.support(com.alicp.jetcache.support) CacheMonitor(com.alicp.jetcache.CacheMonitor) CachePutEvent(com.alicp.jetcache.event.CachePutEvent) CacheRemoveEvent(com.alicp.jetcache.event.CacheRemoveEvent) CachePutAllEvent(com.alicp.jetcache.event.CachePutAllEvent) CacheRemoveAllEvent(com.alicp.jetcache.event.CacheRemoveAllEvent)

Example 14 with Cache

use of com.alicp.jetcache.Cache in project jetcache by alibaba.

the class DefaultCacheMonitorManager method addMetricsMonitor.

protected void addMetricsMonitor(String area, String cacheName, Cache cache) {
    if (defaultMetricsManager != null) {
        cache = CacheUtil.getAbstractCache(cache);
        if (cache instanceof MultiLevelCache) {
            MultiLevelCache mc = (MultiLevelCache) cache;
            if (mc.caches().length == 2) {
                Cache local = mc.caches()[0];
                Cache remote = mc.caches()[1];
                DefaultCacheMonitor localMonitor = new DefaultCacheMonitor(cacheName + "_local");
                local.config().getMonitors().add(localMonitor);
                DefaultCacheMonitor remoteMonitor = new DefaultCacheMonitor(cacheName + "_remote");
                remote.config().getMonitors().add(remoteMonitor);
                defaultMetricsManager.add(localMonitor, remoteMonitor);
            }
        }
        DefaultCacheMonitor monitor = new DefaultCacheMonitor(cacheName);
        cache.config().getMonitors().add(monitor);
        defaultMetricsManager.add(monitor);
    }
}
Also used : MultiLevelCache(com.alicp.jetcache.MultiLevelCache) Cache(com.alicp.jetcache.Cache) MultiLevelCache(com.alicp.jetcache.MultiLevelCache)

Example 15 with Cache

use of com.alicp.jetcache.Cache in project jetcache by alibaba.

the class CacheContext method createCacheInvokeContext.

public CacheInvokeContext createCacheInvokeContext(ConfigMap configMap) {
    CacheInvokeContext c = newCacheInvokeContext();
    c.setCacheFunction((invokeContext, cacheAnnoConfig) -> {
        Cache cache = cacheAnnoConfig.getCache();
        if (cache == null) {
            if (cacheAnnoConfig instanceof CachedAnnoConfig) {
                cache = createCacheByCachedConfig((CachedAnnoConfig) cacheAnnoConfig, invokeContext);
            } else if ((cacheAnnoConfig instanceof CacheInvalidateAnnoConfig) || (cacheAnnoConfig instanceof CacheUpdateAnnoConfig)) {
                CacheInvokeConfig cacheDefineConfig = configMap.getByCacheName(cacheAnnoConfig.getArea(), cacheAnnoConfig.getName());
                if (cacheDefineConfig == null) {
                    String message = "can't find @Cached definition with area=" + cacheAnnoConfig.getArea() + " name=" + cacheAnnoConfig.getName() + ", specified in " + cacheAnnoConfig.getDefineMethod();
                    CacheConfigException e = new CacheConfigException(message);
                    logger.error("Cache operation aborted because can't find @Cached definition", e);
                    return null;
                }
                cache = createCacheByCachedConfig(cacheDefineConfig.getCachedAnnoConfig(), invokeContext);
            }
            cacheAnnoConfig.setCache(cache);
        }
        return cache;
    });
    return c;
}
Also used : CacheConfigException(com.alicp.jetcache.CacheConfigException) CacheInvokeConfig(com.alicp.jetcache.anno.method.CacheInvokeConfig) CacheInvokeContext(com.alicp.jetcache.anno.method.CacheInvokeContext) Cache(com.alicp.jetcache.Cache) EnableCache(com.alicp.jetcache.anno.EnableCache)

Aggregations

Cache (com.alicp.jetcache.Cache)17 EnableCache (com.alicp.jetcache.anno.EnableCache)7 Test (org.junit.Test)5 MultiLevelCache (com.alicp.jetcache.MultiLevelCache)4 RefreshCache (com.alicp.jetcache.RefreshCache)3 CacheConfigException (com.alicp.jetcache.CacheConfigException)2 LoadingCacheTest (com.alicp.jetcache.LoadingCacheTest)2 AbstractExternalCacheTest (com.alicp.jetcache.test.external.AbstractExternalCacheTest)2 Random (java.util.Random)2 AbstractCache (com.alicp.jetcache.AbstractCache)1 CacheGetResult (com.alicp.jetcache.CacheGetResult)1 CacheMonitor (com.alicp.jetcache.CacheMonitor)1 CacheUtil (com.alicp.jetcache.CacheUtil)1 ProxyCache (com.alicp.jetcache.ProxyCache)1 RefreshCacheTest (com.alicp.jetcache.RefreshCacheTest)1 CacheHandler (com.alicp.jetcache.anno.method.CacheHandler)1 CacheInvokeConfig (com.alicp.jetcache.anno.method.CacheInvokeConfig)1 CacheInvokeContext (com.alicp.jetcache.anno.method.CacheInvokeContext)1 EmbeddedCacheBuilder (com.alicp.jetcache.embedded.EmbeddedCacheBuilder)1 CachePutAllEvent (com.alicp.jetcache.event.CachePutAllEvent)1