1@Override 2public Long lastMomentId(Long lessonId, Long userId) { 3 Long challengeMomentId = 0L; 4 String key = RedisKeyUtil.build(RedisConstants.ChallengeMoment.LAST_MOMENT_ID, lessonId, userId); 5 6 if(hashRedisTemplate.exists(key)) { 7 String json = hashRedisTemplate.hget(key, RedisConstants.HashEntity.VALUE); 8 if(StringUtils.isNotBlank(json)) { 9 challengeMomentId = Long.parseLong(json); 10 } 11 }else { 12 ChallengeMoment challengeMoment = challengeMomentRepository.findFirstByMap(Mapper.sql("lesson.last.moment"), new MapBuilder() 13 .append("createBy", userId) 14 .append("lessonId", lessonId) 15 .build()); 16 if(null != challengeMoment) { 17 challengeMomentId = challengeMoment.getId(); 18 hashRedisTemplate.hset(key, RedisConstants.HashEntity.VALUE, String.valueOf(challengeMomentId)); 19 hashRedisTemplate.hset(key, RedisConstants.HashEntity.FLAG, RedisConstants.HashEntity.FLAG_EXIST); //防穿透 20 }else { 21 hashRedisTemplate.hset(key, RedisConstants.HashEntity.FLAG, RedisConstants.HashEntity.FLAG_NOT_EXIST); 22 } 23 hashRedisTemplate.expire(key, CommonConstants.Time.THREE_DAY); 24 } 25 return challengeMomentId; 26} 27 28@Override 29public void addLastMomentId(Long lessonId, Long userId, Long momentId) { 30 String key = RedisKeyUtil.build(RedisConstants.ChallengeMoment.LAST_MOMENT_ID, lessonId, userId); 31 hashRedisTemplate.hset(key, RedisConstants.HashEntity.FLAG, RedisConstants.HashEntity.FLAG_EXIST); 32 hashRedisTemplate.hset(key, RedisConstants.HashEntity.VALUE, String.valueOf(momentId)); 33 hashRedisTemplate.expire(key, CommonConstants.Time.THREE_DAY); 34}
Value对象缓存用Hash防穿透
Wesley13
2021-10-11
1287 0 0
点赞
收藏
评论区
加载中...