1.redis 基本知识
redis5种基本数据
学习参考链接
缓存点赞信息的数据结构是set集合,不太了解的同学请参考上面的链接
2.实现逻辑
传入点赞人id,被点赞的id,以及点赞的状态status 0取消,1点赞
先查询redis中是否有缓存,无新建,有添加
3.实现代码
@PostMapping("/clickPraise")
public ResultVO clickPraise(HttpServletRequest request, @RequestBody String param) {
if (JSON.parseObject(param).containsKey("newsId")) {
if (JSON.parseObject(param).containsKey("status")) {
RegAlumniuser user = sysUserUtil.getUser(request);
String newsId = JSON.parseObject(param).getString("newsId");//新闻主键id
String status = JSON.parseObject(param).getString("status");//点赞状态
if (status.equals("1")) {//点赞状态为1 ,点赞
boolean b = redisUtil.hasKey("like_news" + newsId);
if (b == false) {//不存在,需新建
redisUtil.sSet("like_news" + newsId, newsId + "::" + user.getRegAlumniuserid());
}
redisUtil.sSetAndTime("like_news" + newsId, 3000, newsId + "::" + user.getRegAlumniuserid());//存在则向缓存中添加
} else {//取消点赞
redisUtil.setRemove("like_news" + newsId, newsId + "::" + user.getRegAlumniuserid());
}
boolean b = redisUtil.sHasKey("like_news" + newsId, newsId + "::" + user.getRegAlumniuserid());//判断value是否还在redis中
if (b == true) {
return new ResultVO(ResultCode.SUCCESS, 1);//点赞成功
}
return new ResultVO(ResultCode.SUCCESS, 0);//无点赞
}
return new ResultVO(ResultCode.FAILED, "请传入参数");
}
return new ResultVO(ResultCode.FAILED, "请传入参数");
}