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 32b4f63

Browse files
committed
增加mset, mget
1 parent ad5d5c8 commit 32b4f63

17 files changed

Lines changed: 661 additions & 235 deletions

‎pom.xml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.github.qiujiayu</groupId>
44
<artifactId>autoload-cache</artifactId>
5-
<version>7.0.0</version>
5+
<version>7.0.1-SNAPSHOT</version>
66
<packaging>jar</packaging>
77
<name>AutoLoadCache</name>
88
<description>Use AOP and Annotation manage cache.</description>
@@ -35,6 +35,7 @@
3535
<groupId>org.slf4j</groupId>
3636
<artifactId>slf4j-api</artifactId>
3737
<version>1.7.23</version>
38+
<optional>true</optional>
3839
</dependency>
3940
<dependency>
4041
<groupId>org.apache.commons</groupId>
@@ -89,6 +90,7 @@
8990
<artifactId>lombok</artifactId>
9091
<version>1.16.8</version>
9192
<scope>compile</scope>
93+
<optional>true</optional>
9294
</dependency>
9395
<dependency>
9496
<groupId>com.caucho</groupId>

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.jarvis.cache;
22

3-
import java.lang.reflect.Method;
4-
import java.util.Set;
5-
63
import com.jarvis.cache.annotation.LocalCache;
74
import com.jarvis.cache.exception.CacheCenterConnectionException;
85
import com.jarvis.cache.script.AbstractScriptParser;
96
import com.jarvis.cache.to.CacheKeyTO;
107
import com.jarvis.cache.to.CacheWrapper;
118
import com.jarvis.cache.to.LocalCacheWrapper;
12-
139
import lombok.extern.slf4j.Slf4j;
1410

