Search in sources :

Example 1 with Cache

use of com.alicp.jetcache.Cache 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 Cache

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

the class CacheContext method __createOrGetCache.

public Cache __createOrGetCache(CacheAnnoConfig cacheAnnoConfig, String area, String fullCacheName) {
    Cache cache = cacheManager.getCache(fullCacheName);
    if (cache == null) {
        synchronized (this) {
            cache = cacheManager.getCache(fullCacheName);
            if (cache == null) {
                cache = buildCache(cacheAnnoConfig, area, fullCacheName);
                cacheManager.addCache(fullCacheName, cache);
            }
        }
    }
    return cache;
}
Also used : RefreshCache(com.alicp.jetcache.RefreshCache) Cache(com.alicp.jetcache.Cache) EnableCache(com.alicp.jetcache.anno.EnableCache)

Example 3 with Cache

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

the class CacheContext method buildLocal.

protected Cache buildLocal(CacheAnnoConfig cacheAnnoConfig, String area) {
    Cache cache;
    EmbeddedCacheBuilder cacheBuilder = (EmbeddedCacheBuilder) globalCacheConfig.getLocalCacheBuilders().get(area);
    if (cacheBuilder == null) {
        throw new CacheConfigException("no local cache builder: " + area);
    }
    cacheBuilder = (EmbeddedCacheBuilder) cacheBuilder.clone();
    if (cacheAnnoConfig.getLocalLimit() != CacheConsts.UNDEFINED_INT) {
        cacheBuilder.setLimit(cacheAnnoConfig.getLocalLimit());
    }
    if (cacheAnnoConfig.getExpire() > 0) {
        cacheBuilder.expireAfterWrite(cacheAnnoConfig.getExpire(), cacheAnnoConfig.getTimeUnit());
    }
    if (!CacheConsts.UNDEFINED_STRING.equals(cacheAnnoConfig.getKeyConvertor())) {
        cacheBuilder.setKeyConvertor(configProvider.parseKeyConvertor(cacheAnnoConfig.getKeyConvertor()));
    }
    cache = cacheBuilder.buildCache();
    return cache;
}
Also used : EmbeddedCacheBuilder(com.alicp.jetcache.embedded.EmbeddedCacheBuilder) CacheConfigException(com.alicp.jetcache.CacheConfigException) RefreshCache(com.alicp.jetcache.RefreshCache) Cache(com.alicp.jetcache.Cache) EnableCache(com.alicp.jetcache.anno.EnableCache)

Example 4 with Cache

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

the class SimpleCacheManager method getCache.

@Override
public Cache getCache(String area, String cacheName) {
    ConcurrentHashMap<String, Cache> areaMap = getCachesByArea(area);
    Cache c = areaMap.get(cacheName);
    if (c == null && cacheCreator != null) {
        return cacheCreator.apply(area, cacheName);
    } else {
        return c;
    }
}
Also used : Cache(com.alicp.jetcache.Cache)

Example 5 with Cache

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

the class CacheContext method createCacheByCachedConfig.

private Cache createCacheByCachedConfig(CachedAnnoConfig ac, CacheInvokeContext invokeContext) {
    String area = ac.getArea();
    String cacheName = ac.getName();
    if (CacheConsts.isUndefined(cacheName)) {
        cacheName = configProvider.createCacheNameGenerator(invokeContext.getHiddenPackages()).generateCacheName(invokeContext.getMethod(), invokeContext.getTargetObject());
    }
    Cache cache = __createOrGetCache(ac, area, cacheName);
    return cache;
}
Also used : 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