Commit 0add4ae3 by ThomasAdv

查询所有模型增加缓存机制

1 parent 71204c1b
package cn.com.glsx.cdp.common.constant;
public class RedisKeyConstants {
public static final String MODEL_LIST_STRING = "cdp_model_list_str";
}
......@@ -563,7 +563,7 @@ public class DataReportRabbitMQConsumer {
* @date 2023年09月15日 14:29:24
*/
private List<ModelDTO> matchModel(JSONObject jsonData) {
List<ModelDTO> models = modelService.listModels(null);
List<ModelDTO> models = modelService.listAllModels();
List<ModelDTO> matchModels = new ArrayList<>();
for (ModelDTO model : models) {
// 没有匹配的模型字段则跳过
......
package cn.com.glsx.cdp.modules.service;
import cn.com.glsx.cdp.common.constant.RedisKeyConstants;
import cn.com.glsx.cdp.modules.mapper.ModelMapper;
import cn.com.glsx.cdp.modules.model.ModelDTO;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
......@@ -20,10 +26,25 @@ import java.util.Map;
public class ModelService {
@Autowired
private ModelMapper modelMapper;
@Autowired
private RedisTemplate redisTemplate;
@NacosValue(value = "${cache.minute.model.list}", autoRefreshed = true)
private Long modelListCacheMinute;
public List<ModelDTO> listModels(ModelDTO modelDTO){
public List<ModelDTO> listAllModels(){
try{
return modelMapper.list(modelDTO);
BoundValueOperations valueOps = redisTemplate.boundValueOps(RedisKeyConstants.MODEL_LIST_STRING);
// 先尝试从缓存中获取数据
List<ModelDTO> cachedList = (List<ModelDTO>) valueOps.get();
if (CollectionUtils.isNotEmpty(cachedList)) {
log.info("listModels, get data from redis cache");
return cachedList;
}
List<ModelDTO> list = modelMapper.list(null);
if (CollectionUtils.isNotEmpty(list)) {
valueOps.set(list, Optional.ofNullable(modelListCacheMinute).orElse(60L), TimeUnit.MINUTES);
}
return list;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!