Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34faf57

Browse files
author
qiujiayu
committed
增加CacheOpType.LOAD,类型。并通过三种方式获取CacheOpType.1. Cache注解中获取;2.
从ThreadLocal中获取;3. 从参数中获取;
1 parent d454a19 commit 34faf57

3 files changed

Lines changed: 81 additions & 40 deletions

File tree

‎src/main/java/com/jarvis/cache/CacheHandler.java‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.jarvis.cache.serializer.ISerializer;
2525
import com.jarvis.cache.to.AutoLoadConfig;
2626
import com.jarvis.cache.to.AutoLoadTO;
27-
import com.jarvis.cache.to.CacheConfigTO;
2827
import com.jarvis.cache.to.CacheKeyTO;
2928
import com.jarvis.cache.to.CacheWrapper;
3029
import com.jarvis.cache.to.ProcessingTO;
@@ -70,6 +69,13 @@ public CacheHandler(ICacheManager cacheManager, AbstractScriptParser scriptParse
7069
refreshHandler=new RefreshHandler(this, config);
7170
}
7271

72+
/**
73+
* 从数据源中获取最新数据,并写入缓存。注意:这里不使用"拿来主义"机制,是因为当前可能是更新数据的方法。
74+
* @param pjp CacheAopProxyChain
75+
* @param cache Cache注解
76+
* @return 最新数据
77+
* @throws Throwable 异常
78+
*/
7379
private Object writeOnly(CacheAopProxyChain pjp, Cache cache) throws Throwable {
7480
DataLoaderFactory factory=DataLoaderFactory.getInstance();
7581
DataLoader dataLoader=factory.getDataLoader();
@@ -99,6 +105,36 @@ private Object writeOnly(CacheAopProxyChain pjp, Cache cache) throws Throwable {
99105
return result;
100106
}
101107

108+
/**
109+
* 获取CacheOpType,从三个地方获取:<br>
110+
* 1. Cache注解中获取;<br>
111+
* 2. 从ThreadLocal中获取;<br>
112+
* 3. 从参数中获取;<br>
113+
* 上面三者的优先级:从低到高。
114+
* @param cache 注解
115+
* @param arguments 参数
116+
* @return CacheOpType
117+
*/
118+
private CacheOpType getCacheOpType(Cache cache, Object[] arguments) {
119+
CacheOpType opType=cache.opType();
120+
CacheOpType _tmp=CacheHelper.getCacheOpType();
121+
if(null != _tmp) {
122+
opType=_tmp;
123+
}
124+
if(null != arguments && arguments.length > 0) {
125+
for(Object tmp: arguments) {
126+
if(null != tmp && tmp instanceof CacheOpType) {
127+
opType=(CacheOpType)tmp;
128+
break;
129+
}
130+
}
131+
}
132+
if(null == opType) {
133+
opType=CacheOpType.READ_WRITE;
134+
}
135+
return opType;
136+
}
137+
102138
/**
103139
* 处理@Cache 拦截
104140
* @param pjp 切面
@@ -107,36 +143,36 @@ private Object writeOnly(CacheAopProxyChain pjp, Cache cache) throws Throwable {
107143
* @throws Exception 异常
108144
*/
109145
public Object proceed(CacheAopProxyChain pjp, Cache cache) throws Throwable {
110-
logger.debug("CacheHandler.proceed-->" + pjp.getTargetClass().getName() + "." + pjp.getMethod().getName());
111-
if(null != cache.opType() && cache.opType() == CacheOpType.WRITE) {// 更新缓存操作
146+
Object[] arguments=pjp.getArgs();
147+
CacheOpType opType=getCacheOpType(cache, arguments);
148+
logger.debug("CacheHandler.proceed-->" + pjp.getTargetClass().getName() + "." + pjp.getMethod().getName() + "--" + opType.name());
149+
150+
if(opType == CacheOpType.WRITE) {
112151
return writeOnly(pjp, cache);
152+
} else if(opType == CacheOpType.LOAD) {
153+
return getData(pjp);
113154
}
114-
CacheConfigTO config=CacheHelper.getLocalConfig();
115-
if(config != null) {
116-
CacheHelper.clearLocalConfig();
117-
if(!config.isCacheAble()) {
118-
return getData(pjp);
119-
}
120-
}
121-
Object[] arguments=pjp.getArgs();
155+
122156
if(!scriptParser.isCacheable(cache, arguments)) {// 如果不进行缓存,则直接返回数据
123157
return getData(pjp);
124158
}
159+
125160
CacheKeyTO cacheKey=getCacheKey(pjp, cache);
126161
if(null == cacheKey) {
127162
return getData(pjp);
128163
}
129164
Method method=pjp.getMethod();
130-
// Type returnType=method.getGenericReturnType();
131165
CacheWrapper<Object> cacheWrapper=null;
132166
try {
133167
cacheWrapper=this.get(cacheKey, method, arguments);// 从缓存中获取数据
134168
} catch(Exception ex) {
135169
logger.error(ex.getMessage(), ex);
136170
}
137-
if(null != cache.opType() && cache.opType() == CacheOpType.READ_ONLY) {// 如果是只读,则直接返回
171+
172+
if(opType == CacheOpType.READ_ONLY) {
138173
return null == cacheWrapper ? null : cacheWrapper.getCacheObject();
139174
}
175+
140176
if(null != cacheWrapper && !cacheWrapper.isExpired()) {
141177
AutoLoadTO autoLoadTO=autoLoadHandler.putIfAbsent(cacheKey, pjp, cache, cacheWrapper);
142178
if(null != autoLoadTO) {// 同步最后加载时间

‎src/main/java/com/jarvis/cache/CacheHelper.java‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,36 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6-
import com.jarvis.cache.to.CacheConfigTO;
76
import com.jarvis.cache.to.CacheKeyTO;
7+
import com.jarvis.cache.type.CacheOpType;
88

99
public class CacheHelper {
1010

11-
private static final ThreadLocal<CacheConfigTO> CONFIG=new ThreadLocal<CacheConfigTO>();
11+
private static final ThreadLocal<CacheOpType> OP_TYPE=new ThreadLocal<CacheOpType>();
1212

1313
private static final ThreadLocal<Set<CacheKeyTO>> DELETE_CACHE_KEYS=new ThreadLocal<Set<CacheKeyTO>>();
1414

15-
publicstaticCacheConfigTOgetLocalConfig() {
16-
returnCONFIG.get();
17-
}
18-
19-
private static voidsetLocalConfig(CacheConfigTOconfig) {
20-
CONFIG.set(config);
15+
/**
16+
* 获取CacheOpType
17+
* @return
18+
*/
19+
public static CacheOpTypegetCacheOpType() {
20+
returnOP_TYPE.get();
2121
}
2222

2323
/**
24-
* 移除本地变量
24+
* 设置CacheOpType
25+
* @param config
2526
*/
26-
public static void clearLocalConfig() {
27-
CONFIG.remove();
27+
public static void setCacheOpType(CacheOpTypeopType) {
28+
OP_TYPE.set(opType);
2829
}
2930

30-
public static CacheConfigTO setCacheAble(boolean cacheAble) {
31-
CacheConfigTO config=getLocalConfig();
32-
if(null == config) {
33-
config=new CacheConfigTO();
34-
}
35-
config.setCacheAble(cacheAble);
36-
setLocalConfig(config);
37-
return config;
31+
/**
32+
* 移除CacheOpType
33+
*/
34+
public static void clearCacheOpType() {
35+
OP_TYPE.remove();
3836
}
3937

4038
public static void initDeleteCacheKeysSet() {

‎src/main/java/com/jarvis/cache/type/CacheOpType.java‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
*/
77
public enum CacheOpType {
88
/**
9-
* 读写缓存操
9+
* 读写缓存操:如果缓存中有数据,则使用缓存中的数据,如果缓存中没有数据,则加载数据,并写入缓存。
1010
*/
11-
READ_WRITE, /**
12-
* 只往缓存写数据,不从缓存中读数据
13-
*/
14-
WRITE, /**
15-
* 只从缓存中读取,用于其它地方往缓存写,这里只读的场景。
16-
*/
17-
READ_ONLY;
11+
READ_WRITE, //
12+
/**
13+
* 从数据源中加载最新的数据,并写入缓存。
14+
*/
15+
WRITE, //
16+
/**
17+
* 只从缓存中读取,用于其它地方往缓存写,这里只读的场景。
18+
*/
19+
READ_ONLY, //
20+
/**
21+
* 只从数据源加载数据,不读取缓存中的数据,也不写入缓存。
22+
*/
23+
LOAD, //
24+
;
1825
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /