Redis实现点赞功能模块

 行业动态     |      2019-10-16 20:34
public void likeArticle {
 validateParam; //参数验证
 logger.info;
 synchronized  {
 //只有未点赞的用户才可以进行点赞
 likeArticleLogicValidate;
 //1.用户总点赞数+1
 redisTemplate.opsForHash.increment, 1);
 //2.用户喜欢的文章+1
 String userLikeResult =  redisTemplate.opsForHash.get);
 Set Long articleIdSet = userLikeResult == null ? new HashSet  : FastjsonUtil.deserializeToSet;
 articleIdSet.add;
 redisTemplate.opsForHash.put, FastjsonUtil.serialize);
 //3.文章点赞数+1
 String articleLikedResult =  redisTemplate.opsForHash.get);
 Set Long likePostIdSet = articleLikedResult == null ? new HashSet  : FastjsonUtil.deserializeToSet;
 likePostIdSet.add;
 redisTemplate.opsForHash.put, FastjsonUtil.serialize);
 logger.info;
复制代码
public void unlikeArticle {
 validateParam; //参数校验
 logger.info;
 //1.用户总点赞数-1
 synchronized  {
 //只有点赞的用户才可以取消点赞
 unlikeArticleLogicValidate;
 Long totalLikeCount = Long.parseLongredisTemplate.opsForHash.get));
 redisTemplate.opsForHash.put, String.valueOf);
 //2.用户喜欢的文章-1
 String userLikeResult =  redisTemplate.opsForHash.get);
 Set Long articleIdSet = FastjsonUtil.deserializeToSet;
 articleIdSet.remove;
 redisTemplate.opsForHash.put, FastjsonUtil.serialize);
 //3.取消用户某篇文章的点赞数
 String articleLikedResult =  redisTemplate.opsForHash.get);
 Set Long likePostIdSet = FastjsonUtil.deserializeToSet;
 likePostIdSet.remove;
 redisTemplate.opsForHash.put, FastjsonUtil.serialize);
 logger.info;
复制代码
@Scheduled
public void redisDataToMySQL {
 logger.info.format);
 //1.更新文章总的点赞数
 Map String, String articleCountMap = redisTemplate.opsForHash.entries;
 for ) {
 String articleId = entry.getKey;
 Set Long userIdSet = FastjsonUtil.deserializeToSet, Long.class);
 //1.同步某篇文章总的点赞数到MySQL
 synchronizeTotalLikeCount;
 //2.同步用户喜欢的文章
 synchronizeUserLikeArticle;
 logger.info.format);
复制代码

说明:

十一过后对假期意犹未尽