Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Redis to realize like and cancel like

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Redis to like and cancel likes. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Code implementation: / * @ param userId likes person * @ param type likes and cancels likes expression * @ param textId article ID * @ param entityUserId-the person who is liked Author * / 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); / / multiple update operations, which requires transaction operations.multi (); if (isMember) {/ / unlikes redisTemplate.opsForSet () .remove (entityLikeKey, userId) RedisTemplate.opsForValue (). Decrement (userLikeKey);} else {/ / like redisTemplate.opsForSet (). Add (entityLikeKey, userId); redisTemplate.opsForValue (). Increment (userLikeKey);} return operations.exec ();}}) } / * query the number of likes given by an entity (posts, comments, etc.) * @ param type 1 likes, 2 comments. 0: cancel likes * @ param textId * @ return * / private long findEntityLikeCount (int type, int textId) {String entityLikeKey = RedisKeyUtil.getEntityLikeKey (type, textId); return redisTemplate.opsForSet () .size (entityLikeKey) } / * query someone's like status of an article * @ param textId post ID * @ param userId * @ return * / private int findEntityLikeStatus (int textId,long userId) {String entityLikeKey = RedisKeyUtil.getEntityLikeKey (1, textId); / / return int here for expansion. For example, expand the step, so far 2. Wait, return redisTemplate.opsForSet (). IsMember (entityLikeKey,userId)? 1 Integer count 0;} / * query a user to get likes, which is used to check how many likes have been received on the personal home page * @ param userId * @ return * / private int findUserLikeCount (long userId) {String userLikeKey = RedisKeyUtil.getUserLikeKey (userId); Integer count = (Integer) redisTemplate.opsForValue (). Get (userLikeKey) / / the integer form of count.intValue () data; return count==null?0:count.intValue ();}

Redis-key Settin

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" / * likes received by an entity, such as posts, * like:entity:entityType:entityId-> set (userId) corresponding to set, are stored in userId * @ param entityType * @ param entityId * @ return * / public static String getEntityLikeKey (int entityType, int entityId) {return PREFIX_ENTITY_LIKE + entityType + SPLIT + entityId } * Total number of likes received by a user * like:user:userId-> long * @ param userId public static String getUserLikeKey (long userId) {return PREFIX_USER_LIKE + SPLIT + userId; * summarizes the number of comments on a post public static String getUserCommentsKey (int articleId) {return PREFIX_USER_COMMENTS + SPLIT + articleId This is the end of the article on "how to use Redis to like and cancel likes". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report