//DEFAULT_NULL_VALUE = "";
public <E> E getValueByKeyOrSetNull(String key, Supplier<E> supplier, Class<E> clazz, Long timeout, Long nullTimeout) {
String json = wiseCache.get(key, true);
if (json == null) {
E result = supplier.get();
if (ObjectUtils.isEmpty(result)) {
//为了防止缓存穿透 只允许传递 ""
wiseCache.set(key, DEFAULT_NULL_VALUE, (nullTimeout==null?nullCacheTimeOut : nullTimeout) + new Random().nextInt(60));
return null;
}
wiseCache.seto(key, result, (timeout == null ? DEFAULT_TIME_OUT : timeout) + new Random().nextInt(60));
return result;
} else if (DEFAULT_NULL_VALUE.equals(json)) {
return null;
}else {
if (clazz == String.class) {
return (E) json;
}
try{
return JsonUtils.deserialize(json, clazz);
}catch (Exception e){
return null;
}
}
}
Redis缓存改造
最新推荐文章于 2023-09-20 16:48:59 发布