网站漂浮窗口代码,wap和app,菜鸟是什么网站,工信部网站黑名单学习目标#xff1a;
提示#xff1a;学习如何利用Redisson实现点赞排行榜功能#xff0c;按照时间顺序 当用户给某一篇文章点赞后#xff0c;会再数据库中存储一条数据#xff0c;并且在Redis中存储一条数据为当前博客的点赞用户标识#xff0c;来区分哪个用户对文章进…学习目标
提示学习如何利用Redisson实现点赞排行榜功能按照时间顺序 当用户给某一篇文章点赞后会再数据库中存储一条数据并且在Redis中存储一条数据为当前博客的点赞用户标识来区分哪个用户对文章进行了点赞使用ZSet数据结构对点赞用户进行排序来实现排行榜功能 学习产出
解决方案
点赞后的用户记录在Redis的set数据类型中
1. 准备pom环境 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scopeversion5.1.47/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.3/version/dependency!--hutool--dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.7.17/version/dependencydependencygroupIdorg.redisson/groupIdartifactIdredisson/artifactIdversion3.23.1/version/dependency2. 配置ThreadLocal和过滤器
public class UserHolder {private static final ThreadLocalUserDTO tl new ThreadLocal();public static void saveUser(UserDTO user){tl.set(user);}public static UserDTO getUser(){return tl.get();}public static void removeUser(){tl.remove();}
}Configuration
public class MvcConfig implements WebMvcConfigurer {Autowiredprivate StringRedisTemplate redis;Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).excludePathPatterns(/user/code,/user/login,/blog/hot,/shop/**,/shop-type/**,/voucher/**).order(2);registry.addInterceptor(new RefreshTokenInterceptor(redis)).addPathPatterns(/**).order(1);}
}
---------------------------------------------
Slf4j
public class LoginInterceptor implements HandlerInterceptor {//controller执行之前Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//1.判断是否需要拦截ThreadLocalif (UserHolder.getUser()null) {response.setStatus(401);return false;}//7.放行return true;}//渲染后返回给前台数据前Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {//移除用户避免内存泄露UserHolder.removeUser();}
}
---------------------------------------------------
Slf4j
public class RefreshTokenInterceptor implements HandlerInterceptor {//这个对象不是由spring管理的所以不能用注解自动注入private StringRedisTemplate redis;public RefreshTokenInterceptor(StringRedisTemplate redis) {this.redis redis;}//controller执行之前Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//1.获取请求头中的tokenString token request.getHeader(authorization);if (StrUtil.isBlank(token)) {return true;}//2.基于token获取redis中的用户//通过key取到hash中的map集合数据MapObject, Object userMap redis.opsForHash().entries(login:token: token);//3.判断用户是否存在if (userMap.isEmpty()) {return true;}//5.将查询到的hash数据转为userDto对象UserDTO userDTO BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false);//6.存在保存用户信息到ThreadLocal中UserHolder.saveUser(userDTO);//7.刷新token有效期redis.expire(LOGIN_USER_KEY token, 30, TimeUnit.MINUTES);log.info(我是第一个拦截器当前拦截所有请求的用户为线程为{},{},UserHolder.getUser(),Thread.currentThread());//8.放行return true;}3. Controller层负责接收请求和向下分配
RestController
RequestMapping(/blog)
public class BlogController{Resourceprivate IBlogService blogService;PutMapping(/like/{id})public Result likeBlog(PathVariable(id) Long id) {return blogService.likeBlog(id);}
}4. Service层负责业务的处理逻辑点赞功能将文章的点赞用户以时间戳为分数存入Redis
Service
public class BlogServiceImpl extends ServiceImplBlogMapper, Blog implements IBlogService {Autowiredprivate IUserService userService;Resourceprivate StringRedisTemplate redis;Overridepublic Result likeBlog(Long id) {//1.获取登录用户Long userId UserHolder.getUser().getId();//2.判断当前用户是否已经点赞String key blog:liked: id;//获取当前登录用户的分数若文章中的用户id分数为null说明未点赞Double score redis.opsForZSet().score(key, userId.toString());if (score null) {//3.如果未点赞可以点赞//3.1 点赞1boolean isSuccess update().setSql(liked liked 1).eq(id, id).update();//3.2保存当前点赞用户到Redis的文章set集合中文章set集合中记录的是点赞用户的id//分数是时间戳可以进行排序if (isSuccess) {//存入Redis的分数值以当前时间戳存入redis.opsForZSet().add(key, userId.toString(), System.currentTimeMillis());}} else {//4.如果已点赞取消点赞//4.1点赞-1boolean isSuccess update().setSql(liked liked -1).eq(id, id).update();//4.2把用户从Redis的set集合移除if (isSuccess) {redis.opsForZSet().remove(key, userId.toString());}}return null;}
}5. 上述为下面的排行榜做铺垫 查询当前文章的点赞排行榜id是文章id号 PutMapping(/likes/{id})public Result likesBlog(PathVariable(id) Long id) {return blogService.queryBlogLikes(id);}Overridepublic Result queryBlogLikes(Long id) {String keyblog:liked: id;//取出前五条数据SetString rangeData redis.opsForZSet().range(key, 0, 4);if (rangeDatanull) {return Result.ok(Collections.emptyList());}//将文章点赞的前五条用户id转换为Long类型ListLong ids rangeData.stream().map(Long::valueOf).collect(Collectors.toList());String idStr StrUtil.join(,, ids);//去数据库把这些用户查询出来并且数据脱敏返回给前端ListUser users userService.query().in(id,ids).last(order by field(id,idStr)).list();UserDTO userData BeanUtil.copyProperties(users, UserDTO.class);return Result.ok(userData);}
文章转载自: http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.qcymf.cn.gov.cn.qcymf.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.qyhcg.cn.gov.cn.qyhcg.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.knlgk.cn.gov.cn.knlgk.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.wdhzk.cn.gov.cn.wdhzk.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.kgxrq.cn.gov.cn.kgxrq.cn http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.qrqdr.cn.gov.cn.qrqdr.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn