各位老爷不要说我菜,最近写了一个数据量比较大的查询,一次请求要十几秒钟,搞的前端框框超时
,后面的屎山代码真的不想再看,想了个法子处理一下
代码奉上
Object o = RedisUtil.get(key);
if (o==null){
AsyncUtil.runAsync(()->{
CommonResponse<AutoAssginQueryConfigShopListDto> autoAssginQueryConfigShopListDtoCommonResponse = productIntegration.queryConfigureShopList(request);
String string = JSON.toJSONString(autoAssginQueryConfigShopListDtoCommonResponse);
RedisUtil.set(key,string);
});
throw new HzsxBizException("-1","数据正在加载中请稍后~");
}else {
String result = (String)RedisUtil.get(key);
AsyncUtil.runAsync(()->{
RedisUtil.del(key);
CommonResponse<AutoAssginQueryConfigShopListDto> autoAssginQueryConfigShopListDtoCommonResponse = productIntegration.queryConfigureShopList(request);
String string = JSON.toJSONString(autoAssginQueryConfigShopListDtoCommonResponse);
RedisUtil.set(key,string);
});
return JSON.parseObject(result,CommonResponse.class);
第一次查不到就给抛异常友好一点,后异步干到redis里面去,如果数据变动了话,加个删除key的逻辑就行,注意数据库性能吧,两个异步我已经给mysql干崩了好几次了---刺激啊