網站首頁 編程語言 正文
前言
異步實現
代碼實現:
/** * * @param userId 點贊的人 * @param type 點贊與取消點贊的表示 * @param textId 文章ID * @param entityUserId -- 被點贊的人,文章作者 */ private void like(long userId,int type,int textId,long entityUserId){ redisTemplate.execute(new SessionCallback() { @Override public Object execute(RedisOperations operations) throws DataAccessException { String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId); String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId); boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId); //多個更新操作,需要事務 operations.multi(); if (isMember) { //取消贊 redisTemplate.opsForSet().remove(entityLikeKey, userId); redisTemplate.opsForValue().decrement(userLikeKey); } else { //點贊 redisTemplate.opsForSet().add(entityLikeKey, userId); redisTemplate.opsForValue().increment(userLikeKey); } return operations.exec(); } }); } /** *查詢某實體(帖子,評論等)點贊數量 * @param type 1點贊,2評論。0表示取消點贊 * @param textId * @return */ private long findEntityLikeCount(int type, int textId){ String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId); return redisTemplate.opsForSet().size(entityLikeKey); } /** * 查詢某人對某文章的點贊狀態 * @param textId 帖子ID * @param userId * @return */ private int findEntityLikeStatus(int textId,long userId){ String entityLikeKey = RedisKeyUtil.getEntityLikeKey(1, textId); //此處返回int,是為了進行擴展。比如擴展踩,為止2.等等情況 return redisTemplate.opsForSet().isMember(entityLikeKey,userId)?1:0; } /** * 查詢某個用戶獲得贊,用于在個人主頁查看收獲了多少贊 * @param userId * @return */ private int findUserLikeCount(long userId){ String userLikeKey = RedisKeyUtil.getUserLikeKey(userId); Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey); // count.intValue()數據的整數形式; return count==null?0:count.intValue(); }
Redis–key設置
public class RedisKeyUtil { private static final String SPLIT = ":"; private static final String PREFIX_ENTITY_LIKE = "like:entity"; private static final String PREFIX_USER_LIKE = "like:user"; private static final String PREFIX_USER_COMMENTS="comments:user"; /** *某個實體收到的贊,如帖子, * like:entity:entityType:entityId -> set(userId) 對應set,存入userId * @param entityType * @param entityId * @return */ public static String getEntityLikeKey(int entityType, int entityId) { return PREFIX_ENTITY_LIKE + entityType + SPLIT + entityId; } *某個用戶收到的總贊數 * like:user:userId ->long * @param userId public static String getUserLikeKey(long userId) { return PREFIX_USER_LIKE + SPLIT + userId; * 匯總某個帖子的評論數量 public static String getUserCommentsKey(int articleId) { return PREFIX_USER_COMMENTS + SPLIT + articleId;
原文鏈接:https://blog.csdn.net/qq_32370913/article/details/123552496
相關推薦
- 2022-05-10 二叉樹的遞歸和非遞歸遍歷
- 2022-04-03 對已有的docker容器增加新的端口映射問題(兩種方式)_docker
- 2022-11-05 Golang操作命令行的幾種方式總結_Golang
- 2023-02-07 Pytorch中torch.argmax()函數使用及說明_python
- 2023-03-11 Python基礎教程之增加和去除數字的千位分隔符_python
- 2022-12-12 C++?Boost?Bind庫示例分析使用_C 語言
- 2023-05-24 Django獲取前端數據的實現方式_python
- 2024-07-15 bootspring第三方資源配置管理
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支