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

Commit c481e0e

Browse files
committed
feat: remove object cache from collection
1 parent b6da017 commit c481e0e

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.code4everything.boot.cache;
22

3+
import cn.hutool.core.collection.CollUtil;
34
import cn.hutool.core.collection.ConcurrentHashSet;
45
import cn.hutool.core.util.ObjectUtil;
56
import cn.hutool.core.util.StrUtil;
@@ -168,6 +169,24 @@ public void delVal(String cacheName, String key) {
168169
}
169170
}
170171

172+
/**
173+
* 从缓存列表中移除缓存
174+
*
175+
* @param cacheName 缓存名称
176+
* @param key 键
177+
* @param removable 移除条件的回调
178+
* @param <T> 值类型
179+
*
180+
* @since 1.1.3
181+
*/
182+
public <T> void delVal(String cacheName, String key, CacheRemovable<T> removable) {
183+
Collection<T> collection = getValByType(cacheName, key);
184+
if (CollUtil.isEmpty(collection)) {
185+
return;
186+
}
187+
collection.removeIf(removable::shouldRemove);
188+
}
189+
171190
/**
172191
* 删除所有对象缓存,works on all caches if cacheName==null || cacheName=="" || cacheName=="*"
173192
*
@@ -204,4 +223,21 @@ public Object getVal(String cacheName, String key) {
204223
}
205224
return null;
206225
}
226+
227+
/**
228+
* 获取缓存的对象
229+
*
230+
* @param cacheName 缓存名
231+
* @param key 键
232+
* @param <T> 目标类型
233+
*
234+
* @return 缓存的对象
235+
*
236+
* @since 1.1.3
237+
*/
238+
@SuppressWarnings("unchecked")
239+
public <T> T getValByType(String cacheName, String key) {
240+
Object value = getVal(cacheName, key);
241+
return Objects.isNull(value) ? null : (T) value;
242+
}
207243
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.code4everything.boot.cache;
2+
3+
/**
4+
* 缓存列表移除接口
5+
*
6+
* @author pantao
7+
* @since 2019/6/18
8+
**/
9+
@FunctionalInterface
10+
public interface CacheRemovable<T> {
11+
12+
/**
13+
* 是否从列表中移除该元素
14+
*
15+
* @param value 是否移除的值
16+
*
17+
* @return 是否移除
18+
*
19+
* @since 1.1.3
20+
*/
21+
boolean shouldRemove(T value);
22+
}

0 commit comments

Comments
 (0)