此文记录了redis实现点赞功能
本文会随着作者日常学习进行补充及内容修正
思路:
1.用户查询当前用户点赞状态用于初始化
2.用户对某个博客进行点赞/取消点赞
3.如果点赞,则将该用户添加到redis set中的用户集
4.如果取消点赞,则删除用户集中的用户
5.提供当前点赞数查询的接口
1.Controller层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.lin.controller;
import com.lin.service.LikeService; import com.lin.vo.Result; import com.lin.vo.params.LikeParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("like") public class LikeAriticleController {
@Autowired LikeService likeService;
@PostMapping("num") public Result like(@RequestBody LikeParam likeParam){ Long num= likeService.findLikeNum(likeParam); return Result.success(num); }
@PostMapping("islike") public Result islike(@RequestBody LikeParam likeParam){ boolean flag = likeService.findLike(likeParam); return Result.success(flag); }
@PostMapping("initlike") public Result initlike(@RequestBody LikeParam likeParam){ boolean flag = likeService.selectLike(likeParam); return Result.success(flag); } }
|
2.LikeParam(DTO)
1 2 3 4 5 6 7 8 9
| package com.lin.vo.params;
import lombok.Data;
@Data public class LikeParam { private Long articleId; private Long account; }
|
3.Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.lin.vo;
import lombok.AllArgsConstructor; import lombok.Data;
@Data @AllArgsConstructor public class Result {
private boolean success;
private int code;
private String msg;
private Object data;
public static Result success(Object data){ return new Result(true,200,"success",data); }
public static Result fail(int code, String msg){ return new Result(false,code,msg,null); } }
|
4.Service层
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.lin.service;
import com.lin.vo.params.LikeParam;
public interface LikeService {
Long findLikeNum(LikeParam likeParam) ;
boolean findLike(LikeParam likeParam) ;
boolean selectLike(LikeParam likeParam) ; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package com.lin.service.impl;
import com.lin.dao.pojo.SysUser; import com.lin.service.LikeService; import com.lin.utils.UserThreadLocal; import com.lin.vo.params.LikeParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;
@Service public class LikeServiceImpl implements LikeService { @Autowired RedisTemplate redisTemplate; @Override public Long findLikeNum(LikeParam likeParam) { Long size = redisTemplate.opsForSet().size("articleId_"+likeParam.getArticleId()); return size; }
@Override public boolean findLike(LikeParam likeParam) { boolean bl; boolean flag = redisTemplate.opsForSet().isMember("articleId_"+likeParam.getArticleId(),"user_"+likeParam.getAccount()); if (flag) { redisTemplate.opsForSet().remove("articleId_"+likeParam.getArticleId(),"user_"+likeParam.getAccount()); bl = false; }else { redisTemplate.opsForSet().add("articleId_"+likeParam.getArticleId(),"user_"+likeParam.getAccount()); bl = true; } return bl; }
@Override public boolean selectLike(LikeParam likeParam) { boolean flag = redisTemplate.opsForSet().isMember("articleId_"+likeParam.getArticleId(),"user_"+likeParam.getAccount()); return flag; } }
|
5.application.yml
1 2 3 4
| spring: redis: host: localhost port: 6379
|
记得启动redis和导包!