11+
import java.lang.reflect.Method;
12+
import java.util.Map;
13+
import java.util.Set;
14+
1515
/**
1616
* 组合多种缓存管理方案,本地保存短期缓存,远程保存长期缓存
17-
*
17+
*
1818
* @author gongqin
1919
* @version 2016年6月8日
2020
*/
@@ -45,9 +45,8 @@ public ComboCacheManager(ICacheManager localCache, ICacheManager remoteCache, Ab
4545
@Override
4646
public void setCache(CacheKeyTO cacheKey, CacheWrapper<Object> result, Method method)
4747
throws CacheCenterConnectionException {
48-
LocalCache lCache = null;
4948
if (method.isAnnotationPresent(LocalCache.class)) {
50-
lCache = method.getAnnotation(LocalCache.class);
49+
LocalCachelCache = method.getAnnotation(LocalCache.class);
5150
setLocalCache(lCache, cacheKey, result, method);
5251
if (lCache.localOnly()) {// 只本地缓存
5352
return;
@@ -56,6 +55,20 @@ public void setCache(CacheKeyTO cacheKey, CacheWrapper<Object> result, Method me
5655
remoteCache.setCache(cacheKey, result, method);
5756
}
5857

58+
@Override
59+
public void mset(Method method, MSetParam... params) throws CacheCenterConnectionException {
60+
if (method.isAnnotationPresent(LocalCache.class)) {
61+
LocalCache lCache = method.getAnnotation(LocalCache.class);
62+
for (MSetParam param : params) {
63+
setLocalCache(lCache, param.getCacheKey(), param.getResult(), method);
64+
}
65+
if (lCache.localOnly()) {// 只本地缓存
66+
return;
67+
}
68+
}
69+
remoteCache.mset(method, params);
70+
}
71+
5972
private void setLocalCache(LocalCache lCache, CacheKeyTO cacheKey, CacheWrapper<Object> result, Method method) {
6073
try {
6174
LocalCacheWrapper<Object> localResult = new LocalCacheWrapper<Object>();
@@ -103,6 +116,11 @@ public CacheWrapper<Object> get(CacheKeyTO key, Method method) throws CacheCente
103116
return result;
104117
}
105118

119+
@Override
120+
public Map<CacheKeyTO, CacheWrapper<Object>> mget(final Method method, final CacheKeyTO... keys) throws CacheCenterConnectionException {
121+
return remoteCache.mget(method, keys);
122+
}
123+
106124
@Override
107125
public void delete(Set<CacheKeyTO> keys) throws CacheCenterConnectionException {
108126
localCache.delete(keys);

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
package com.jarvis.cache;
22

3-
import java.lang.reflect.Method;
4-
import java.util.List;
5-
import java.util.Set;
6-
73
import com.jarvis.cache.exception.CacheCenterConnectionException;
84
import com.jarvis.cache.to.CacheKeyTO;
95
import com.jarvis.cache.to.CacheWrapper;
106

7+
import java.lang.reflect.Method;
8+
import java.util.Map;
9+
import java.util.Set;
10+
1111
/**
1212
* 缓存管理
13-
*
13+
*
1414
* @author jiayu.qiu
1515
*/
1616
public interface ICacheManager {
1717

18+
/**
19+
* 当过期时间为0时,说明永不过期
20+
*/
21+
int NEVER_EXPIRE = 0;
22+
1823
/**
1924
* 往缓存写数据
20-
*
25+
*
2126
* @param cacheKey 缓存Key
22-
* @param result 缓存数据
23-
* @param method Method
27+
* @param result 缓存数据
28+
* @param method Method
2429
* @throws CacheCenterConnectionException 缓存异常
2530
*/
2631
void setCache(final CacheKeyTO cacheKey, final CacheWrapper<Object> result, final Method method) throws CacheCenterConnectionException;
2732

2833
/**
2934
* 往缓存写数据
3035
*
31-
* @param params 缓存Key 和 缓存数据
3236
* @param method Method
37+
* @param params 缓存Key 和 缓存数据
3338
* @throws CacheCenterConnectionException 缓存异常
3439
*/
35-
//void mset(final MSetParam[] params, final Method method) throws CacheCenterConnectionException;
40+
void mset(final Methodmethod, final MSetParam... params) throws CacheCenterConnectionException;
3641

3742
/**
3843
* 根据缓存Key获得缓存中的数据
39-
*
40-
* @param key 缓存key
44+
*
45+
* @param key 缓存key
4146
* @param method Method
4247
* @return 缓存数据
4348
* @throws CacheCenterConnectionException 缓存异常
@@ -46,17 +51,16 @@ public interface ICacheManager {
4651

4752
/**
4853
* 根据缓存Key获得缓存中的数据
49-
*
50-
* @param keys 缓存keys
5154
* @param method Method
52-
* @return 缓存数据
55+
* @param keys 缓存keys
56+
* @return 缓存数据 返回值必须与keys 一一对应
5357
* @throws CacheCenterConnectionException 缓存异常
5458
*/
55-
//List<CacheWrapper<Object>> mget(final CacheKeyTO[] keys, final Method method) throws CacheCenterConnectionException;
59+
Map<CacheKeyTO, CacheWrapper<Object>> mget(final Methodmethod, final CacheKeyTO... keys) throws CacheCenterConnectionException;
5660

5761
/**
5862
* 删除缓存
59-
*
63+
*
6064
* @param keys 缓存keys
6165
* @throws CacheCenterConnectionException 缓存异常
6266
*/

‎src/main/java/com/jarvis/cache/map/MapCacheManager.java‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jarvis.cache.map;
22

33
import com.jarvis.cache.ICacheManager;
4+
import com.jarvis.cache.MSetParam;
45
import com.jarvis.cache.clone.ICloner;
56
import com.jarvis.cache.exception.CacheCenterConnectionException;
67
import com.jarvis.cache.to.AutoLoadConfig;
@@ -11,6 +12,8 @@
1112

1213
import java.lang.ref.SoftReference;
1314
import java.lang.reflect.Method;
15+
import java.util.HashMap;
16+
import java.util.Map;
1417
import java.util.Set;
1518
import java.util.concurrent.ConcurrentHashMap;
1619

@@ -70,7 +73,7 @@ public MapCacheManager(AutoLoadConfig config, ICloner cloner) {
7073
}
7174

7275
public MapCacheManager(AutoLoadConfig config, ICloner cloner, int initSize) {
73-
this.cache = new ConcurrentHashMap<String, Object>(initSize);
76+
this.cache = new ConcurrentHashMap<>(initSize);
7477
this.config = config;
7578
this.config.setCheckFromCacheBeforeLoad(false);
7679
this.cloner = cloner;
@@ -103,7 +106,7 @@ public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> res
103106
return;
104107
}
105108
String cacheKey = cacheKeyTO.getCacheKey();
106-
if (null == cacheKey || cacheKey.length() == 0) {
109+
if (null == cacheKey || cacheKey.isEmpty()) {
107110
return;
108111
}
109112
CacheWrapper<Object> value = null;
@@ -119,7 +122,7 @@ public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> res
119122
}
120123
SoftReference<CacheWrapper<Object>> reference = new SoftReference<CacheWrapper<Object>>(value);
121124
String hfield = cacheKeyTO.getHfield();
122-
if (null == hfield || hfield.length() == 0) {
125+
if (null == hfield || hfield.isEmpty()) {
123126
cache.put(cacheKey, reference);
124127
} else {
125128
Object tmpObj = cache.get(cacheKey);
@@ -145,6 +148,16 @@ public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> res
145148
this.changeListener.cacheChange();
146149
}
147150

151+
@Override
152+
public void mset(final Method method, final MSetParam... params) throws CacheCenterConnectionException {
153+
if (null == params || params.length == 0) {
154+
return;
155+
}
156+
for (MSetParam param : params) {
157+
this.setCache(param.getCacheKey(), param.getResult(), method);
158+
}
159+
}
160+
148161
@SuppressWarnings("unchecked")
149162
@Override
150163
public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, final Method method)
@@ -153,7 +166,7 @@ public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, final Method method
153166
return null;
154167
}
155168
String cacheKey = cacheKeyTO.getCacheKey();
156-
if (null == cacheKey || cacheKey.length() == 0) {
169+
if (null == cacheKey || cacheKey.isEmpty()) {
157170
return null;
158171
}
159172
Object obj = cache.get(cacheKey);
@@ -162,7 +175,7 @@ public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, final Method method
162175
}
163176
String hfield = cacheKeyTO.getHfield();
164177
CacheWrapper<Object> value = null;
165-
if (null == hfield || hfield.length() == 0) {
178+
if (null == hfield || hfield.isEmpty()) {
166179
if (obj instanceof SoftReference) {
167180
SoftReference<CacheWrapper<Object>> reference = (SoftReference<CacheWrapper<Object>>) obj;
168181
if (null != reference) {
@@ -200,6 +213,19 @@ public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, final Method method
200213
return value;
201214
}
202215

216+
@Override
217+
public Map<CacheKeyTO, CacheWrapper<Object>> mget(final Method method, final CacheKeyTO... keys) throws CacheCenterConnectionException {
218+
if (null == keys || keys.length == 0) {
219+
return null;
220+
}
221+
int len = keys.length;
222+
Map<CacheKeyTO, CacheWrapper<Object>> res = new HashMap<>(len);
223+
for (int i = 0; i < len; i++) {
224+
res.put(keys[i], get(keys[i], method));
225+
}
226+
return res;
227+
}
228+
203229
@SuppressWarnings("unchecked")
204230
@Override
205231
public void delete(Set<CacheKeyTO> keys) throws CacheCenterConnectionException {

‎src/main/java/com/jarvis/cache/memcache/MemcachedCacheManager.java‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.jarvis.cache.memcache;
22

3-
import java.lang.reflect.Method;
4-
import java.util.Set;
5-
63
import com.jarvis.cache.ICacheManager;
4+
import com.jarvis.cache.MSetParam;
75
import com.jarvis.cache.exception.CacheCenterConnectionException;
86
import com.jarvis.cache.to.CacheKeyTO;
97
import com.jarvis.cache.to.CacheWrapper;
10-
118
import lombok.extern.slf4j.Slf4j;
129
import net.spy.memcached.MemcachedClient;
1310

11+
import java.lang.reflect.Method;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Set;
15+
1416
/**
1517
* memcache缓存管理
16-
*
18+
*
1719
* @author: jiayu.qiu
1820
*/
1921
@Slf4j
@@ -30,7 +32,7 @@ public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> res
3032
return;
3133
}
3234
String cacheKey = cacheKeyTO.getCacheKey();
33-
if (null == cacheKey || cacheKey.length() == 0) {
35+
if (null == cacheKey || cacheKey.isEmpty()) {
3436
return;
3537
}
3638
String hfield = cacheKeyTO.getHfield();
@@ -42,14 +44,24 @@ public void setCache(final CacheKeyTO cacheKeyTO, final CacheWrapper<Object> res
4244
}
4345
}
4446

47+
@Override
48+
public void mset(final Method method, final MSetParam... params) throws CacheCenterConnectionException {
49+
if (null == params || params.length == 0) {
50+
return;
51+
}
52+
for (MSetParam param : params) {
53+
this.setCache(param.getCacheKey(), param.getResult(), method);
54+
}
55+
}
56+
4557
@SuppressWarnings("unchecked")
4658
@Override
4759
public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, Method method) throws CacheCenterConnectionException {
4860
if (null == cacheKeyTO) {
4961
return null;
5062
}
5163
String cacheKey = cacheKeyTO.getCacheKey();
52-
if (null == cacheKey || cacheKey.length() == 0) {
64+
if (null == cacheKey || cacheKey.isEmpty()) {
5365
return null;
5466
}
5567
String hfield = cacheKeyTO.getHfield();
@@ -59,18 +71,40 @@ public CacheWrapper<Object> get(final CacheKeyTO cacheKeyTO, Method method) thro
5971
return (CacheWrapper<Object>) memcachedClient.get(cacheKey);
6072
}
6173

74+
@Override
75+
public Map<CacheKeyTO, CacheWrapper<Object>> mget(final Method method, final CacheKeyTO... keys) throws CacheCenterConnectionException {
76+
if (null == keys || keys.length == 0) {
77+
return null;
78+
}
79+
String[] k = new String[keys.length];
80+
for (int i = 0; i < keys.length; i++) {
81+
k[i] = keys[i].getCacheKey();
82+
}
83+
Map<String, Object> values = memcachedClient.getBulk(k);
84+
if (null == values || values.isEmpty()) {
85+
return null;
86+
}
87+
88+
int len = keys.length;
89+
Map<CacheKeyTO, CacheWrapper<Object>> res = new HashMap<>(len);
90+
for (int i = 0; i < len; i++) {
91+
res.put(keys[i], (CacheWrapper<Object>) values.get(keys[i].getCacheKey()));
92+
}
93+
return res;
94+
}
95+
6296
@Override
6397
public void delete(Set<CacheKeyTO> keys) throws CacheCenterConnectionException {
6498
if (null == memcachedClient || null == keys || keys.isEmpty()) {
6599
return;
66100
}
67101
String hfield;
68-
for(CacheKeyTO cacheKeyTO: keys) {
102+
for(CacheKeyTO cacheKeyTO: keys) {
69103
if (null == cacheKeyTO) {
70104
continue;
71105
}
72106
String cacheKey = cacheKeyTO.getCacheKey();
73-
if (null == cacheKey || cacheKey.length() == 0) {
107+
if (null == cacheKey || cacheKey.isEmpty()) {
74108
continue;
75109
}
76110
hfield = cacheKeyTO.getHfield();

0 commit comments

Comments
(0)

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