第4章 Redis,一站式高性能存储方案【仿牛客网社区论坛项目】

第4章 Redis,一站式高性能存储方案【仿牛客网社区论坛项目】

  • 前言
  • 推荐
  • 项目总结
    • 第4章 Redis,一站式高性能存储方案
      • 1. Redis入门
      • 2. Spring整合Redis
      • 3.点赞
      • 4.我收到的赞
      • 5.关注、取消关注
      • 6.关注列表、粉丝列表
      • 7.优化登录模块
  • 最后

前言

2023-4-30 20:42:51

以下内容源自【Java面试项目】
仅供学习交流使用

推荐

仿牛客网项目【面试】

项目总结

第4章 Redis,一站式高性能存储方案

1. Redis入门

2. Spring整合Redis

3.点赞

   @RequestMapping(path = "/like", method = RequestMethod.POST)@ResponseBodypublic String like(int entityType, int entityId, int entityUserId, int postId) {//这里需要权限检查User user = hostHolder.getUser();// 点赞likeService.like(user.getId(), entityType, entityId, entityUserId);// 数量long likeCount = likeService.findEntityLikeCount(entityType, entityId);// 状态int likeStatus = likeService.findEntityLikeStatus(user.getId(), entityType, entityId);// 返回的结果Map<String, Object> map = new HashMap<>();map.put("likeCount", likeCount);map.put("likeStatus", likeStatus);// 触发点赞事件if (likeStatus == 1) {Event event = new Event().setTopic(TOPIC_LIKE).setUserId(hostHolder.getUser().getId()).setEntityType(entityType).setEntityId(entityId).setEntityUserId(entityUserId).setData("postId", postId);eventProducer.fireEvent(event);}if(entityType == ENTITY_TYPE_POST) {// 计算帖子分数String redisKey = RedisKeyUtil.getPostScoreKey();redisTemplate.opsForSet().add(redisKey, postId);}return CommunityUtil.getJSONString(0, null, map);}

4.我收到的赞

5.关注、取消关注

   @RequestMapping(path = "/follow", method = RequestMethod.POST)@ResponseBodypublic String follow(int entityType, int entityId) {User user = hostHolder.getUser();followService.follow(user.getId(), entityType, entityId);// 触发关注事件Event event = new Event().setTopic(TOPIC_FOLLOW).setUserId(hostHolder.getUser().getId()).setEntityType(entityType).setEntityId(entityId).setEntityUserId(entityId);eventProducer.fireEvent(event);return CommunityUtil.getJSONString(0, "已关注!");}//取消关注@RequestMapping(path = "/unfollow", method = RequestMethod.POST)@ResponseBodypublic String unfollow(int entityType, int entityId) {User user = hostHolder.getUser();followService.unfollow(user.getId(), entityType, entityId);return CommunityUtil.getJSONString(0, "已取消关注!");}

6.关注列表、粉丝列表

   //得到关注者@RequestMapping(path = "/followees/{userId}", method = RequestMethod.GET)public String getFollowees(@PathVariable("userId") int userId, Page page, Model model) {User user = userService.findUserById(userId);if (user == null) {throw new RuntimeException("该用户不存在!");}model.addAttribute("user", user);//分页信息page.setLimit(5);page.setPath("/followees/" + userId);page.setRows((int) followService.findFolloweeCount(userId, ENTITY_TYPE_USER));//查询关注者List<Map<String, Object>> userList = followService.findFollowees(userId, page.getOffset(), page.getLimit());if (userList != null) {for (Map<String, Object> map : userList) {User u = (User) map.get("user");map.put("hasFollowed", hasFollowed(u.getId()));}}model.addAttribute("users", userList);return "site/followee";}//得到粉丝@RequestMapping(path = "/followers/{userId}", method = RequestMethod.GET)public String getFollowers(@PathVariable("userId") int userId, Page page, Model model) {User user = userService.findUserById(userId);if (user == null) {throw new RuntimeException("该用户不存在!");}model.addAttribute("user", user);//分页信息page.setLimit(5);page.setPath("/followers/" + userId);page.setRows((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));//查询粉丝List<Map<String, Object>> userList = followService.findFollowers(userId, page.getOffset(), page.getLimit());if (userList != null) {for (Map<String, Object> map : userList) {User u = (User) map.get("user");map.put("hasFollowed", hasFollowed(u.getId()));}}model.addAttribute("users", userList);return "site/follower";}

7.优化登录模块

 	@RequestMapping(path = "/kaptcha",method = RequestMethod.GET)public void getKaptcha(HttpServletResponse response/*, HttpSession session*/){//生成验证码String text = kaptchaProducer.createText();BufferedImage image = kaptchaProducer.createImage(text);//将验证码存入session
//        session.setAttribute("kaptcha",text);//验证码的归属者String kaptchaOwner= CommunityUtil.generateUUID();Cookie cookie=new Cookie("kaptchaOwner",kaptchaOwner);cookie.setMaxAge(60);cookie.setPath(contextPath);response.addCookie(cookie);// 将验证码存入redisString redisKey= RedisKeyUtil.getKaptchaKey(kaptchaOwner);redisTemplate.opsForValue().set(redisKey,text,60, TimeUnit.SECONDS);//将图片输出给浏览器response.setContentType("image/png");try {ServletOutputStream os = response.getOutputStream();ImageIO.write(image,"png",os);} catch (IOException e) {logger.error("响应验证码失败:"+e.getMessage());}}@RequestMapping(path = "/login", method = RequestMethod.POST)public String login(String username, String password, String code, boolean rememberme,Model model, /*HttpSession session,*/ HttpServletResponse response,@CookieValue("kaptchaOwner") String kaptchaOwner) {//检查验证码
//         String kaptcha = (String) session.getAttribute("kaptcha");String kaptcha=null;//Cookie-->kaptchaOwner-->(Redis)kaptchaif (StringUtils.isNotBlank(kaptchaOwner)){String redisKey=RedisKeyUtil.getKaptchaKey(kaptchaOwner);kaptcha= (String) redisTemplate.opsForValue().get(redisKey);}if (StringUtils.isBlank(kaptcha) || StringUtils.isBlank(code) || !kaptcha.equalsIgnoreCase(code)) {model.addAttribute("codeMsg", "验证码不正确!");return "site/login";}// 检查账号,密码int expiredSeconds = rememberme ? REMEMBER_EXPIRED_SECONDS : DEFAULT_EXPIRED_SECONDS;Map<String, Object> map = userService.login(username, password, expiredSeconds);if (map.containsKey("ticket")) {Cookie cookie = new Cookie("ticket", map.get("ticket").toString());cookie.setPath(contextPath);cookie.setMaxAge(expiredSeconds);response.addCookie(cookie);return "redirect:/index";} else {model.addAttribute("usernameMsg", map.get("usernameMsg"));model.addAttribute("passwordMsg", map.get("passwordMsg"));return "site/login";}}@RequestMapping(path = "/logout", method = RequestMethod.GET)public String logout(@CookieValue("ticket") String ticket) {userService.logout(ticket);SecurityContextHolder.clearContext();return "redirect:/login";}

最后

这篇博客能写好的原因是:站在巨人的肩膀上

这篇博客要写好的目的是:做别人的肩膀

开源:为爱发电

学习:为我而行

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/12572.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hadoop 安装步骤

hadoop 是一个免费开源软件, 可以安装在window上&#xff0c;但是有些麻烦。 也可以安装 在linux 上 hadoop 下载地址 &#xff1a;https://hadoop.apache.org/releases.html 安装前的准备工作&#xff1a; 1.安装jdk Apache Hadoop 与最新版本的JDK不兼容。建议下载Java SE D…

SFTPGO 整合minio AD群组 测试 |sftpgo with minio and ldap group test

SFTP-GO 研究 最近在测试sftpgo&#xff0c;发现中文的资料比较少&#xff0c;在企业中很多存储开始支持S3&#xff0c;比如netapp 于是想尝试把文件服务器换成sftpgoS3的存储&#xff0c;sftp go和AD 群组的搭配测试比较少 自己测试了一把&#xff0c;觉得还是没有server-u的A…

JVS物联网、无忧企业文档、规则引擎5.14功能新增说明

项目介绍 JVS是企业级数字化服务构建的基础脚手架&#xff0c;主要解决企业信息化项目交付难、实施效率低、开发成本高的问题&#xff0c;采用微服务配置化的方式&#xff0c;提供了 低代码数据分析物联网的核心能力产品&#xff0c;并构建了协同办公、企业常用的管理工具等&am…

ubuntu在当前路径下打开Terminal

在 Ubuntu 20.04 中&#xff0c;nautilus-open-terminal 已经被 nautilus-extension-gnome-terminal 替代了。你可以尝试安装这个新的包。以下是在终端中执行的命令&#xff1a; sudo apt-get update sudo apt-get install nautilus-extension-gnome-terminal安装完成后&#…

Java面向对象——抽象类

abstract修饰符可以用来修饰方法也可以修饰类&#xff0c;如果修饰方法&#xff0c;那么该方法就是抽象方法&#xff1b;如果修饰类&#xff0c;那么该类就是抽象类。 抽象类中可以没有抽象方法&#xff0c;但是有抽象方法的类一定要声明为抽象类。 抽象类&#xff0c;不能…

函数的递归调用

在调用一个函数的过程中又出现直接或间接地调用该函数本身&#xff0c;称为函数的递归&#xff08;recursive&#xff09;调用。C和C允许函数的递归调用。例如&#xff1a; int f(int x) { int y,z; zf(y); //在调用函数 f 的过程中&…

云服务器修改端口通常涉及几个步骤

云服务器修改端口通常涉及几个步骤 远程连接并登录到Linux云服务器&#xff1a; 使用SSH工具&#xff08;如PuTTY、SecureCRT等&#xff09;远程连接到云服务器。 输入云服务器的IP地址、用户名和密码&#xff08;或密钥&#xff09;进行登录。 修改SSH配置文件&#xff1a…

Jmeter使用While控制器

1.前言 对于性能测试场景中&#xff0c;需要用”执行某个事物&#xff0c;直到一个条件停止“的概念时&#xff0c;While控制器控制器无疑是首选&#xff0c;但是在编写脚本时&#xff0c;经常会出现推出循环异常&#xff0c;获取参数异常等问题&#xff0c;下面总结两种常用的…

如何将Excel表格中的图片链接直接显示成图片?

在 Excel 中&#xff0c;你可以通过以下步骤将图片链接转换为直接显示图片&#xff1a; 1. **插入图片链接**&#xff1a;首先&#xff0c;在 Excel 表格中插入图片的链接。你可以在某个单元格中输入图片的链接地址&#xff0c;或者使用 Excel 的“插入图片”功能插入链接。 2.…

从新手到高手,教你如何改造你的广告思维方式!

想要广告震撼人心又让人长时间记住&#xff1f;答案肯定是“创意”二字。广告创意&#xff0c;说白了就是脑洞大开&#xff0c;想法新颖。那些很流行的广告&#xff0c;都是因为背后的想法特别、新颖。做广告啊&#xff0c;就得不停地思考&#xff0c;创新思维是关键。 广告思…

天锐绿盾 | 如何防止电脑内文件遭到泄露?

天锐绿盾是一款专为企业设计的数据防泄漏软件系统&#xff0c;它通过一系列综合性的安全措施来有效防止电脑内文件遭到泄露。 PC地址&#xff1a; https://isite.baidu.com/site/wjz012xr/2eae091d-1b97-4276-90bc-6757c5dfedee 以下是天锐绿盾防止文件泄露的主要功能和方法&a…

qt 麒麟系统 connot find /usr/local/lib

目录 解决方法&#xff1a; 后来又报错&#xff1a; cannot find -lopencv_world3.4.6 connot find /usr/local/lib 解决方法&#xff1a; LIBS -L/usr/local/lib -lopencv_world3.4.6QMAKE_LFLAGS -Wl,-rpath,/usr/local/lib 后来又报错&#xff1a; cannot find -lopencv…

【CSP CCF记录】202009-1 称检测点查询

题目 过程 难点&#xff1a;编号和位置的一一对应&#xff0c;不同位置的距离可能相等。 所以使用一个结构体记录不同检测点的编号和到居民地的距离。 sort函数进行排序。Sort函数使用方法 参考&#xff1a;http://t.csdnimg.cn/Y0Hpi 代码 #include <bits/stdc.h>…

Vue3.0-Ref

一、值类型与引用类型 1.1 定义和说明 在JavaScript中&#xff0c;数据类型可以分为两类&#xff1a;值类型&#xff08;或基本数据类型&#xff09;和引用类型。 值类型&#xff08;基本数据类型&#xff09;&#xff1a; undefined null boolean number string symbo…

正则表达式和lambda表达式

正则表达式&#xff08;Regular Expressions&#xff09;和Lambda表达式虽然都包含“表达式”一词&#xff0c;但它们在编程中的作用和用法是完全不同的。让我们详细比较一下它们的定义、用途和应用场景&#xff1a; 正则表达式 定义&#xff1a;正则表达式是一种用于匹配文本…

人工智能AI聊天chatgpt系统openai对话创作文言一心源码APP小程序功能介绍

你提到的是一个集成了多种智能AI创作能力的系统&#xff0c;它结合了OpenAI的ChatGPT、百度的文言一心&#xff08;ERNIE Bot&#xff09;以及可能的微信WeLM&#xff08;或其他类似接口&#xff09;等。这样的系统确实能够极大地提高创作效率&#xff0c;并且在各种场景下为用…

Rust Web开发框架actix-web入门案例

概述 在看书的时候&#xff0c;用到了actix-web这个框架的案例。 书里面的版本是1.0&#xff0c;但是我看官网最新都4.4了。 为了抹平这种信息差&#xff0c;所以我决定把官方提供的示例代码过一遍。 核心代码 Cargo.toml [package] name "hello" version &q…

VueRouter使用总结

VueRouter 是 Vue.js 的官方路由管理器&#xff0c;用于构建单页面应用&#xff08;SPA&#xff09;。在使用 VueRouter 时&#xff0c;开发者可以定义路由映射规则&#xff0c;并在 Vue 组件中通过编程式导航或声明式导航的方式控制页面的跳转和展示。以下是 VueRouter 使用的…

随笔:贝特弹琴

半年前&#xff0c;我买了一架朗朗代言的智能电子琴。所谓智能是指&#xff0c;它配套的手机软件知道你在按哪个键&#xff0c;它还能让任意按键发光。用专业术语说&#xff0c;它的键盘具有输入和输出功能&#xff0c;和软件组合起来是一个完整的计算机系统。 随着软件练习曲…