ApplyTaskCacheService.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.qmth.exam.reserve.cache.impl;
  2. import com.qmth.exam.reserve.cache.CacheConstants;
  3. import com.qmth.exam.reserve.cache.RedisClient;
  4. import com.qmth.exam.reserve.service.ExamSiteService;
  5. import com.qmth.exam.reserve.service.StudentApplyService;
  6. import com.qmth.exam.reserve.service.StudentService;
  7. import org.redisson.api.RAtomicLong;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Component;
  12. import java.util.concurrent.TimeUnit;
  13. @Component
  14. public class ApplyTaskCacheService implements CacheConstants {
  15. private static final Logger log = LoggerFactory.getLogger(ApplyTaskCacheService.class);
  16. @Autowired
  17. private RedisClient redisClient;
  18. @Autowired
  19. private ExamSiteService examSiteService;
  20. @Autowired
  21. private StudentService studentService;
  22. @Autowired
  23. private StudentApplyService studentApplyService;
  24. /**
  25. * 获取某考生的“允许预约时段次数”
  26. */
  27. public int getStudentApplyNumber(Long studentId) {
  28. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  29. Integer value = redisClient.get(cacheKey, Integer.class);
  30. if (value != null) {
  31. return value;
  32. }
  33. value = studentService.findStudentApplyNumberById(studentId);
  34. redisClient.set(cacheKey, value, 3, TimeUnit.HOURS);
  35. log.info("{} = {}", cacheKey, value);
  36. return value;
  37. }
  38. /**
  39. * 获取某考点某时段的“可预约总量”
  40. */
  41. public int getApplyTotalCount(Long examSiteId, Long timePeriodId) {
  42. String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId, timePeriodId);
  43. Integer value = redisClient.get(cacheKey, Integer.class);
  44. if (value != null) {
  45. return value;
  46. }
  47. // 当前考点当前时段容量 = 当前考点总容量
  48. value = examSiteService.countExamSiteCapacityById(examSiteId);
  49. redisClient.set(cacheKey, value, 3, TimeUnit.HOURS);
  50. log.info("{} = {}", cacheKey, value);
  51. return value;
  52. }
  53. /**
  54. * 获取某考点某时段的“已预约数量”
  55. */
  56. public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
  57. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  58. if (redisClient.exist(cacheKey)) {
  59. return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
  60. }
  61. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  62. int value = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
  63. atomic.set(value);
  64. atomic.expire(30, TimeUnit.DAYS);
  65. log.info("{} = {}", cacheKey, value);
  66. return value;
  67. }
  68. /**
  69. * 累加 某考点某时段的“已预约数量”
  70. */
  71. public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
  72. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  73. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  74. atomic.incrementAndGet();
  75. }
  76. /**
  77. * 累减 某考点某时段的“已预约数量”
  78. */
  79. public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
  80. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  81. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  82. atomic.decrementAndGet();
  83. }
  84. }