文件名称:缓存穿透.txt
文件大小:860B
文件格式:TXT
更新时间:2022-09-12 03:38:21
高并发技术
@Service("userServiceImpl") public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Autowired private RedisTemplate<Object,Object> redisTemplate; @Override public Long queryAllUserCount() { Long allUserCount= (Long) redisTemplate.opsForValue().get(Constants.ALL_USER_COUNT); if(allUserCount==null){ //注意缓存穿透 synchronized (this){ allUserCount= (Long) redisTemplate.opsForValue().get(Constants.ALL_USER_COUNT); if(allUserCount==null){ allUserCount=userMapper.selectAllUserCount(); redisTemplate.opsForValue().set(Constants.ALL_USER_COUNT, allUserCount, 15, TimeUnit.SECONDS); } } } return allUserCount; } }