Skip to content

Commit

Permalink
improve cache initialization in CacheManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Jun 21, 2019
1 parent 786a74e commit af36a76
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.alicp.jetcache.anno.support.ConfigMap;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;

/**
* @author <a href="mailto:[email protected]">huangli</a>
Expand All @@ -14,6 +15,7 @@ public class CacheAdvisor extends AbstractBeanFactoryPointcutAdvisor {

public static final String CACHE_ADVISOR_BEAN_NAME = "jetcache2.internalCacheAdvisor";

@Autowired
private ConfigMap cacheConfigMap;

private String[] basePackages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

Expand All @@ -23,6 +24,7 @@ public class JetCacheInterceptor implements MethodInterceptor, ApplicationContex

//private static final Logger logger = LoggerFactory.getLogger(JetCacheInterceptor.class);

@Autowired
private ConfigMap cacheConfigMap;
private ApplicationContext applicationContext;
GlobalCacheConfig globalCacheConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,25 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

@Bean(name = CacheAdvisor.CACHE_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public CacheAdvisor jetcacheAdvisor() {
ConfigMap configMap = new ConfigMap();

JetCacheInterceptor jetCacheInterceptor = new JetCacheInterceptor();
jetCacheInterceptor.setCacheConfigMap(configMap);
jetCacheInterceptor.setApplicationContext(applicationContext);

public CacheAdvisor jetcacheAdvisor(JetCacheInterceptor jetCacheInterceptor) {
CacheAdvisor advisor = new CacheAdvisor();
advisor.setAdviceBeanName(CacheAdvisor.CACHE_ADVISOR_BEAN_NAME);
advisor.setAdvice(jetCacheInterceptor);
advisor.setBasePackages(this.enableMethodCache.getStringArray("basePackages"));
advisor.setCacheConfigMap(configMap);
advisor.setOrder(this.enableMethodCache.<Integer>getNumber("order"));
return advisor;
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public JetCacheInterceptor jetCacheInterceptor() {
return new JetCacheInterceptor();
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ConfigMap jetcacheConfigMap() {
return new ConfigMap();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.alicp.jetcache.anno.method;

import com.alicp.jetcache.anno.support.CacheContext;
import com.alicp.jetcache.anno.support.ConfigMap;
import com.alicp.jetcache.anno.support.GlobalCacheConfig;
import com.alicp.jetcache.anno.support.SimpleCacheManager;
import org.springframework.context.ApplicationContext;

/**
Expand All @@ -23,4 +23,19 @@ public SpringCacheContext(GlobalCacheConfig globalCacheConfig, ApplicationContex
protected CacheInvokeContext newCacheInvokeContext() {
return new SpringCacheInvokeContext(applicationContext);
}

@Override
public synchronized void init() {
if (applicationContext != null) {
ConfigMap configMap = applicationContext.getBean(ConfigMap.class);
cacheManager.setCacheCreator((area, cacheName) -> {
CacheInvokeConfig cic = configMap.getByCacheName(area, cacheName);
if (cic == null) {
throw new IllegalArgumentException("cache definition not found: area=" + area + ",cacheName=" + cacheName);
}
return __createOrGetCache(cic.getCachedAnnoConfig(), area, cacheName);
});
}
super.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected CacheThreadLocal initialValue() {
private GlobalCacheConfig globalCacheConfig;

private DefaultCacheMonitorManager defaultCacheMonitorManager;
private SimpleCacheManager cacheManager = SimpleCacheManager.defaultManager;
protected SimpleCacheManager cacheManager = SimpleCacheManager.defaultManager;
private boolean inited;

public CacheContext(GlobalCacheConfig globalCacheConfig) {
Expand Down Expand Up @@ -112,21 +112,23 @@ private Cache createCacheByCachedConfig(CachedAnnoConfig ac, CacheInvokeContext
return cache;
}

@Deprecated
public <K, V> Cache<K, V> getCache(String cacheName) {
return getCache(CacheConsts.DEFAULT_AREA, cacheName);
}

@Deprecated
public <K, V> Cache<K, V> getCache(String area, String cacheName) {
Cache cache = cacheManager.getCache(area, cacheName);
Cache cache = cacheManager.getCacheWithoutCreate(area, cacheName);
return cache;
}

public Cache __createOrGetCache(CachedAnnoConfig cachedAnnoConfig, String area, String cacheName) {
String fullCacheName = area + "_" + cacheName;
Cache cache = cacheManager.getCache(area, cacheName);
Cache cache = cacheManager.getCacheWithoutCreate(area, cacheName);
if (cache == null) {
synchronized (this) {
cache = cacheManager.getCache(area, cacheName);
cache = cacheManager.getCacheWithoutCreate(area, cacheName);
if (cache == null) {
if (globalCacheConfig.isAreaInCacheName()) {
// for compatible reason, if we use default configuration, the prefix should same to that version <=2.4.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;

/**
* @author <a href="mailto:[email protected]">huangli</a>
Expand All @@ -17,6 +18,7 @@ public class SimpleCacheManager implements CacheManager {
private static final Logger logger = LoggerFactory.getLogger(SimpleCacheManager.class);

private ConcurrentHashMap<String, ConcurrentHashMap<String, Cache>> caches = new ConcurrentHashMap<>();
private BiFunction<String, String, Cache> cacheCreator;

static SimpleCacheManager defaultManager = new SimpleCacheManager();

Expand All @@ -42,6 +44,16 @@ private ConcurrentHashMap<String, Cache> getCachesByArea(String area) {

@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;
}
}

public Cache getCacheWithoutCreate(String area, String cacheName) {
ConcurrentHashMap<String, Cache> areaMap = getCachesByArea(area);
return areaMap.get(cacheName);
}
Expand All @@ -50,4 +62,8 @@ public void putCache(String area, String cacheName, Cache cache) {
ConcurrentHashMap<String, Cache> areaMap = getCachesByArea(area);
areaMap.put(cacheName, cache);
}

public void setCacheCreator(BiFunction<String, String, Cache> cacheCreator) {
this.cacheCreator = cacheCreator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.alicp.jetcache.anno.config.EnableMethodCache;
import com.alicp.jetcache.test.anno.TestUtil;
import com.alicp.jetcache.test.spring.SpringTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.junit.Assert;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -53,8 +53,8 @@ public CountBean countBean() {
}


@Before
@After
@BeforeEach
@AfterEach
public void init() {
SimpleCacheManager.defaultManager.rebuild();
}
Expand All @@ -68,4 +68,9 @@ public void test() {
Assert.assertNotEquals(value, bean.count("K1"));
}

@Test
public void test2() {
Assert.assertNotNull(CacheManager.defaultManager().getCache("C1"));
}

}

0 comments on commit af36a76

Please sign in to comment.