Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1st refactor: complex method in invoke method #901

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,61 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
initializeDependencies();

if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
return invocation.proceed();
}

if (!initializeCacheManager()) {
return invocation.proceed();
}

CacheInvokeConfig cac = getCacheInvokeConfig(invocation);

if (cac == null || cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
return invocation.proceed();
}

return handleCacheInvocation(invocation, cac);
}

private void initializeDependencies() {
if (configProvider == null) {
configProvider = applicationContext.getBean(ConfigProvider.class);
}
if (configProvider != null && globalCacheConfig == null) {
globalCacheConfig = configProvider.getGlobalCacheConfig();
}
if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
return invocation.proceed();
}
}

private boolean initializeCacheManager() {
if (cacheManager == null) {
cacheManager = applicationContext.getBean(CacheManager.class);
if (cacheManager == null) {
logger.error("There is no cache manager instance in spring context");
return invocation.proceed();
return false;
}
}
return true;
}

private CacheInvokeConfig getCacheInvokeConfig(final MethodInvocation invocation) {
Method method = invocation.getMethod();
Object obj = invocation.getThis();
CacheInvokeConfig cac = null;
if (obj != null) {
String key = CachePointcut.getKey(method, obj.getClass());
cac = cacheConfigMap.getByMethodInfo(key);
}

/*
if(logger.isTraceEnabled()){
logger.trace("JetCacheInterceptor invoke. foundJetCacheConfig={}, method={}.{}(), targetClass={}",
cac != null,
method.getDeclaringClass().getName(),
method.getName(),
invocation.getThis() == null ? null : invocation.getThis().getClass().getName());
}
*/

if (cac == null || cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
return invocation.proceed();
cac = cacheConfigMap.getByMethodInfo(key);
}
return cac;
}

