第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.pswp.cn/web/12572.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/12572.shtml
英文地址,請注明出處:http://en.pswp.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;和軟件組合起來是一個完整的計算機系統。 隨著軟件練習曲…