fuliqi
2024-10-17 8546b3d285af4235a0ef615a0c6e89486ae2c806
src/main/java/com/ycl/jxkg/job/StudyRecordJob.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.ycl.jxkg.constants.CaffeineConstant;
import com.ycl.jxkg.domain.entity.StudyRecord;
import com.ycl.jxkg.mapper.StudyRecordMapper;
import com.ycl.jxkg.service.StudyRecordService;
@@ -32,37 +33,36 @@
    private final StudyRecordMapper studyRecordMapper;
    private final StudyRecordService studyRecordService;
    @Scheduled(fixedRate = 1200000) // 20分钟执行一次
    @Scheduled(fixedRate = 1200000) // 2分钟执行一次
    private void updateStudyRecord() {
        log.info("开始存学习时长");
        List<StudyRecord> cacheList = new ArrayList<>();
        // 取出所有缓存项
        ConcurrentMap<String, Object> map = caffeineCache.asMap();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.startsWith("STUDENT_")) {
                StudyRecord studyRecord = (StudyRecord) entry.getValue();
                cacheList.add(studyRecord);
        // 取出所有学习记录缓存项
        Map<String, StudyRecord> studyMap = (Map<String, StudyRecord>) caffeineCache.getIfPresent(CaffeineConstant.STUDY_RECORD);
        if (!CollectionUtils.isEmpty(studyMap)) {
            for (Map.Entry<String, StudyRecord> entry : studyMap.entrySet()) {
                cacheList.add(entry.getValue());
            }
        }
        List<Integer> studentIds = cacheList.stream().map(StudyRecord::getStudentId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(studentIds)) {
            //数据库中已经存在的学生数据
            QueryWrapper<StudyRecord> wrapper = new QueryWrapper<>();
            wrapper.in("student_id", studentIds);
            List<StudyRecord> studyRecords = studyRecordMapper.selectList(wrapper);
            for (StudyRecord record : studyRecords) {
                for (StudyRecord cacheRecord : cacheList) {
                    if (record.getStudentId().equals(cacheRecord.getStudentId())) {
                        cacheRecord.setId(record.getId());
                        cacheRecord.setStudyTime(record.getStudyTime() + cacheRecord.getStudyTime());
                        cacheRecord.setMeetCount(record.getMeetCount());
            List<Integer> studentIds = cacheList.stream().map(StudyRecord::getStudentId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(studentIds)) {
                //数据库中已经存在的学生数据
                QueryWrapper<StudyRecord> wrapper = new QueryWrapper<>();
                wrapper.in("student_id", studentIds);
                List<StudyRecord> studyRecords = studyRecordMapper.selectList(wrapper);
                for (StudyRecord record : studyRecords) {
                    for (StudyRecord cacheRecord : cacheList) {
                        if (record.getStudentId().equals(cacheRecord.getStudentId())) {
                            cacheRecord.setId(record.getId());
                            cacheRecord.setStudyTime(record.getStudyTime() + cacheRecord.getStudyTime());
                            cacheRecord.setMeetCount(record.getMeetCount());
                        }
                    }
                }
                studyRecordService.saveOrUpdateBatch(cacheList);
            }
            studyRecordService.saveOrUpdateBatch(cacheList);
            caffeineCache.invalidate(CaffeineConstant.STUDY_RECORD);
        }
        caffeineCache.invalidateAll(map.keySet());
        log.info("结束存学习时长");
    }
}