123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package com.qmth.exam.reserve.cache.impl;
- import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
- import com.qmth.exam.reserve.cache.CacheConstants;
- import com.qmth.exam.reserve.cache.RedisClient;
- import com.qmth.exam.reserve.service.ApplyTaskService;
- import com.qmth.exam.reserve.service.ExamSiteService;
- import com.qmth.exam.reserve.service.StudentApplyService;
- import com.qmth.exam.reserve.service.StudentService;
- import org.redisson.api.RAtomicLong;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.util.concurrent.TimeUnit;
- @Component
- public class ApplyTaskCacheService implements CacheConstants {
- private static final Logger log = LoggerFactory.getLogger(ApplyTaskCacheService.class);
- @Autowired
- private RedisClient redisClient;
- @Autowired
- private ApplyTaskService applyTaskService;
- @Autowired
- private ExamSiteService examSiteService;
- @Autowired
- private StudentService studentService;
- @Autowired
- private StudentApplyService studentApplyService;
- /**
- * 获取当前启用的预约任务缓存
- */
- public CurrentApplyTaskVO currentApplyTask(Long orgId) {
- String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
- CurrentApplyTaskVO value = redisClient.get(cacheKey, CurrentApplyTaskVO.class);
- if (value != null) {
- return value;
- }
- value = applyTaskService.currentApplyTask(orgId);
- if (value == null) {
- return null;
- }
- redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
- log.info("{} = id:{} name:{}", cacheKey, value.getTaskId(), value.getTaskName());
- return value;
- }
- public void clearCurrentApplyTaskCache(Long orgId) {
- String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
- redisClient.delete(cacheKey);
- log.warn("清理缓存!cacheKey:{}", cacheKey);
- }
- /**
- * 获取某考生的“允许预约时段次数”
- */
- public int getStudentApplyNumber(Long studentId) {
- String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
- Integer value = redisClient.get(cacheKey, Integer.class);
- if (value != null) {
- return value;
- }
- value = studentService.findStudentApplyNumberById(studentId);
- redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
- log.info("{} = {}", cacheKey, value);
- return value;
- }
- public void clearStudentApplyNumberCache(Long studentId) {
- String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
- redisClient.delete(cacheKey);
- log.warn("清理缓存!cacheKey:{}", cacheKey);
- }
- /**
- * 获取某考点的“可预约总量”
- */
- public int getApplyTotalCount(Long examSiteId) {
- String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
- Integer value = redisClient.get(cacheKey, Integer.class);
- if (value != null) {
- return value;
- }
- value = examSiteService.countExamSiteCapacityById(examSiteId);
- redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
- log.info("{} = {}", cacheKey, value);
- return value;
- }
- public void clearApplyTotalCountCache(Long examSiteId) {
- String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
- redisClient.delete(cacheKey);
- log.warn("清理缓存!cacheKey:{}", cacheKey);
- }
- /**
- * 获取某考点某时段的“已预约数量”
- */
- public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
- if (redisClient.exist(cacheKey)) {
- return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
- }
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- int value = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
- atomic.set(value);
- atomic.expire(30, TimeUnit.DAYS);
- log.info("{} = {}", cacheKey, value);
- return value;
- }
- /**
- * 累加 某考点某时段的“已预约数量”
- */
- public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- atomic.incrementAndGet();
- }
- /**
- * 累减 某考点某时段的“已预约数量”
- */
- public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- atomic.decrementAndGet();
- }
- }
|