Browse Source

组织树添加临时缓存 要不然前端容易挂

dev
jianjun 3 years ago
parent
commit
cef8129dd5
  1. 4
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java
  2. 36
      epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.java

4
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java

@ -440,4 +440,8 @@ public class RedisUtils {
public Long bitCount(String key, int start, int end) {
return redisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes(), start, end));
}
public Long getTTL(String treeCacheKey) {
return redisTemplate.getExpire(treeCacheKey);
}
}

36
epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.java

@ -12,8 +12,11 @@ import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.dto.form.DingTextBriefNessFormDTO;
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.commons.tools.redis.common.CustomerOrgRedis;
import com.epmet.commons.tools.redis.common.CustomerStaffRedis;
import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache;
@ -60,6 +63,7 @@ import java.io.OutputStream;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
/**
@ -95,6 +99,10 @@ public class GovOrgServiceImpl implements GovOrgService {
private EvaluationIndexService evaluationIndexService;
@Autowired
private IcBuildingDao icBuildingDao;
@Autowired
private RedisUtils redisUtils;
@Autowired
private ExecutorService executorService;
/**
* @param staffId
@ -583,12 +591,34 @@ public class GovOrgServiceImpl implements GovOrgService {
@Override
public List<OrgTreeResultDTO> getAgencyTree(TokenDto tokenDto, SubOrgFormDTO formDTO) {
CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId());
if (staffInfo == null || StringUtils.isBlank(staffInfo.getAgencyId())){
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),"工作人员所属组织不存在");
}
//组织缓存key
String treeCacheKey = RedisKeys.getOrgTreeCacheKey(formDTO.getAgencyId()).concat(StrConstant.COLON).concat(formDTO.getClient());
Long expiryTime = redisUtils.getTTL(treeCacheKey);
List<OrgTreeResultDTO> orgTreeResultDTOS = (List<OrgTreeResultDTO>) redisUtils.get(treeCacheKey);
String agencyId = staffInfo.getAgencyId();
String client = formDTO.getClient();
//如果接近过期或已经过期且缓存数据不为空 则异步查询
if ((expiryTime == null || expiryTime <= NumConstant.ONE_THOUSAND) && CollectionUtils.isNotEmpty(orgTreeResultDTOS)) {
executorService.submit(() -> {
List<OrgTreeResultDTO> list = buildTempOrgTree(agencyId, client);
redisUtils.set(treeCacheKey, list, RedisUtils.HOUR_FOUR_EXPIRE);
});
} else {
orgTreeResultDTOS = buildTempOrgTree(staffInfo.getAgencyId(), formDTO.getClient());
redisUtils.set(treeCacheKey, orgTreeResultDTOS, RedisUtils.HOUR_FOUR_EXPIRE);
}
return orgTreeResultDTOS;
}
private List<OrgTreeResultDTO> buildTempOrgTree(String agencyId, String client){
List<OrgTreeResultDTO> list = new ArrayList<>();
if ("resi".equals(formDTO.getClient())) {
list.add(customerAgencyDao.getResiOrgTree(staffInfo.getAgencyId()));
if ("resi".equals(client)) {
list.add(customerAgencyDao.getResiOrgTree(agencyId));
return list;
}
list.add(customerAgencyDao.getOrgTree(staffInfo.getAgencyId()));
list.add(customerAgencyDao.getOrgTree(agencyId));
return list;
}

Loading…
Cancel
Save