123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- package com.qmth.exam.reserve.cache.impl;
- import com.qmth.exam.reserve.cache.CacheConstants;
- import com.qmth.exam.reserve.cache.RedisClient;
- 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 ExamSiteService examSiteService;
- @Autowired
- private StudentService studentService;
- @Autowired
- private StudentApplyService studentApplyService;
- /**
- * 获取某考生的“允许预约时段次数”
- */
- 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, 3, TimeUnit.HOURS);
- log.info("{} = {}", cacheKey, value);
- return value;
- }
- /**
- * 获取某考点某时段的“可预约总量”
- */
- public int getApplyTotalCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId, timePeriodId);
- Integer value = redisClient.get(cacheKey, Integer.class);
- if (value != null) {
- return value;
- }
- // 当前考点当前时段容量 = 当前考点总容量
- value = examSiteService.countExamSiteCapacityById(examSiteId);
- redisClient.set(cacheKey, value, 3, TimeUnit.HOURS);
- log.info("{} = {}", cacheKey, value);
- return value;
- }
- /**
- * 获取某考点某时段的“已预约数量”
- */
- 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();
- }
- }
|