Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit b6da017

Browse files
committed
docs: add java doc
1 parent 1e2fcae commit b6da017

File tree

4 files changed

+219
-4
lines changed

4 files changed

+219
-4
lines changed

src/main/java/org/code4everything/boot/cache/AbstractCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected <T> T convert2(Object value, Callable<T> valueLoader) {
5757
return valueLoader.call();
5858
} catch (Exception e) {
5959
if (BootConfig.isDebug()) {
60-
throw new RuntimeException("value loader error");
60+
throw new RuntimeException("value loader error, details: " + e.getMessage(), e);
6161
}
6262
return null;
6363
}

src/main/java/org/code4everything/boot/cache/BootCacheManager.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import cn.hutool.core.collection.ConcurrentHashSet;
44
import cn.hutool.core.util.ObjectUtil;
5+
import cn.hutool.core.util.StrUtil;
6+
import org.code4everything.boot.base.constant.StringConsts;
57
import org.springframework.cache.Cache;
68
import org.springframework.cache.CacheManager;
79

@@ -19,12 +21,32 @@
1921
**/
2022
public class BootCacheManager implements CacheManager {
2123

24+
/**
25+
* 缓存名与缓存的映射
26+
*
27+
* @since 1.1.3
28+
*/
2229
protected final Map<String, Cache> cacheMap;
2330

31+
/**
32+
* 缓存名集合
33+
*
34+
* @since 1.1.3
35+
*/
2436
protected final Set<String> cacheNames;
2537

38+
/**
39+
* 缓存创建者
40+
*
41+
* @since 1.1.3
42+
*/
2643
protected final CacheCreator cacheCreator;
2744

45+
/**
46+
* 是否可以动态的创建缓存
47+
*
48+
* @since 1.1.3
49+
*/
2850
protected final boolean dynamic;
2951

3052
public BootCacheManager(CacheCreator cacheCreator) {
@@ -76,33 +98,104 @@ public Collection<String> getCacheNames() {
7698
return cacheNames;
7799
}
78100

101+
/**
102+
* 请求一个缓存必须存在
103+
*
104+
* @param cacheName 缓存名
105+
*
106+
* @return 对应的缓存
107+
*
108+
* @since 1.1.3
109+
*/
79110
public Cache requireCache(String cacheName) {
80111
Cache cache = getCache(cacheName);
81-
Objects.requireNonNull(cache, "cache '" + cacheName + "' has no config");
112+
Objects.requireNonNull(cache, "cache '" + cacheName + "' not exists");
82113
return cache;
83114
}
84115

116+
/**
117+
* 新增对象缓存
118+
*
119+
* @param cacheName 缓存名
120+
* @param key 键
121+
* @param value 值
122+
*
123+
* @since 1.1.3
124+
*/
85125
public void putVal(String cacheName, String key, Object value) {
86126
Cache cache = getCache(cacheName);
87127
if (ObjectUtil.isNotNull(cache)) {
88128
cache.put(key, value);
89129
}
90130
}
91131

132+
/**
133+
* 添加一个对象缓存,如果缓存是列表的话
134+
*
135+
* @param cacheName 缓存名
136+
* @param key 键
137+
* @param value 值
138+
*
139+
* @since 1.1.3
140+
*/
141+
@SuppressWarnings("unchecked")
142+
public void addVal(String cacheName, String key, Object value) {
143+
Cache cache = getCache(cacheName);
144+
if (ObjectUtil.isNotNull(cache)) {
145+
Object object = cache.get(key);
146+
if (object instanceof Collection) {
147+
((Collection) object).add(value);
148+
}
149+
}
150+
}
151+
152+
/**
153+
* 删除一个对象缓存,works on all caches if cacheName==null || cacheName=="" || cacheName=="*"
154+
*
155+
* @param cacheName 缓存名
156+
* @param key 键
157+
*
158+
* @since 1.1.3
159+
*/
92160
public void delVal(String cacheName, String key) {
161+
if (StrUtil.isEmpty(cacheName) || StringConsts.Sign.STAR.equals(cacheName)) {
162+
cacheMap.values().forEach(cache -> cache.evict(key));
163+
return;
164+
}
93165
Cache cache = getCache(cacheName);
94166
if (ObjectUtil.isNotNull(cache)) {
95167
cache.evict(key);
96168
}
97169
}
98170

171+
/**
172+
* 删除所有对象缓存,works on all caches if cacheName==null || cacheName=="" || cacheName=="*"
173+
*
174+
* @param cacheName 缓存名
175+
*
176+
* @since 1.1.3
177+
*/
99178
public void removeAll(String cacheName) {
179+
if (StrUtil.isEmpty(cacheName) || StringConsts.Sign.STAR.equals(cacheName)) {
180+
cacheMap.values().forEach(Cache::clear);
181+
return;
182+
}
100183
Cache cache = getCache(cacheName);
101184
if (ObjectUtil.isNotNull(cache)) {
102185
cache.clear();
103186
}
104187
}
105188

189+
/**
190+
* 获取缓存的对象
191+
*
192+
* @param cacheName 缓存名
193+
* @param key 键
194+
*
195+
* @return 缓存的对象
196+
*
197+
* @since 1.1.3
198+
*/
106199
public Object getVal(String cacheName, String key) {
107200
Cache cache = getCache(cacheName);
108201
if (ObjectUtil.isNotNull(cache)) {

src/main/java/org/code4everything/boot/cache/CacheUtils.java

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,62 @@ public class CacheUtils {
2424

2525
private CacheUtils() {}
2626

27-
2827
// -------------------------------------------Redis-----------------------------------------------------------------
2928

29+
/**
30+
* 新建缓存管理器
31+
*
32+
* @param redisCacheCreatorMap 缓存名与缓存创建者的映射
33+
*
34+
* @return 缓存管理器
35+
*
36+
* @since 1.1.3
37+
*/
3038
public static RedisCacheManager newRedisCacheManager(Map<String, RedisCacheCreator> redisCacheCreatorMap) {
3139
return newRedisCacheManager(redisCacheCreatorMap, null);
3240
}
3341

42+
/**
43+
* 新建缓存管理器
44+
*
45+
* @param redisCacheCreatorMap 缓存名与缓存创建者的映射
46+
* @param defaultRedisCacheCreator 缓存创建者
47+
*
48+
* @return 缓存管理器
49+
*
50+
* @since 1.1.3
51+
*/
3452
public static RedisCacheManager newRedisCacheManager(Map<String, RedisCacheCreator> redisCacheCreatorMap,
3553
RedisCacheCreator defaultRedisCacheCreator) {
3654
Collection<RedisCache> caches = new ArrayList<>();
3755
redisCacheCreatorMap.forEach((k, v) -> caches.add(v.createCache(k)));
3856
return new RedisCacheManager(caches, defaultRedisCacheCreator);
3957
}
4058

59+
/**
60+
* 新建缓存管理器
61+
*
62+
* @param redisCacheCreator 缓存创建者
63+
* @param names 缓存名集合
64+
*
65+
* @return 缓存管理器
66+
*
67+
* @since 1.1.3
68+
*/
4169
public static RedisCacheManager newRedisCacheManager(RedisCacheCreator redisCacheCreator, String... names) {
4270
return newRedisCacheManager(redisCacheCreator, Arrays.asList(names));
4371
}
4472

73+
/**
74+
* 新建缓存管理器
75+
*
76+
* @param redisCacheCreator 缓存创建者
77+
* @param names 缓存名集合
78+
*
79+
* @return 缓存管理器
80+
*
81+
* @since 1.1.3
82+
*/
4583
public static RedisCacheManager newRedisCacheManager(RedisCacheCreator redisCacheCreator,
4684
Collection<String> names) {
4785
if (CollUtil.isEmpty(names)) {
@@ -54,21 +92,60 @@ public static RedisCacheManager newRedisCacheManager(RedisCacheCreator redisCach
5492

5593
// ---------------------------------------Guava---------------------------------------------------------------------
5694

95+
/**
96+
* 新建缓存管理器
97+
*
98+
* @param cacheBuilderMap 缓存名与缓存创建者的映射
99+
*
100+
* @return 缓存管理器
101+
*
102+
* @since 1.1.3
103+
*/
57104
public static GuavaCacheManager newGuavaCacheManager(Map<String, CacheBuilder<Object, Object>> cacheBuilderMap) {
58105
return newGuavaCacheManager(cacheBuilderMap, null);
59106
}
60107

108+
/**
109+
* 新建缓存管理器
110+
*
111+
* @param cacheBuilderMap 缓存名与缓存创建者的映射
112+
* @param defaultCacheBuilder 缓存创建者
113+
*
114+
* @return 缓存管理器
115+
*
116+
* @since 1.1.3
117+
*/
61118
public static GuavaCacheManager newGuavaCacheManager(Map<String, CacheBuilder<Object, Object>> cacheBuilderMap,
62119
CacheBuilder<Object, Object> defaultCacheBuilder) {
63120
Collection<GuavaCache> caches = new ArrayList<>();
64121
cacheBuilderMap.forEach((k, v) -> caches.add(new GuavaCache(k, v.build())));
65122
return new GuavaCacheManager(caches, defaultCacheBuilder);
66123
}
67124

125+
/**
126+
* 新建缓存管理器
127+
*
128+
* @param cacheBuilder 缓存创建者
129+
* @param names 缓存名集合
130+
*
131+
* @return 缓存管理器
132+
*
133+
* @since 1.1.3
134+
*/
68135
public static GuavaCacheManager newGuavaCacheManager(CacheBuilder<Object, Object> cacheBuilder, String... names) {
69136
return newGuavaCacheManager(cacheBuilder, Arrays.asList(names));
70137
}
71138

139+
/**
140+
* 新建缓存管理器
141+
*
142+
* @param cacheBuilder 缓存创建者
143+
* @param names 缓存名集合
144+
*
145+
* @return 缓存管理器
146+
*
147+
* @since 1.1.3
148+
*/
72149
public static GuavaCacheManager newGuavaCacheManager(CacheBuilder<Object, Object> cacheBuilder,
73150
Collection<String> names) {
74151
if (CollUtil.isEmpty(names)) {
@@ -81,21 +158,60 @@ public static GuavaCacheManager newGuavaCacheManager(CacheBuilder<Object, Object
81158

82159
// --------------------------------------------Custom---------------------------------------------------------------
83160

161+
/**
162+
* 新建缓存管理器
163+
*
164+
* @param cacheCreatorMap 缓存名与缓存创建者的映射
165+
*
166+
* @return 缓存管理器
167+
*
168+
* @since 1.1.3
169+
*/
84170
public static BootCacheManager newCacheManager(Map<String, CacheCreator> cacheCreatorMap) {
85171
return newCacheManager(cacheCreatorMap, null);
86172
}
87173

174+
/**
175+
* 新建缓存管理器
176+
*
177+
* @param cacheCreatorMap 缓存名与缓存创建者的映射
178+
* @param defaultCacheCreator 缓存创建者
179+
*
180+
* @return 缓存管理器
181+
*
182+
* @since 1.1.3
183+
*/
88184
public static BootCacheManager newCacheManager(Map<String, CacheCreator> cacheCreatorMap,
89185
CacheCreator defaultCacheCreator) {
90186
Collection<Cache> caches = new ArrayList<>();
91187
cacheCreatorMap.forEach((k, v) -> caches.add(v.createCache(k)));
92188
return new BootCacheManager(caches, defaultCacheCreator);
93189
}
94190

191+
/**
192+
* 新建缓存管理器
193+
*
194+
* @param cacheCreator 缓存创建者
195+
* @param names 缓存名集合
196+
*
197+
* @return 缓存管理器
198+
*
199+
* @since 1.1.3
200+
*/
95201
public static BootCacheManager newCacheManager(CacheCreator cacheCreator, String... names) {
96202
return newCacheManager(cacheCreator, Arrays.asList(names));
97203
}
98204

205+
/**
206+
* 新建缓存管理器
207+
*
208+
* @param cacheCreator 缓存创建者
209+
* @param names 缓存名集合
210+
*
211+
* @return 缓存管理器
212+
*
213+
* @since 1.1.3
214+
*/
99215
public static BootCacheManager newCacheManager(CacheCreator cacheCreator, Collection<String> names) {
100216
if (CollUtil.isEmpty(names)) {
101217
return new BootCacheManager(cacheCreator);

src/main/java/org/code4everything/boot/cache/redis/RedisCacheManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.code4everything.boot.cache.redis;
22

33
import cn.hutool.core.util.ObjectUtil;
4+
import cn.hutool.core.util.StrUtil;
5+
import org.code4everything.boot.base.constant.StringConsts;
46
import org.code4everything.boot.cache.BootCacheManager;
57
import org.springframework.cache.Cache;
68

@@ -33,7 +35,7 @@ public RedisCacheManager(Collection<RedisCache> caches) {
3335
}
3436

3537
/**
36-
* 在某个时间点过期
38+
* 在某个时间点过期,works on all caches if cacheName==null || cacheName=="" || cacheName=="*"
3739
*
3840
* @param cacheName 缓存名
3941
* @param key 键
@@ -42,6 +44,10 @@ public RedisCacheManager(Collection<RedisCache> caches) {
4244
* @since 1.1.3
4345
*/
4446
public void expireAt(String cacheName, String key, Date date) {
47+
if (StrUtil.isEmpty(cacheName) || StringConsts.Sign.STAR.equals(cacheName)) {
48+
cacheMap.values().forEach(cache -> ((RedisCache) cache).expireAt(key, date));
49+
return;
50+
}
4551
Cache cache = getCache(cacheName);
4652
if (ObjectUtil.isNotNull(cache)) {
4753
((RedisCache) cache).expireAt(key, date);

0 commit comments

Comments
 (0)