1010
1111import java .lang .reflect .Method ;
1212import java .lang .reflect .Type ;
13- import java .util .Collection ;
14- import java .util .Map ;
15- import java .util .Set ;
13+ import java .util .*;
1614
1715/**
1816 * 组合多种缓存管理方案,本地保存短期缓存,远程保存长期缓存
@@ -63,7 +61,7 @@ public void mset(Method method, Collection<MSetParam> params) throws CacheCenter
6361 if (method .isAnnotationPresent (LocalCache .class )) {
6462 LocalCache lCache = method .getAnnotation (LocalCache .class );
6563 for (MSetParam param : params ) {
66- if (param == null ){
64+ if (param == null ){
6765 continue ;
6866 }
6967 setLocalCache (lCache , param .getCacheKey (), param .getResult (), method );
@@ -95,23 +93,18 @@ private void setLocalCache(LocalCache lCache, CacheKeyTO cacheKey, CacheWrapper<
9593
9694 @ Override
9795 public CacheWrapper <Object > get (CacheKeyTO key , Method method ) throws CacheCenterConnectionException {
98- String threadName = Thread .currentThread ().getName ();
99- // 如果是自动加载线程,则只从远程缓存获取。
100- if (threadName .startsWith (AutoLoadHandler .THREAD_NAME_PREFIX )) {
101- return remoteCache .get (key , method );
102- }
10396 LocalCache lCache = null ;
10497 if (method .isAnnotationPresent (LocalCache .class )) {
105- CacheWrapper <Object > result = localCache .get (key , method );
10698 lCache = method .getAnnotation (LocalCache .class );
99+ }
100+ String threadName = Thread .currentThread ().getName ();
101+ // 如果是自动加载线程,则只从远程缓存获取。
102+ if (null != lCache && !threadName .startsWith (AutoLoadHandler .THREAD_NAME_PREFIX )) {
103+ CacheWrapper <Object > result = localCache .get (key , method );
107104 if (null != result ) {
108105 if (result instanceof LocalCacheWrapper ) {
109106 LocalCacheWrapper <Object > localResult = (LocalCacheWrapper <Object >) result ;
110- CacheWrapper <Object > result2 = new CacheWrapper <Object >();
111- result2 .setCacheObject (localResult .getCacheObject ());
112- result2 .setExpire (localResult .getRemoteExpire ());
113- result2 .setLastLoadTime (localResult .getRemoteLastLoadTime ());
114- return result2 ;
107+ return new CacheWrapper <Object >(localResult .getCacheObject (), localResult .getRemoteExpire (), localResult .getRemoteLastLoadTime ());
115108 } else {
116109 return result ;
117110 }
@@ -127,7 +120,58 @@ public CacheWrapper<Object> get(CacheKeyTO key, Method method) throws CacheCente
127120
128121 @ Override
129122 public Map <CacheKeyTO , CacheWrapper <Object >> mget (final Method method , final Type returnType , final Set <CacheKeyTO > keys ) throws CacheCenterConnectionException {
130- return remoteCache .mget (method , returnType , keys );
123+ LocalCache lCache = null ;
124+ if (method .isAnnotationPresent (LocalCache .class )) {
125+ lCache = method .getAnnotation (LocalCache .class );
126+ } else {
127+ return remoteCache .mget (method , returnType , keys );
128+ }
129+ String threadName = Thread .currentThread ().getName ();
130+ Map <CacheKeyTO , CacheWrapper <Object >> all ;
131+ Map <CacheKeyTO , CacheWrapper <Object >> remoteResults = null ;
132+ if (!threadName .startsWith (AutoLoadHandler .THREAD_NAME_PREFIX )) {
133+ all = new HashMap <>(keys .size ());
134+ Map <CacheKeyTO , CacheWrapper <Object >> localResults = localCache .mget (method , returnType , keys );
135+ if (null != localResults && !localResults .isEmpty ()) {
136+ Iterator <Map .Entry <CacheKeyTO , CacheWrapper <Object >>> iterator = localResults .entrySet ().iterator ();
137+ while (iterator .hasNext ()) {
138+ Map .Entry <CacheKeyTO , CacheWrapper <Object >> item = iterator .next ();
139+ CacheWrapper <Object > result = item .getValue ();
140+ if (result instanceof LocalCacheWrapper ) {
141+ LocalCacheWrapper <Object > localResult = (LocalCacheWrapper <Object >) result ;
142+ CacheWrapper <Object > result2 = new CacheWrapper <Object >(localResult .getCacheObject (), localResult .getRemoteExpire (), localResult .getRemoteLastLoadTime ());
143+ all .put (item .getKey (), result2 );
144+ } else {
145+ all .put (item .getKey (), result );
146+ }
147+ }
148+ }
149+ if (all .size () < keys .size ()) {
150+ Set <CacheKeyTO > unCachekeys = new HashSet <>(keys .size () - all .size ());
151+ for (CacheKeyTO key : keys ) {
152+ if (!all .containsKey (key )) {
153+ unCachekeys .add (key );
154+ }
155+ }
156+ remoteResults = remoteCache .mget (method , returnType , keys );
157+ if (null != remoteResults && !remoteResults .isEmpty ()) {
158+ all .putAll (remoteResults );
159+ }
160+ }
161+ } else {
162+ remoteResults = remoteCache .mget (method , returnType , keys );
163+ all = remoteResults ;
164+ }
165+ 166+ if (null != remoteResults && !remoteResults .isEmpty ()) {
167+ // 放到本地缓存里
168+ Iterator <Map .Entry <CacheKeyTO , CacheWrapper <Object >>> iterator = remoteResults .entrySet ().iterator ();
169+ while (iterator .hasNext ()) {
170+ Map .Entry <CacheKeyTO , CacheWrapper <Object >> item = iterator .next ();
171+ setLocalCache (lCache , item .getKey (), item .getValue (), method );
172+ }
173+ }
174+ return all ;
131175 }
132176
133177 @ Override
0 commit comments