update 加入缓存

develop
hhb@hotmail.com 7 days ago
parent afab1c74c3
commit 28c746c662

@ -22,6 +22,8 @@ import com.azure.cosmos.CosmosException;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
@ -36,9 +38,7 @@ import javax.servlet.http.HttpServletRequest;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum;
@ -749,6 +749,8 @@ public class TeacherServiceImpl implements TeacherService {
final int SLICE_SIZE = 1000;
List<SugVo> sugVoList = getSugVos(teacherDto, request);
//获取教师年级分配情况
long startTimeOnline = System.currentTimeMillis(); // 记录开始时间
Map<String, Object> Map = getTeacherGradeCount(teacherDto, request);
User loginUser = SecurityUtil.getLoginUser();
String loginId = loginUser.getId();
@ -937,6 +939,10 @@ public class TeacherServiceImpl implements TeacherService {
mapTeach.put("Lessons",records.size());
//资源数量:试卷数量
mapTeach.put("paperCount",count);
long endTime = System.currentTimeMillis(); // 记录结束时间
long duration = endTime - startTimeOnline; // 计算耗时
System.out.println("research 方法耗时: " + duration + " ms");
return mapTeach;
}
}
@ -1264,55 +1270,79 @@ public class TeacherServiceImpl implements TeacherService {
return result;
}
private @NotNull List<SugVo> getSugVos(TeacherDto teacherDto, HttpServletRequest request) {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 引入缓存,需根据实际情况配置大小和过期时间
private static final Cache<String, Map<String, Object>> REQUEST_CACHE = CacheBuilder.newBuilder()
.maximumSize(500)
.expireAfterWrite(8, TimeUnit.HOURS)
.build();
@NotNull
private List<SugVo> getSugVos(TeacherDto teacherDto, HttpServletRequest request) {
String url = env.getProperty("ies.server-url-get-channel-data");
Map<String, Object> overView;
List<SugVo> sugVoList = new ArrayList<SugVo>();
List<SugVo> sugVoList = new ArrayList<>();
try {
Instant instantSTime = Instant.ofEpochMilli(teacherDto.getStartTime());
Instant instantETime = Instant.ofEpochMilli(teacherDto.getEndTime());
// 处理时间参数
Instant startInstant = Instant.ofEpochMilli(teacherDto.getStartTime());
Instant endInstant = Instant.ofEpochMilli(teacherDto.getEndTime());
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalDate localDateSTime = instantSTime.atZone(zoneId).toLocalDate();
LocalDate localDateETime = instantETime.atZone(zoneId).toLocalDate();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String sTime = localDateSTime.format(formatter);
String eTime = localDateETime.format(formatter);
teacherDto.duration.add(sTime);
teacherDto.duration.add(eTime);
String sTime = startInstant.atZone(zoneId).toLocalDate().format(DATE_FORMATTER);
String eTime = endInstant.atZone(zoneId).toLocalDate().format(DATE_FORMATTER);
teacherDto.duration.addAll(Arrays.asList(sTime, eTime));
teacherDto.school_shortcode.add(teacherDto.getCode());
overView = GroupUtil.getGroupId(teacherDto,new GroupUtil(env), request,url);
for(Map.Entry<String, Object> entry : overView.entrySet()) {
if (entry.getKey().equals("data")) {
List<Map<String, Object>> dataList = (List<Map<String, Object>>) entry.getValue();
for (Map<String, Object> dataMap : dataList) {
if(dataMap.containsKey("statistics")) {
List<Map<String, Object>> statisticsList = (List<Map<String, Object>>) dataMap.get("statistics");
for (Map<String, Object> statisticsMap : statisticsList) {
SugVo sugVo = convertToSugVo(statisticsMap);
sugVoList.add(sugVo);
}
}
if (dataMap.containsKey("member_list")) {
List<Map<String, Object>> memberList = (List<Map<String, Object>>) dataMap.get("member_list");
for (Map<String, Object> memberMap : memberList) {
for (SugVo sugVo : sugVoList) {
if (memberMap.get("habook").equals(sugVo.getHabook())) {
sugVo.setStar_level(Integer.parseInt(memberMap.get("star_level").toString()));
}
}
}
// 生成缓存键,根据实际参数组合
String cacheKey = generateCacheKey(teacherDto);
overView = REQUEST_CACHE.get(cacheKey, () -> {
// 缓存未命中时调用第三方接口
return GroupUtil.getGroupId(teacherDto, new GroupUtil(env), request, url);
});
// 处理返回数据
if (overView.containsKey("data")) {
List<Map<String, Object>> dataList = (List<Map<String, Object>>) overView.get("data");
// 使用并行流处理独立的dataMap确保线程安全
dataList.parallelStream().forEach(dataMap -> {
List<Map<String, Object>> statisticsList = (List<Map<String, Object>>) dataMap.getOrDefault("statistics", Collections.emptyList());
List<Map<String, Object>> memberList = (List<Map<String, Object>>) dataMap.getOrDefault("member_list", Collections.emptyList());
// 构建habook到SugVo的映射
Map<String, SugVo> habookToSugVo = new ConcurrentHashMap<>();
List<SugVo> localSugVos = statisticsList.stream()
.map(this::convertToSugVo)
.collect(Collectors.toList());
localSugVos.forEach(sug -> habookToSugVo.put(sug.getHabook(), sug));
// 更新star_level
memberList.forEach(member -> {
String habook = (String) member.get("habook");
SugVo sug = habookToSugVo.get(habook);
if (sug != null) {
sug.setStar_level(Integer.parseInt(member.get("star_level").toString()));
}
});
// 将本地结果合并到主列表(需同步避免并发问题)
synchronized (sugVoList) {
sugVoList.addAll(localSugVos);
}
}
});
}
}catch (Exception e) {
} catch (Exception e) {
throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");
}
return sugVoList;
}
// 生成缓存键
private String generateCacheKey(TeacherDto dto) {
return String.join(":",
dto.getCode(),
String.valueOf(dto.getStartTime()),
String.valueOf(dto.getEndTime()));
}
private SugVo convertToSugVo(Map<String, Object> dataMap) {
SugVo sugVo = new SugVo();
sugVo.setDate(dataMap.get("date").toString());

Loading…
Cancel
Save