update 加入缓存

develop
hhb@hotmail.com 3 days ago
parent 94a1b90826
commit bb402b7eb6

@ -10,13 +10,17 @@ import cn.teammodel.repository.ExamRepository;
import cn.teammodel.repository.SchoolRepository;
import cn.teammodel.utils.GroupUtil;
import com.azure.cosmos.models.PartitionKey;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Service
public class ExamServiceImpl implements ExamService {
@ -27,9 +31,30 @@ public class ExamServiceImpl implements ExamService {
private SchoolRepository schoolRepository;
@Autowired
private Environment env;
private static final int CACHE_EXPIRATION_HOURS = 8;
private static final int CACHE_MAXIMUM_SIZE = 1000;
private static final TimeUnit CACHE_EXPIRATION_UNIT = TimeUnit.HOURS;
// 引入缓存,需根据实际情况配置大小和过期时间 每次服务重启时 缓存会清空
private Cache<String, Map<String, Object>> analysisCache;
@PostConstruct
public void init() {
// 创建公共配置的CacheBuilder
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRATION_HOURS, CACHE_EXPIRATION_UNIT)
.maximumSize(CACHE_MAXIMUM_SIZE);
// 构建各缓存实例
analysisCache = cacheBuilder.build();
}
@Override
public Map<String, Object> getAnalysis(OverViewDto overViewDto, HttpServletRequest request) {
String cacheKey = overViewDto.getSchool() + "-" + overViewDto.getPeriodId() + "-" + overViewDto.getSemesterId()
+ "-" + overViewDto.getStudyYear() + "-" + overViewDto.getClassIds() + "-" + overViewDto.getStudentYear();
if (analysisCache.getIfPresent(cacheKey) != null) {
return analysisCache.getIfPresent(cacheKey);
}
Map<String, Object> overView;
Map<String, Object> examRate;
String url = env.getProperty("ies.server-url-overview");
@ -49,6 +74,7 @@ public class ExamServiceImpl implements ExamService {
throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");
}
analysisCache.put(cacheKey, overView);
return overView;
}

@ -28,6 +28,8 @@ import cn.teammodel.test.LessonRecordQueryService;
import cn.teammodel.utils.GroupUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
@ -48,6 +50,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -76,14 +79,33 @@ public class LaborEducationServiceImpl implements LaborEducationService {
private static Environment environment; // 静态字段
@Autowired
private Environment env; // 非静态字段
private static final int CACHE_EXPIRATION_HOURS = 8;
private static final int CACHE_MAXIMUM_SIZE = 1000;
private static final TimeUnit CACHE_EXPIRATION_UNIT = TimeUnit.HOURS;
// 引入缓存,需根据实际情况配置大小和过期时间 每次服务重启时 缓存会清空
private Cache<String, Map<String, Object>> analysisCache;
@PostConstruct
public void init() {
LaborEducationServiceImpl.environment = env; // 在初始化时将非静态字段赋值给静态字段
// 创建公共配置的CacheBuilder
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRATION_HOURS, CACHE_EXPIRATION_UNIT)
.maximumSize(CACHE_MAXIMUM_SIZE);
// 构建各缓存实例
analysisCache = cacheBuilder.build();
}
@Override
public Map<String, Object> getAnalysis(MoralDto laborDto, HttpServletRequest request) {
String cacheKey = String.format("Moral_%s_%s_%s_%s_%s_%s_%s_%s_%s", laborDto.getCode(), laborDto.getStartTime(),
laborDto.getEndTime(), laborDto.getGrade(), laborDto.getClassId(), laborDto.getPeriodId(),
laborDto.getSubjectId(),laborDto.getAcademicYearId(),laborDto.getTmdId());
Map<String, Object> cachedResult = analysisCache.getIfPresent(cacheKey);
if (cachedResult != null) {
return cachedResult;
}
//根据具体参数查询相关课列内容
List<LessonRecord> records;
LessonRecordQueryService queryService = new LessonRecordQueryService(lessonRecordRepository);
@ -222,6 +244,7 @@ public class LaborEducationServiceImpl implements LaborEducationService {
List<Map<String, Object>> students = combineScoresWithExamResults(scores,examResults,knowledgeMap,point,knowledgeBlockToPointsMap);
resMap.put("scores", students);
}
analysisCache.put(cacheKey, resMap);
//处理主观评价内容
return resMap;

@ -28,6 +28,8 @@ import cn.teammodel.test.LessonRecordQueryService;
import cn.teammodel.utils.GroupUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
@ -48,6 +50,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -76,14 +79,31 @@ public class MoralEducationServiceImpl implements MoralEducationService {
private static Environment environment; // 静态字段
@Autowired
private Environment env; // 非静态字段
private static final int CACHE_EXPIRATION_HOURS = 8;
private static final int CACHE_MAXIMUM_SIZE = 1000;
private static final TimeUnit CACHE_EXPIRATION_UNIT = TimeUnit.HOURS;
// 引入缓存,需根据实际情况配置大小和过期时间 每次服务重启时 缓存会清空
private Cache<String, Map<String, Object>> analysisCache;
@PostConstruct
public void init() {
MoralEducationServiceImpl.environment = env; // 在初始化时将非静态字段赋值给静态字段
// 创建公共配置的CacheBuilder
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRATION_HOURS, CACHE_EXPIRATION_UNIT)
.maximumSize(CACHE_MAXIMUM_SIZE);
// 构建各缓存实例
analysisCache = cacheBuilder.build();
}
@Override
public Map<String, Object> getAnalysis(LaborDto laborDto, HttpServletRequest request) {
String cacheKey = String.format("Labor_%s_%s_%s_%s_%s_%s_%s_%s_%s", laborDto.getCode(), laborDto.getStartTime(),
laborDto.getEndTime(), laborDto.getGrade(), laborDto.getClassId(), laborDto.getPeriodId(),
laborDto.getSubjectId(),laborDto.getAcademicYearId(),laborDto.getTmdId());
Map<String, Object> cachedResult = analysisCache.getIfPresent(cacheKey);
if (cachedResult != null) {
return cachedResult;
}
//根据具体参数查询相关课列内容
List<LessonRecord> records;
LessonRecordQueryService queryService = new LessonRecordQueryService(lessonRecordRepository);
@ -223,6 +243,7 @@ public class MoralEducationServiceImpl implements MoralEducationService {
resMap.put("scores", students);
}
//处理主观评价内容
analysisCache.put(cacheKey, resMap);
return resMap;
}

Loading…
Cancel
Save