2424import com .jarvis .cache .serializer .ISerializer ;
2525import com .jarvis .cache .to .AutoLoadConfig ;
2626import com .jarvis .cache .to .AutoLoadTO ;
27- import com .jarvis .cache .to .CacheConfigTO ;
2827import com .jarvis .cache .to .CacheKeyTO ;
2928import com .jarvis .cache .to .CacheWrapper ;
3029import 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 ) {// 同步最后加载时间
0 commit comments