1 Star 3 Fork 0

清风/CommonUtils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CacheUtil.java 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
清风 提交于 2019-04-03 11:25 +08:00 . Create CacheUtil.java
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
public class CacheUtil {
public static Map<String, Cache> MAP = new ConcurrentHashMap();
public CacheUtil() {
}
public static Cache getCacheObj(String key) {
Cache cache = (Cache)MAP.get(key);
if (cache == null) {
Class var2 = CacheUtil.class;
synchronized(CacheUtil.class) {
MAP.put(key, CacheBuilder.newBuilder().expireAfterWrite(48L, TimeUnit.HOURS).maximumSize(5000L).weakValues().build());
}
return (Cache)MAP.get(key);
} else {
return cache;
}
}
public static void putObj(String key, String val, String type) {
String cacheKey = DigestUtils.md5Hex(val);
try {
getCacheObj(key).put(cacheKey, new String((type + val).getBytes("utf-8")));
} catch (UnsupportedEncodingException var5) {
;
}
}
public static void cleanTopic(String key) {
Cache cache = getCacheObj(key);
cache.cleanUp();
}
public static Collection<String> queryByKey(String key, String searchKey) {
Cache cache = getCacheObj(key);
ConcurrentMap<String, String> map = cache.asMap();
return StringUtils.isNotBlank(searchKey) ? (Collection)map.entrySet().stream().filter((f) -> {
return ((String)f.getValue()).contains(searchKey);
}).map((m) -> {
return (String)m.getValue();
}).collect(Collectors.toList()) : map.values();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/agp5050/CommonUtils.git
[email protected]:agp5050/CommonUtils.git
agp5050
CommonUtils
CommonUtils
master

搜索帮助