private Object handleCacheInvocation(final MethodInvocation invocation, CacheInvokeConfig cac) throws Throwable {
CacheInvokeContext context = configProvider.newContext(cacheManager).createCacheInvokeContext(cacheConfigMap);
context.setTargetObject(invocation.getThis());
context.setInvoker(invocation::proceed);
context.setMethod(method);
context.setMethod(invocation.getMethod());
context.setArgs(invocation.getArguments());
context.setCacheInvokeConfig(cac);
context.setHiddenPackages(globalCacheConfig.getHiddenPackages());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static RefreshPolicy parseRefreshPolicy(CacheRefresh cacheRefresh) {
TimeUnit t = cacheRefresh.timeUnit();
policy.setRefreshMillis(t.toMillis(cacheRefresh.refresh()));
if (!CacheConsts.isUndefined(cacheRefresh.stopRefreshAfterLastAccess())) {
policy.setStopRefreshAfterLastAccessMillis(t.toMillis(cacheRefresh.stopRefreshAfterLastAccess()));
policy.setRefreshIntervalMillis(t.toMillis(cacheRefresh.stopRefreshAfterLastAccess()));
}
if (!CacheConsts.isUndefined(cacheRefresh.refreshLockTimeout())) {
policy.setRefreshLockTimeoutMillis(t.toMillis(cacheRefresh.refreshLockTimeout()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void run() {
return;
}
long now = System.currentTimeMillis();
long stopRefreshAfterLastAccessMillis = config.getRefreshPolicy().getStopRefreshAfterLastAccessMillis();
long stopRefreshAfterLastAccessMillis = config.getRefreshPolicy().getRefreshIntervalMillis();
if (stopRefreshAfterLastAccessMillis > 0) {
if (lastAccessTime + stopRefreshAfterLastAccessMillis < now) {
logger.debug("cancel refresh: {}", key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class RefreshPolicy implements Cloneable {

private long refreshMillis;
private long stopRefreshAfterLastAccessMillis;
private long refreshIntervalMillis;
private long refreshLockTimeoutMillis = 60 * 1000;

public RefreshPolicy() {
Expand All @@ -22,8 +22,8 @@ public static RefreshPolicy newPolicy(long time, TimeUnit timeUnit) {
return p;
}

public RefreshPolicy stopRefreshAfterLastAccess(long time, TimeUnit timeUnit) {
this.stopRefreshAfterLastAccessMillis = timeUnit.toMillis(time);
public RefreshPolicy refreshIntervalMillis(long time, TimeUnit unit) {
this.refreshIntervalMillis = unit.toMillis(time);
return this;
}

Expand All @@ -49,12 +49,12 @@ public void setRefreshMillis(long refreshMillis) {
this.refreshMillis = refreshMillis;
}

public long getStopRefreshAfterLastAccessMillis() {
return stopRefreshAfterLastAccessMillis;
public long getRefreshIntervalMillis() {
return refreshIntervalMillis;
}

public void setStopRefreshAfterLastAccessMillis(long stopRefreshAfterLastAccessMillis) {
this.stopRefreshAfterLastAccessMillis = stopRefreshAfterLastAccessMillis;
public void setRefreshIntervalMillis(long refreshIntervalMillis) {
this.refreshIntervalMillis = refreshIntervalMillis;
}

public long getRefreshLockTimeoutMillis() {
Expand Down
8 changes: 8 additions & 0 deletions jetcache-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public static void refreshCacheTest(Cache cache, long refresh, long stopRefreshA
getRefreshCache(cache).stopRefresh();

count.set(0);
cache.config().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(stopRefreshAfterLastAccess);
cache.config().getRefreshPolicy().setRefreshIntervalMillis(stopRefreshAfterLastAccess);
refreshCacheTest2(cache);
getRefreshCache(cache).stopRefresh();

cache.config().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(0);
cache.config().getRefreshPolicy().setRefreshIntervalMillis(0);
vetoTest(cache);
getRefreshCache(cache).stopRefresh();

Expand All @@ -126,7 +126,7 @@ public static void refreshCacheTest(AbstractCacheBuilder builder, long refresh,
cache.close();

count.set(0);
builder.getConfig().getRefreshPolicy().stopRefreshAfterLastAccess(stopRefreshAfterLastAccess, TimeUnit.MILLISECONDS);
builder.getConfig().getRefreshPolicy().refreshIntervalMillis(stopRefreshAfterLastAccess, TimeUnit.MILLISECONDS);
cache = builder.buildCache();
refreshCacheTest2(cache);
cache.close();
Expand Down Expand Up @@ -253,7 +253,7 @@ 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();
long stopRefresh = cache.config().getRefreshPolicy().getRefreshIntervalMillis();

Set s = new HashSet();
s.add("refreshCacheTest2_K1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testMultiLevelCache() {
RefreshPolicy rp = new RefreshPolicy();
rp.setRefreshMillis(100);
rp.setRefreshLockTimeoutMillis(200);
rp.setStopRefreshAfterLastAccessMillis(300);
rp.setRefreshIntervalMillis(300);

Function keyConvertor = k -> k;
Function valueEncoder = k -> k;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,70 +143,35 @@ private Cache getTarget(Cache cache) {
return cache;
}


@PostConstruct
public void test() throws Exception {
public void init() throws Exception {
runGeneralTest();
refreshTest();
cacheWithoutConvertorTest();
AbstractCacheTest.penetrationProtectTest(cacheWithProtect);
testCacheWithLocalExpire();

cache1.put("KK1", "V1");
Assert.assertNull(cache_A1.get("KK1"));
Assert.assertNull(cache2.get("KK1"));

Assert.assertSame(getTarget(cacheSameName1), getTarget(cacheSameName2));
Assert.assertSame(getTarget(cacheSameName1),
getTarget(cacheManager.getCache("sameCacheName")));
Assert.assertNotSame(getTarget(cacheSameName1), getTarget(cache1));

cacheSameName1.put("SameKey", "SameValue");
Assert.assertEquals(cacheSameName1.get("SameKey"),cacheSameName2.get("SameKey"));
Assert.assertNull(cache1.get("SameKey"));

Assert.assertTrue(getTarget(cache1) instanceof MockRemoteCache);
Assert.assertSame(Fastjson2KeyConvertor.INSTANCE, cache1.config().getKeyConvertor());

Assert.assertTrue(getTarget(cacheWithConfig) instanceof MultiLevelCache);
Assert.assertEquals(50, cacheWithConfig.config().getExpireAfterWriteInMillis());

MultiLevelCache mc = (MultiLevelCache) getTarget(cacheWithConfig);
Cache localCache = getTarget(mc.caches()[0]);
Cache remoteCache = getTarget(mc.caches()[1]);
Assert.assertTrue(localCache instanceof LinkedHashMapCache);
Assert.assertTrue(remoteCache instanceof MockRemoteCache);
EmbeddedCacheConfig localConfig = (EmbeddedCacheConfig) localCache.config();
ExternalCacheConfig remoteConfig = (ExternalCacheConfig) remoteCache.config();
Assert.assertEquals(50, localConfig.getExpireAfterWriteInMillis());
Assert.assertEquals(50, remoteConfig.getExpireAfterWriteInMillis());
Assert.assertEquals(10, localConfig.getLimit());
Assert.assertEquals(JavaValueEncoder.class, remoteConfig.getValueEncoder().getClass());
Assert.assertTrue(remoteConfig.getValueDecoder() instanceof JavaValueDecoder);
Assert.assertSame(KeyConvertor.NONE_INSTANCE, localConfig.getKeyConvertor());
Assert.assertSame(KeyConvertor.NONE_INSTANCE, remoteConfig.getKeyConvertor());

}

private void testCacheWithLocalExpire() {
MultiLevelCacheConfig<?,?> config = (MultiLevelCacheConfig) cacheWithLocalExpire_1.config();
Assert.assertTrue(config.isUseExpireOfSubCache());
Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
Assert.assertEquals(1000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());

config = (MultiLevelCacheConfig) cacheWithLocalExpire_2.config();
Assert.assertFalse(config.isUseExpireOfSubCache());
Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
Assert.assertEquals(2000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());

Assert.assertEquals(2000, cacheWithLocalExpire_3.config().getExpireAfterWriteInMillis());
}

private void runGeneralTest() throws Exception {
super.cache = this.cache1;
super.baseTest();
}
private int refreshCount;
private void refreshTest() throws Exception {
LoadingCacheTest.loadingCacheTest(cacheWithRefresh1, 0);
RefreshCacheTest.refreshCacheTest(cacheWithRefresh2, 200, 100);
RefreshCacheTest.computeIfAbsentTest(cacheWithRefresh2);

cacheWithRefresh3.config().setLoader((k) -> refreshCount++);
cacheWithRefresh3.put("K1", "V1");
Assert.assertEquals("V1", cacheWithRefresh3.get("K1"));
Thread.sleep((long) (cacheWithRefresh3.config().getRefreshPolicy().getRefreshMillis() * 1.5));
Assert.assertEquals(0, cacheWithRefresh3.get("K1"));

cacheWithRefresh1.close();
cacheWithRefresh2.close();
cacheWithRefresh3.close();
}

private void cacheWithoutConvertorTest() {
DynamicQuery q1 = new DynamicQuery();
Expand Down Expand Up @@ -236,24 +201,33 @@ private void cacheWithoutConvertorTest() {
Assert.assertEquals(CacheResultCode.SUCCESS, cacheWithoutConvertor.GET(dqwe3).getResultCode());
}

private int refreshCount;
private void refreshTest() throws Exception {
LoadingCacheTest.loadingCacheTest(cacheWithRefresh1, 0);
RefreshCacheTest.refreshCacheTest(cacheWithRefresh2, 200, 100);
RefreshCacheTest.computeIfAbsentTest(cacheWithRefresh2);
private void testCacheWithLocalExpire() {
testCache1Config();
testCache2Config();
testCache3Config();
}

cacheWithRefresh3.config().setLoader((k) -> refreshCount++);
cacheWithRefresh3.put("K1", "V1");
Assert.assertEquals("V1", cacheWithRefresh3.get("K1"));
Thread.sleep((long) (cacheWithRefresh3.config().getRefreshPolicy().getRefreshMillis() * 1.5));
Assert.assertEquals(0, cacheWithRefresh3.get("K1"));
private void testCache1Config() {
MultiLevelCacheConfig<?, ?> config = (MultiLevelCacheConfig<?, ?>) cacheWithLocalExpire_1.config();
Assert.assertTrue(config.isUseExpireOfSubCache());
Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
Assert.assertEquals(1000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());
}

cacheWithRefresh1.close();
cacheWithRefresh2.close();
cacheWithRefresh3.close();
private void testCache2Config() {
MultiLevelCacheConfig<?, ?> config = (MultiLevelCacheConfig<?, ?>) cacheWithLocalExpire_2.config();
Assert.assertFalse(config.isUseExpireOfSubCache());
Assert.assertEquals(2000, config.getExpireAfterWriteInMillis());
Assert.assertEquals(2000, config.getCaches().get(0).config().getExpireAfterWriteInMillis());
Assert.assertEquals(2000, config.getCaches().get(1).config().getExpireAfterWriteInMillis());
}

private void testCache3Config() {
Assert.assertEquals(2000, cacheWithLocalExpire_3.config().getExpireAfterWriteInMillis());
}

}
}


}
Loading