使用Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊的詳細(xì)代碼
更新時(shí)間:2022年03月20日 08:43:43 作者:總是幸福的老豌豆
這篇文章主要介紹了Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊的詳細(xì)代碼,通過查詢某實(shí)體(帖子、評(píng)論等)點(diǎn)贊數(shù)量,需要用到事務(wù)相關(guān)知識(shí),結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
前言
異步實(shí)現(xiàn)
代碼實(shí)現(xiàn):
/** * * @param userId 點(diǎn)贊的人 * @param type 點(diǎn)贊與取消點(diǎn)贊的表示 * @param textId 文章ID * @param entityUserId -- 被點(diǎn)贊的人,文章作者 */ 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); //多個(gè)更新操作,需要事務(wù) operations.multi(); if (isMember) { //取消贊 redisTemplate.opsForSet().remove(entityLikeKey, userId); redisTemplate.opsForValue().decrement(userLikeKey); } else { //點(diǎn)贊 redisTemplate.opsForSet().add(entityLikeKey, userId); redisTemplate.opsForValue().increment(userLikeKey); } return operations.exec(); } }); } /** *查詢某實(shí)體(帖子,評(píng)論等)點(diǎn)贊數(shù)量 * @param type 1點(diǎn)贊,2評(píng)論。0表示取消點(diǎn)贊 * @param textId * @return */ private long findEntityLikeCount(int type, int textId){ String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId); return redisTemplate.opsForSet().size(entityLikeKey); } /** * 查詢某人對(duì)某文章的點(diǎn)贊狀態(tài) * @param textId 帖子ID * @param userId * @return */ private int findEntityLikeStatus(int textId,long userId){ String entityLikeKey = RedisKeyUtil.getEntityLikeKey(1, textId); //此處返回int,是為了進(jìn)行擴(kuò)展。比如擴(kuò)展踩,為止2.等等情況 return redisTemplate.opsForSet().isMember(entityLikeKey,userId)?1:0; } /** * 查詢某個(gè)用戶獲得贊,用于在個(gè)人主頁查看收獲了多少贊 * @param userId * @return */ private int findUserLikeCount(long userId){ String userLikeKey = RedisKeyUtil.getUserLikeKey(userId); Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey); // count.intValue()數(shù)據(jù)的整數(shù)形式; return count==null?0:count.intValue(); }
Redis–key設(shè)置
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"; /** *某個(gè)實(shí)體收到的贊,如帖子, * like:entity:entityType:entityId -> set(userId) 對(duì)應(yīng)set,存入userId * @param entityType * @param entityId * @return */ public static String getEntityLikeKey(int entityType, int entityId) { return PREFIX_ENTITY_LIKE + entityType + SPLIT + entityId; } *某個(gè)用戶收到的總贊數(shù) * like:user:userId ->long * @param userId public static String getUserLikeKey(long userId) { return PREFIX_USER_LIKE + SPLIT + userId; * 匯總某個(gè)帖子的評(píng)論數(shù)量 public static String getUserCommentsKey(int articleId) { return PREFIX_USER_COMMENTS + SPLIT + articleId;
到此這篇關(guān)于Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊的文章就介紹到這了,更多相關(guān)Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis發(fā)布訂閱_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了redis發(fā)布訂閱,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08關(guān)于Redis未授權(quán)訪問漏洞利用的介紹與修復(fù)建議
Redis是一個(gè)開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API,下面這篇文章主要給大家介紹了關(guān)于Redis未授權(quán)訪問漏洞利用的介紹和修復(fù)建議,文中介紹的非常詳細(xì),需要的朋友可以參考下。2017-07-07