保定网站建设技术支持,扫码进入网站 怎么做,酒店的网站建设方案,wordpress自动链接到图片大小Redis 学习笔记 3#xff1a;黑马点评
准备工作
需要先导入项目相关资源#xff1a;
数据库文件 hmdp.sql后端代码 hm-dianping.zip包括前端代码的 Nginx
启动后端代码和 Nginx。
短信登录
发送验证码
PostMapping(code)
public Result sendCode(RequestP…Redis 学习笔记 3黑马点评
准备工作
需要先导入项目相关资源
数据库文件 hmdp.sql后端代码 hm-dianping.zip包括前端代码的 Nginx
启动后端代码和 Nginx。
短信登录
发送验证码
PostMapping(code)
public Result sendCode(RequestParam(phone) String phone, HttpSession session) {// 发送短信验证码并保存验证码return userService.sendCode(phone, session);
}Log4j2
Service
public class UserServiceImpl extends ServiceImplUserMapper, User implements IUserService {Overridepublic Result sendCode(String phone, HttpSession session) {if (RegexUtils.isPhoneInvalid(phone)) {return Result.fail(不是合法的手机号);}String code RandomUtil.randomNumbers(6);session.setAttribute(code, code);// 发送短信log.debug(发送短信验证码{}, code);return Result.ok();}
}登录
PostMapping(/login)
public Result login(RequestBody LoginFormDTO loginForm, HttpSession session) {// 实现登录功能return userService.login(loginForm, session);
}Override
public Result login(LoginFormDTO loginForm, HttpSession session) {// 验证手机号和验证码if (RegexUtils.isPhoneInvalid(loginForm.getPhone())) {return Result.fail(手机号不合法);}String code (String) session.getAttribute(code);if (code null || !code.equals(loginForm.getCode())) {return Result.fail(验证码不正确);}// 检查用户是否存在QueryWrapperUser qw new QueryWrapper();qw.eq(phone, loginForm.getPhone());User user this.baseMapper.selectOne(qw);if (user null) {user this.createUserByPhone(loginForm.getPhone());}// 将用户信息保存到 sessionsession.setAttribute(user, user);return Result.ok();
}private User createUserByPhone(String phone) {User user new User();user.setPhone(phone);user.setNickName(user_ RandomUtil.randomString(5));this.baseMapper.insert(user);return user;
}统一身份校验
定义拦截器
public class LoginInterceptor implements HandlerInterceptor {Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 从 session 获取用户信息HttpSession session request.getSession();User user (User) session.getAttribute(user);if (user null) {response.setStatus(401);return false;}// 将用户信息保存到 ThreadLocalUserDTO userDTO new UserDTO();userDTO.setIcon(user.getIcon());userDTO.setId(user.getId());userDTO.setNickName(user.getNickName());UserHolder.saveUser(userDTO);return true;}Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {UserHolder.removeUser();}
}添加拦截器
Configuration
public class WebMVCConfig implements WebMvcConfigurer {Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(/shop/**,/voucher/**,/shop-type/**,/upload/**,/blog/hot,/user/code,/user/login);}
}使用 Redis 存储验证码和用户信息
用 Session 存储验证码和用户信息的系统无法进行横向扩展因为多台 Tomcat 无法共享 Session。如果改用 Redis 存储就可以解决这个问题。
修改后的 UserService
Log4j2
Service
public class UserServiceImpl extends ServiceImplUserMapper, User implements IUserService {Autowiredprivate StringRedisTemplate stringRedisTemplate;private static final ObjectMapper OBJECT_MAPPER new ObjectMapper();Overridepublic Result sendCode(String phone, HttpSession session) {if (RegexUtils.isPhoneInvalid(phone)) {return Result.fail(不是合法的手机号);}String code RandomUtil.randomNumbers(6);stringRedisTemplate.opsForValue().set(LOGIN_CODE_KEY phone, code, LOGIN_CODE_TTL);// 发送短信log.debug(发送短信验证码{}, code);return Result.ok();}Overridepublic Result login(LoginFormDTO loginForm, HttpSession session) {// 验证手机号和验证码if (RegexUtils.isPhoneInvalid(loginForm.getPhone())) {return Result.fail(手机号不合法);}String code stringRedisTemplate.opsForValue().get(LOGIN_CODE_KEY loginForm.getPhone());if (code null || !code.equals(loginForm.getCode())) {return Result.fail(验证码不正确);}// 检查用户是否存在QueryWrapperUser qw new QueryWrapper();qw.eq(phone, loginForm.getPhone());User user this.baseMapper.selectOne(qw);if (user null) {user this.createUserByPhone(loginForm.getPhone());}// 将用户信息保存到 sessionString token UUID.randomUUID().toString(true);UserDTO userDTO new UserDTO();BeanUtils.copyProperties(user, userDTO);try {stringRedisTemplate.opsForValue().set(LOGIN_USER_KEY token,OBJECT_MAPPER.writeValueAsString(userDTO), LOGIN_USER_TTL);} catch (JsonProcessingException e) {e.printStackTrace();throw new RuntimeException(e);}return Result.ok(token);}private User createUserByPhone(String phone) {User user new User();user.setPhone(phone);user.setNickName(user_ RandomUtil.randomString(5));this.baseMapper.insert(user);return user;}
}修改后的登录校验拦截器
public class LoginInterceptor implements HandlerInterceptor {private final StringRedisTemplate stringRedisTemplate;private static final ObjectMapper OBJECT_MAPPER new ObjectMapper();public LoginInterceptor(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate stringRedisTemplate;}Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 从头信息获取 tokenString token request.getHeader(Authorization);if (ObjectUtils.isEmpty(token)) {// 缺少 tokenresponse.setStatus(401);return false;}// 从 Redis 获取用户信息String jsonUser this.stringRedisTemplate.opsForValue().get(LOGIN_USER_KEY token);UserDTO userDTO OBJECT_MAPPER.readValue(jsonUser, UserDTO.class);if (userDTO null) {response.setStatus(401);return false;}// 将用户信息保存到 ThreadLocalUserHolder.saveUser(userDTO);// 刷新 token 有效期stringRedisTemplate.expire(LOGIN_USER_KEY token, LOGIN_USER_TTL);return true;}Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {UserHolder.removeUser();}
}还需要添加一个更新用户信息有效期的拦截器
public class RefreshTokenInterceptor implements HandlerInterceptor {private StringRedisTemplate stringRedisTemplate;public RefreshTokenInterceptor(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate stringRedisTemplate;}Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 如果请求头中有 token且 redis 中有 token 相关的用户信息刷新其有效期String token request.getHeader(Authorization);if (ObjectUtils.isEmpty(token)) {return true;}if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(LOGIN_USER_KEY token))) {stringRedisTemplate.expire(LOGIN_USER_KEY token, LOGIN_USER_TTL);}return true;}
}添加这个新的拦截器并且确保其位于登录验证拦截器之前
Configuration
public class WebMVCConfig implements WebMvcConfigurer {Autowiredprivate StringRedisTemplate stringRedisTemplate;Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns(/**);registry.addInterceptor(new LoginInterceptor(stringRedisTemplate)).excludePathPatterns(/shop/**,/voucher/**,/shop-type/**,/upload/**,/blog/hot,/user/code,/user/login);}
}商户查询
缓存
对商户类型查询使用 Redis 缓存以提高查询效率
Service
public class ShopTypeServiceImpl extends ServiceImplShopTypeMapper, ShopType implements IShopTypeService {Autowiredprivate StringRedisTemplate stringRedisTemplate;Overridepublic Result queryTypeList() {String jsonTypeList stringRedisTemplate.opsForValue().get(CACHE_TYPE_LIST_KEY);if (!StringUtils.isEmpty(jsonTypeList)) {ListShopType typeList JSONUtil.toList(jsonTypeList, ShopType.class);return Result.ok(typeList);}ListShopType typeList this.query().orderByAsc(sort).list();if (!typeList.isEmpty()){stringRedisTemplate.opsForValue().set(CACHE_TYPE_LIST_KEY, JSONUtil.toJsonStr(typeList), CACHE_TYPE_LIST_TTL);}return Result.ok(typeList);}
}对商户详情使用缓存
Service
public class ShopServiceImpl extends ServiceImplShopMapper, Shop implements IShopService {private static final ObjectMapper OBJECT_MAPPER new ObjectMapper();Autowiredprivate StringRedisTemplate stringRedisTemplate;Overridepublic Result queryById(Long id) {// 先从 Redis 中查询String jsonShop stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY id);if (!StringUtils.isEmpty(jsonShop)) {Shop shop JSONUtil.toBean(jsonShop, Shop.class);return Result.ok(shop);}// Redis 中没有从数据库查Shop shop this.getById(id);if (shop ! null) {jsonShop JSONUtil.toJsonStr(shop);stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY id, jsonShop, CACHE_SHOP_TTL);}return Result.ok(shop);}
}缓存更新策略
在编辑商户信息时将对应的缓存删除
Override
public Result update(Shop shop) {if (shop.getId() null) {return Result.fail(商户id不能为空);}// 更新商户信息this.updateById(shop);// 删除缓存stringRedisTemplate.delete(CACHE_SHOP_KEY shop.getId());return Result.ok();
}缓存穿透
缓存穿透指如果请求的数据在缓存和数据库中都不存在就不会生成缓存数据每次请求都不会使用缓存会对数据库造成压力。
可以通过缓存空对象的方式解决缓存穿透问题。
在查询商铺信息时缓存空对象
Override
public Result queryById(Long id) {// 先从 Redis 中查询String jsonShop stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY id);if (!StringUtils.isEmpty(jsonShop)) {Shop shop JSONUtil.toBean(jsonShop, Shop.class);return Result.ok(shop);}// Redis 中没有从数据库查Shop shop this.getById(id);if (shop ! null) {jsonShop JSONUtil.toJsonStr(shop);stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY id, jsonShop, CACHE_SHOP_TTL);return Result.ok(shop);} else {// 缓存空对象到缓存中stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY id, , CACHE_NULL_TTL);return Result.fail(店铺不存在);}
}在这里缓存中的空对象用空字符串代替并且将缓存存活时间设置为一个较短的值比如说2分钟。
在从缓存中查询到空对象时返回商铺不存在
Override
public Result queryById(Long id) {// 先从 Redis 中查询String jsonShop stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY id);if (!StringUtils.isEmpty(jsonShop)) {Shop shop JSONUtil.toBean(jsonShop, Shop.class);return Result.ok(shop);}// 如果从缓存中查询到空对象表示商铺不存在if (.equals(jsonShop)) {return Result.fail(商铺不存在);}// ...
}缓存击穿
缓存击穿问题也叫热点Key问题就是一个被高并发访问并且缓存重建业务较复杂的key突然失效了无数的请求访问会在瞬间给数据库带来巨大的冲击。
常见的解决方案有两种
互斥锁逻辑过期
可以利用 Redis 做互斥锁来解决缓存击穿问题
Override
public Result queryById(Long id) {// return queryWithCachePenetration(id);return queryWithCacheBreakdown(id);
}/*** 用 Redis 创建互斥锁** param name 锁名称* return 成功/失败*/
private boolean lock(String name) {Boolean result stringRedisTemplate.opsForValue().setIfAbsent(name, 1, Duration.ofSeconds(10));return BooleanUtil.isTrue(result);
}/*** 删除 Redis 互斥锁** param name 锁名称*/
private void unlock(String name) {stringRedisTemplate.delete(name);
}/*** 查询店铺信息-缓存击穿** param id* return*/
private Result queryWithCacheBreakdown(Long id) {// 先查询是否存在缓存String jsonShop stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY id);if (!StringUtils.isEmpty(jsonShop)) {Shop shop JSONUtil.toBean(jsonShop, Shop.class);return Result.ok(shop);}// 如果从缓存中查询到空对象表示商铺不存在if (.equals(jsonShop)) {return Result.fail(商铺不存在);}// 缓存不存在尝试获取锁并创建缓存final String lockName lock:shop: id;try {if (!lock(lockName)){// 获取互斥锁失败休眠一段时间后重试Thread.sleep(50);return queryWithCacheBreakdown(id);}// 获取互斥锁成功创建缓存// 模拟长时间才能创建缓存Thread.sleep(100);Shop shop this.getById(id);if (shop ! null) {jsonShop JSONUtil.toJsonStr(shop);stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY id, jsonShop, CACHE_SHOP_TTL);return Result.ok(shop);} else {// 缓存空对象到缓存中stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY id, , CACHE_NULL_TTL);return Result.fail(店铺不存在);}} catch (InterruptedException e) {throw new RuntimeException(e);} finally {// 释放锁unlock(lockName);}
}下面是用逻辑过期解决缓存击穿问题的方式。
首先需要将热点数据的缓存提前写入 Redis缓存预热
public class ShopServiceImpl extends ServiceImplShopMapper, Shop implements IShopService {/*** 创建店铺缓存** param id 店铺id* param duration 缓存有效时长*/public void saveShopCache(Long id, Duration duration) {Shop shop getById(id);RedisCacheShop redisCache new RedisCache();redisCache.setExpire(LocalDateTime.now().plus(duration));redisCache.setData(shop);stringRedisTemplate.opsForValue().set(CACHE_SHOP_KEY id, JSONUtil.toJsonStr(redisCache));}// ...
}SpringBootTest
class HmDianPingApplicationTests {Autowiredprivate ShopServiceImpl shopService;Testpublic void testSaveShopCache(){shopService.saveShopCache(1L, Duration.ofSeconds(1));}}Data
public class RedisCacheT {private LocalDateTime expire; //逻辑过期时间private T data; // 数据
}Redis 中的缓存信息包含两部分过期时间和具体信息。大致如下
{data: {area: 大关,openHours: 10:00-22:00,sold: 4215,// ...},expire: 1708258021725
}且其 TTL 是-1也就是永不过期。
具体的缓存读取和重建逻辑
/*** 用逻辑过期解决缓存击穿问题** return*/
private Result queryWithLogicalExpiration(Long id) {//检查缓存是否存在String jsonShop stringRedisTemplate.opsForValue().get(CACHE_SHOP_KEY id);if (StringUtils.isEmpty(jsonShop)) {// 缓存不存在return Result.fail(店铺不存在);}// 缓存存在检查是否过期RedisCacheShop redisCache JSONUtil.toBean(jsonShop, new TypeReferenceRedisCacheShop() {}, true);if (redisCache.getExpire().isBefore(LocalDateTime.now())) {// 如果过期尝试获取互斥锁final String LOCK_NAME LOCK_SHOP_KEY id;if (lock(LOCK_NAME)) {// 获取互斥锁后单独启动线程更新缓存CACHE_UPDATE_ES.execute(() - {try {// 模拟缓存重建的延迟Thread.sleep(200);saveShopCache(id, Duration.ofSeconds(1));} catch (Exception e) {throw new RuntimeException(e);} finally {unlock(LOCK_NAME);}});}}// 无论是否过期返回缓存对象中的信息return Result.ok(redisCache.getData());
}封装 Redis 缓存工具类
可以对对 Redis 缓存相关逻辑进行封装可以避免在业务代码中重复编写相关逻辑。封装后分别对应以下方法
设置缓存数据TTL设置缓存数据逻辑过期时间从缓存获取数据用空对象解决缓存穿透问题从缓存获取数据用互斥锁解决缓存击穿问题从缓存获取数据用逻辑过期解决缓存击穿问题
工具类的完整代码可以参考这里。
本文的完整示例代码可以从这里获取。
参考资料
黑马程序员Redis入门到实战教程 文章转载自: http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.pskjm.cn.gov.cn.pskjm.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.tqxtx.cn.gov.cn.tqxtx.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.nqmdc.cn.gov.cn.nqmdc.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.kzhgy.cn.gov.cn.kzhgy.cn http://www.morning.pcxgj.cn.gov.cn.pcxgj.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.khclr.cn.gov.cn.khclr.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.rgwz.cn.gov.cn.rgwz.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.itvsee.com.gov.cn.itvsee.com http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.xjqhh.cn.gov.cn.xjqhh.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.lcbnb.cn.gov.cn.lcbnb.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.nrchx.cn.gov.cn.nrchx.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn