redis——實戰關注

效果:?

思路:很好想,把自己的粉絲和自己關注的人都存起來(set即可),做增刪改查。

package com.now.community.community.service;import com.now.community.community.entity.User;
import com.now.community.community.util.CommunityConstant;
import com.now.community.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;import java.util.*;@Service
public class FollowService implements CommunityConstant {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate UserService userService;public void follow(int userId, int entityType, int entityId) {redisTemplate.execute(new SessionCallback() {@Overridepublic Object execute(RedisOperations operations) throws DataAccessException {String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);operations.multi();operations.opsForZSet().add(followeeKey, entityId, System.currentTimeMillis());operations.opsForZSet().add(followerKey, userId, System.currentTimeMillis());return operations.exec();}});}public void unfollow(int userId, int entityType, int entityId) {redisTemplate.execute(new SessionCallback() {@Overridepublic Object execute(RedisOperations operations) throws DataAccessException {String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);operations.multi();operations.opsForZSet().remove(followeeKey, entityId);operations.opsForZSet().remove(followerKey, userId);return operations.exec();}});}// 查詢關注的實體的數量public long findFolloweeCount(int userId, int entityType) {String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);return redisTemplate.opsForZSet().zCard(followeeKey);}// 查詢實體的粉絲的數量public long findFollowerCount(int entityType, int entityId) {String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);return redisTemplate.opsForZSet().zCard(followerKey);}// 查詢當前用戶是否已關注該實體public boolean hasFollowed(int userId, int entityType, int entityId) {String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);return redisTemplate.opsForZSet().score(followeeKey, entityId) != null;}// 查詢某用戶關注的人public List<Map<String, Object>> findFollowees(int userId, int offset, int limit) {String followeeKey = RedisKeyUtil.getFolloweeKey(userId, ENTITY_TYPE_USER);Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followeeKey, offset, offset + limit - 1);if (targetIds == null) {return null;}List<Map<String, Object>> list = new ArrayList<>();for (Integer targetId : targetIds) {Map<String, Object> map = new HashMap<>();User user = userService.findUserById(targetId);map.put("user", user);Double score = redisTemplate.opsForZSet().score(followeeKey, targetId);map.put("followTime", new Date(score.longValue()));list.add(map);}return list;}// 查詢某用戶的粉絲public List<Map<String, Object>> findFollowers(int userId, int offset, int limit) {String followerKey = RedisKeyUtil.getFollowerKey(ENTITY_TYPE_USER, userId);Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followerKey, offset, offset + limit - 1);if (targetIds == null) {return null;}List<Map<String, Object>> list = new ArrayList<>();for (Integer targetId : targetIds) {Map<String, Object> map = new HashMap<>();User user = userService.findUserById(targetId);map.put("user", user);Double score = redisTemplate.opsForZSet().score(followerKey, targetId);map.put("followTime", new Date(score.longValue()));list.add(map);}return list;}
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445155.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445155.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445155.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

NLP復習資料(5)-第九章 句法分析

NLP復習資料-第九章1.短語結構分析2.短語結構分析方法的評估指標3依存句法分析器設計P134依存句法分析器性能評價5短語結構與依存結構能夠相互轉換6漢英句法結構對比7漢語長句的層次化句法分析國科大&#xff0c;宗老師《自然語言處理》課程復習筆記&#xff0c;個人整理&#…

神經網絡中的優化算法總結

在調整模型更新權重和偏差參數的方式時&#xff0c;你是否考慮過哪種優化算法能使模型產生更好且更快的效果&#xff1f;應該用梯度下降&#xff0c;隨機梯度下降&#xff0c;還是Adam方法&#xff1f; 這篇文章介紹了不同優化算法之間的主要區別&#xff0c;以及如何選擇最佳的…

OS --written test1

241&#xff0e;在提供虛擬存儲的系統中&#xff0c;用戶的邏輯地址空間主要受( )的限制。A.內存空閑塊的大小 B&#xff0e;外存的大小 C&#xff0e;計算機編址范圍 D&#xff0e;頁表大小答案&#xff1a;C242&#xff0e;在分時系統中&#xff0c;時間片一定&#xff0c;( …

Java開發需要知道的HTML知識

概述 HTML(HyperText Markup Language) 不是一門編程語言&#xff0c;而是一種用來告知瀏覽器如何組織頁面的標記語言。 HTML 可復雜、可簡單&#xff0c;一切取決于開發者。它由一系列的元素組成&#xff0c;這些元素可以用來包圍不同部分的內容&#xff0c;使其以某種方式呈…

OS-written test2

操作系統各大公司筆試題匯總 1、在段頁式存儲管理中&#xff0c;其虛擬地址空間是&#xff08;&#xff09; A、一維 B、二維 C、三維 D、層次 答案&#xff1a;B 2、采用( )不會產…

Ubuntu18.04上安裝RTX 2080Ti顯卡驅動

文章目錄1.安裝Linux系統1.1下載Linux鏡像文件1.2 制作系統盤1.3 安裝Linux系統1.4 配置linux系統2.安裝英偉達顯卡驅動2.1 預備工作2.2 安裝顯卡驅動3.安裝cuda4.安裝cudnn1.安裝Linux系統 1.1下載Linux鏡像文件 根據電腦配置和自己的愛好&#xff0c;選擇合適的Linux鏡像文…

OS

操作系統筆試題及答案一 1&#xff0e;在下列系統中&#xff0c;( )是實時系統。 A.計算機激光照排系統 B.航空定票系統 C&#xff0e;辦公自動化系統 D.計算機輔助設計系統 答案&#xff1a;B 2&#xff0e;操作系統是一種( )。 A.應用軟件 B&#xff0e;系統軟件 C&#xff…

NLP復習資料(6)-第十章 語義分析

NLP復習資料-第十章1語義理論簡介2格語法4語義網絡5詞義消歧5語義角色標注6詞向量表示7篇章分析國科大&#xff0c;宗老師《自然語言處理》課程復習筆記&#xff0c;個人整理&#xff0c;僅供參考。語義分析&#xff1a;P3解釋句子或篇章的含義&#xff0c;主要困難&#xff08…

處理機調度與死鎖

處理機調度與死鎖 處理機調度的層次 高級調度 高級調度又稱為作業調度或長程調度&#xff0c;其主要功能是根據某種算法&#xff0c;把外存上處于后備隊列中的那些作業調入內存&#xff0c;也就是說&#xff0c;它的調度對象是作業。 1.作業和作業步 作業&#xff1a;一個比程…

各種卷積

從最開始的卷積層&#xff0c;發展至今&#xff0c;卷積已不再是當初的卷積&#xff0c;而是一個研究方向。在反卷積這篇博客中&#xff0c;介紹了一些常見的卷積的關系&#xff0c;本篇博客就是要梳理這些有趣的卷積結構。 閱讀本篇博客之前&#xff0c;建議將這篇博客結合在一…

springboot——kaptcha

導入包&#xff1a; <dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version></dependency> 配置類&#xff1a; package com.now.community.community.config;import…

NLP復習資料(7)-機器翻譯、文本分類、情感分析

NLP復習資料國科大&#xff0c;宗老師《自然語言處理》課程復習筆記&#xff0c;個人整理&#xff0c;僅供參考。此部分為手稿&#xff0c;高清圖下載見鏈接&#xff1a;https://download.csdn.net/download/sinat_40624829/11662412

XMl文件解析讀取

DOM方式&#xff1a; http://www.w3schools.com/dom/dom_parser.asp

redis——對項目的一些優化方案

這是我們之前項目的業務流程&#xff0c;做一下簡單介紹。 登錄&#xff1a; 用戶輸入賬號、密碼、驗證碼。我們先判斷用戶輸入的驗證碼是不是我們session存的驗證碼&#xff0c;然后去查賬號密碼是否正確。 如果登錄成功&#xff0c;發送給用戶一張憑證&#xff08;ticket&a…

const的思考

const的思考 1、什么是const?常類型是指使用類型修飾符const說明的類型&#xff0c;常類型的變量或對象的值是不能被更新的。&#xff08;當然&#xff0c;我們可以偷梁換柱進行更新&#xff1a;&#xff09;2、為什么引入const&#xff1f;const 推出的初始目的&#xff0c;正…

深度模型壓縮論文(02)- BlockSwap: Fisher-guided Block Substitution for Network Compression

文章目錄1.摘要和背景1.1 摘要1.2 背景2.方法和貢獻2.1 方法2.2 貢獻3.實驗和結果3.1 實驗3.2 結果4.總結和展望4.1 總結4.2 展望本系列是在閱讀深度神經網絡模型小型化方面論文時的筆記&#xff01;內容大部分從論文中摘取&#xff0c;也會有部分自己理解&#xff0c;有錯誤的…

NLP復習資料(8)-知識圖譜、信息抽取

NLP復習資料第16講—知識圖譜第17講-信息抽取&#xff08;知識圖譜生命周期中信息獲取的關鍵技術&#xff09;國科大&#xff0c;宗老師《自然語言處理》課程復習筆記&#xff0c;個人整理&#xff0c;僅供參考。第16講—知識圖譜 知識圖譜經典知識表示理論語義網資源描述框架…

Collection源碼閱讀

package java.util;import java.util.function.Predicate; import java.util.stream.Stream; import java.util.stream.StreamSupport;/*** 集合層次結構的根接口&#xff0c;一個集合表示一組對象&#xff0c;稱為元素* JDK不提供任何該接口的直接實現&#xff0c;JDK提供實現…

socket阻塞和非阻塞的區別

讀操作 對于阻塞的socket,當socket的接收緩沖區中沒有數據時,read調用會一直阻塞住,直到有數據到來才返 回。當socket緩沖區中的數據量小于期望讀取的數據量時,返回實際讀取的字節數。當sockt的接收緩沖 區中的數據大于期望讀取的字節數時,讀取期望讀取的字節數,返回實際讀…

深度模型壓縮論文(01)- Meta Filter Pruning to Accelerate Deep Convolutional Neural Networks

文章目錄1.摘要和介紹1.1摘要部分2.背景和方法2.1 背景2.2 貢獻2.3 方法3.實驗和結果3.1 實驗3.2 結果4.總結和展望4.1 總結4.2 展望本系列是在閱讀深度神經網絡模型小型化方面論文時的筆記&#xff01;內容大部分從論文中摘取&#xff0c;也會有部分自己理解&#xff0c;有錯誤…