ApplyTaskCacheService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.qmth.exam.reserve.cache.impl;
  2. import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
  3. import com.qmth.exam.reserve.cache.CacheConstants;
  4. import com.qmth.exam.reserve.cache.RedisClient;
  5. import com.qmth.exam.reserve.service.ApplyTaskService;
  6. import com.qmth.exam.reserve.service.ExamSiteService;
  7. import com.qmth.exam.reserve.service.StudentApplyService;
  8. import com.qmth.exam.reserve.service.StudentService;
  9. import org.redisson.api.RAtomicLong;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. import java.util.concurrent.TimeUnit;
  15. @Component
  16. public class ApplyTaskCacheService implements CacheConstants {
  17. private static final Logger log = LoggerFactory.getLogger(ApplyTaskCacheService.class);
  18. @Autowired
  19. private RedisClient redisClient;
  20. @Autowired
  21. private ApplyTaskService applyTaskService;
  22. @Autowired
  23. private ExamSiteService examSiteService;
  24. @Autowired
  25. private StudentService studentService;
  26. @Autowired
  27. private StudentApplyService studentApplyService;
  28. /**
  29. * 获取当前启用的预约任务缓存
  30. */
  31. public CurrentApplyTaskVO currentApplyTask(Long orgId) {
  32. String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
  33. CurrentApplyTaskVO value = redisClient.get(cacheKey, CurrentApplyTaskVO.class);
  34. if (value != null) {
  35. return value;
  36. }
  37. value = applyTaskService.currentApplyTask(orgId);
  38. if (value == null) {
  39. return null;
  40. }
  41. redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
  42. log.info("{} = id:{} name:{}", cacheKey, value.getTaskId(), value.getTaskName());
  43. return value;
  44. }
  45. public void clearCurrentApplyTaskCache(Long orgId) {
  46. String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
  47. redisClient.delete(cacheKey);
  48. log.warn("清理缓存!cacheKey:{}", cacheKey);
  49. }
  50. /**
  51. * 获取某考生的“允许预约时段次数”
  52. */
  53. public int getStudentApplyNumber(Long studentId) {
  54. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  55. Integer value = redisClient.get(cacheKey, Integer.class);
  56. if (value != null) {
  57. return value;
  58. }
  59. value = studentService.findStudentApplyNumberById(studentId);
  60. redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
  61. log.info("{} = {}", cacheKey, value);
  62. return value;
  63. }
  64. public void clearStudentApplyNumberCache(Long studentId) {
  65. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  66. redisClient.delete(cacheKey);
  67. log.warn("清理缓存!cacheKey:{}", cacheKey);
  68. }
  69. /**
  70. * 获取某考点的“可预约总量”
  71. */
  72. public int getApplyTotalCount(Long examSiteId) {
  73. String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
  74. Integer value = redisClient.get(cacheKey, Integer.class);
  75. if (value != null) {
  76. return value;
  77. }
  78. value = examSiteService.countExamSiteCapacityById(examSiteId);
  79. redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
  80. log.info("{} = {}", cacheKey, value);
  81. return value;
  82. }
  83. public void clearApplyTotalCountCache(Long examSiteId) {
  84. String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
  85. redisClient.delete(cacheKey);
  86. log.warn("清理缓存!cacheKey:{}", cacheKey);
  87. }
  88. /**
  89. * 获取某考点某时段的“已预约数量”
  90. */
  91. public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
  92. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  93. if (redisClient.exist(cacheKey)) {
  94. return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
  95. }
  96. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  97. int value = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
  98. atomic.set(value);
  99. atomic.expire(30, TimeUnit.DAYS);
  100. log.info("{} = {}", cacheKey, value);
  101. return value;
  102. }
  103. /**
  104. * 累加 某考点某时段的“已预约数量”
  105. */
  106. public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
  107. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  108. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  109. atomic.incrementAndGet();
  110. }
  111. /**
  112. * 累减 某考点某时段的“已预约数量”
  113. */
  114. public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
  115. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  116. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  117. atomic.decrementAndGet();
  118. }
  119. }