diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/CacheAdvisor.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/CacheAdvisor.java
index 20c700283..f540f3f7e 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/CacheAdvisor.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/CacheAdvisor.java
@@ -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 huangli
@@ -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;
diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java
index 64f01028c..40fc195ed 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/aop/JetCacheInterceptor.java
@@ -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;
@@ -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;
diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/config/JetCacheProxyConfiguration.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/config/JetCacheProxyConfiguration.java
index 3448166c4..930ae3d4a 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/config/JetCacheProxyConfiguration.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/config/JetCacheProxyConfiguration.java
@@ -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.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();
+ }
+
}
\ No newline at end of file
diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/SpringCacheContext.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/SpringCacheContext.java
index 75fb29339..24b4dae86 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/SpringCacheContext.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/SpringCacheContext.java
@@ -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;
/**
@@ -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();
+ }
}
diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/CacheContext.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/CacheContext.java
index 7db37fd10..bc6b6738d 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/CacheContext.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/CacheContext.java
@@ -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) {
@@ -112,21 +112,23 @@ private Cache createCacheByCachedConfig(CachedAnnoConfig ac, CacheInvokeContext
return cache;
}
+ @Deprecated
public Cache getCache(String cacheName) {
return getCache(CacheConsts.DEFAULT_AREA, cacheName);
}
+ @Deprecated
public Cache 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
diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/SimpleCacheManager.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/SimpleCacheManager.java
index 3008def1e..369b3cf7c 100644
--- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/SimpleCacheManager.java
+++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/support/SimpleCacheManager.java
@@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiFunction;
/**
* @author huangli
@@ -17,6 +18,7 @@ public class SimpleCacheManager implements CacheManager {
private static final Logger logger = LoggerFactory.getLogger(SimpleCacheManager.class);
private ConcurrentHashMap> caches = new ConcurrentHashMap<>();
+ private BiFunction cacheCreator;
static SimpleCacheManager defaultManager = new SimpleCacheManager();
@@ -42,6 +44,16 @@ private ConcurrentHashMap getCachesByArea(String area) {
@Override
public Cache getCache(String area, String cacheName) {
+ ConcurrentHashMap 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 areaMap = getCachesByArea(area);
return areaMap.get(cacheName);
}
@@ -50,4 +62,8 @@ public void putCache(String area, String cacheName, Cache cache) {
ConcurrentHashMap areaMap = getCachesByArea(area);
areaMap.put(cacheName, cache);
}
+
+ public void setCacheCreator(BiFunction cacheCreator) {
+ this.cacheCreator = cacheCreator;
+ }
}
diff --git a/jetcache-test/src/test/java/com/alicp/jetcache/anno/support/CacheManagerTest.java b/jetcache-test/src/test/java/com/alicp/jetcache/anno/support/CacheManagerTest.java
index 818442b11..f16d43daf 100644
--- a/jetcache-test/src/test/java/com/alicp/jetcache/anno/support/CacheManagerTest.java
+++ b/jetcache-test/src/test/java/com/alicp/jetcache/anno/support/CacheManagerTest.java
@@ -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;
@@ -53,8 +53,8 @@ public CountBean countBean() {
}
- @Before
- @After
+ @BeforeEach
+ @AfterEach
public void init() {
SimpleCacheManager.defaultManager.rebuild();
}
@@ -68,4 +68,9 @@ public void test() {
Assert.assertNotEquals(value, bean.count("K1"));
}
+ @Test
+ public void test2() {
+ Assert.assertNotNull(CacheManager.defaultManager().getCache("C1"));
+ }
+
}