Redis帖子相关功能实现 (发布笔记查看笔记点赞功能点赞排行榜)
Posted Perceus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis帖子相关功能实现 (发布笔记查看笔记点赞功能点赞排行榜)相关的知识,希望对你有一定的参考价值。
(目录)
帖子相关功能
1. 发布笔记
发布帖子
探店笔记类似点评网站的评价,往往是图文结合
。对应的表有两个:
具体发布流程
上传图片接口
@Slf4j
@RestController
@RequestMapping("upload")
public class UploadController
@PostMapping("blog")
public Result uploadImage(@RequestParam("file") MultipartFile image)
try
// 获取原始文件名称
String originalFilename = image.getOriginalFilename();
// 生成新文件名
String fileName = createNewFileName(originalFilename);
// 保存文件
image.transferTo(new File(SystemConstants.IMAGE_UPLOAD_DIR, fileName));
// 返回结果
log.debug("文件上传成功,", fileName);
return Result.ok(fileName);
catch (IOException e)
throw new RuntimeException("文件上传失败", e);
注意:
保存博文接口
@RestController
@RequestMapping("/blog")
public class BlogController
@Resource
private IBlogService blogService;
@PostMapping
public Result saveBlog(@RequestBody Blog blog)
//获取登录用户
UserDTO user = UserHolder.getUser();
blog.setUpdateTime(user.getId());
//保存探店博文
blogService.saveBlog(blog);
//返回id
return Result.ok(blog.getId());
2. 查看探店笔记
实现代码:
@Override
public Result queryBlogById(Long id)
// 1.查询blog
Blog blog = getById(id);
if (blog == null)
return Result.fail("笔记不存在!");
// 2.查询blog有关的用户
queryBlogUser(blog);
return Result.ok(blog);
private void queryBlogUser(Blog blog)
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
3. 点赞功能
初始代码
@GetMapping("/likes/id")
public Result queryBlogLikes(@PathVariable("id") Long id)
//修改点赞数量
blogService.update().setSql("liked = liked +1 ").eq("id",id).update();
return Result.ok();
问题分析:
完善点赞功能
需求:
这里就不使用MySQL数据库建表那个逻辑,使用Redis
实现步骤:
为什么采用set集合:
具体步骤:
@TableField(exist = false)
private Boolean isLike;
@Override
public Result likeBlog(Long id)
// 1.获取登录用户
Long userId = UserHolder.getUser().getId();
// 2.判断当前登录用户是否已经点赞
String key = BLOG_LIKED_KEY + id;
Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
//3.如果未点赞,可以点赞
if (BooleanUtil.isFalse(isMember))
//3.1 数据库点赞数+1
boolean isSuccess = update().setSql("liked = liked + 1").eq("id", id).update();
//3.2 保存用户到Redis的set集合
if (isSuccess)
stringRedisTemplate.opsForSet().add(key, userId.toString());
else
//4.如果已点赞,取消点赞
//4.1 数据库点赞数-1
boolean isSuccess = update().setSql("liked = liked - 1").eq("id", id).update();
//4.2 把用户从Redis的set集合移除
if (isSuccess)
stringRedisTemplate.opsForSet().remove(key, userId.toString());
return Result.ok();
4. 点赞排行榜
在探店笔记的详情页面,应该把给该笔记点赞的人显示出来,比如最早点赞的TOP5,形成点赞排行榜
:
我们接下来来对比一下这些集合的区别是什么?
修改代码之前:BlogServiceImpl类实现
点赞逻辑
:
@Override
public Result likeBlog(Long id)
// 1.获取登录用户
Long userId = UserHolder.getUser().getId();
// 2.判断当前登录用户是否已经点赞
String key = BLOG_LIKED_KEY + id;
Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
if (score == null)
// 3.如果未点赞,可以点赞
// 3.1.数据库点赞数 + 1
boolean isSuccess = update().setSql("liked = liked + 1").eq("id", id).update();
// 3.2.保存用户到Redis的set集合 zadd key value score
if (isSuccess)
stringRedisTemplate.opsForZSet().add(key, userId.toString(), System.currentTimeMillis());
else
// 4.如果已点赞,取消点赞
// 4.1.数据库点赞数 -1
boolean isSuccess = update().setSql("liked = liked - 1").eq("id", id).update();
// 4.2.把用户从Redis的set集合移除
if (isSuccess)
stringRedisTemplate.opsForZSet().remove(key, userId.toString());
return Result.ok();
private void isBlogLiked(Blog blog)
// 1.获取登录用户
UserDTO user = UserHolder.getUser();
if (user == null)
// 用户未登录,无需查询是否点赞
return;
Long userId = user.getId();
// 2.判断当前登录用户是否已经点赞
String key = "blog:liked:" + blog.getId();
Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
blog.setIsLike(score != null);
BlogController接口
@GetMapping("/likes/id")
public Result queryBlogLikes(@PathVariable("id") Long id)
return blogService.queryBlogLikes(id);
BlogService类实现
@Override
public Result queryBlogLikes(Long id)
String key = BLOG_LIKED_KEY + id;
// 1.查询top5的点赞用户 zrange key 0 4
Set<String> top5 = stringRedisTemplate.opsForZSet().range(key, 0, 4);
if (top5 == null || top5.isEmpty())
return Result.ok(Collections.emptyList());
// 2.解析出其中的用户id
List<Long> ids = top5.stream().map(Long::valueOf).collect(Collectors.toList());
String idStr = StrUtil.join(",", ids);
// 3.根据用户id查询用户 WHERE id IN ( 5 , 1 ) ORDER BY FIELD(id, 5, 1)
List<UserDTO> userDTOS = userService.query()
.in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list()
.stream()
.map(user -> BeanUtil.copyProperties(user, UserDTO.class))
.collect(Collectors.toList());
// 4.返回
return Result.ok(userDTOS);
完整的BlogServiceImpl代码:
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService
@Resource
private IUserService userService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public Result queryBlogById(Long id)
// 1.查询blog
Blog blog = getById(id);
if (blog == null)
return Result.fail("笔记不存在!");
// 2.查询blog有关的用户
queryBlogUser(blog);
// 3. 查询是否点赞
isBlogLiked(blog);
return Result.ok(blog);
@Override
public Result queryHotBlog(Integer current)
// 根据用户查询
Page<Blog> page = query().orderByDesc("liked")
.page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
// 获取当前页数据
List<Blog> records = page.getRecords();
// 查询用户
records.forEach(blog ->
this.queryBlogUser(blog); // 查用户
this.isBlogLiked(blog); // 查是否点赞
);
return Result.ok(records);
@Override
public Result likeBlog(Long id)
// 1.获取登录用户
Long userId = UserHolder.getUser().getId();
// 2.判断当前登录用户是否已经点赞
String key = BLOG_LIKED_KEY + id;
Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
//3.如果未点赞,可以点赞
if (score == null)
//3.1 数据库点赞数+1
boolean isSuccess = update().setSql("liked = liked + 1").eq("id", id).update();
//3.2 保存用户到Redis的Zset集合 zadd key value score
if (isSuccess)
stringRedisTemplate.opsForZSet().add(key, userId.toString(),System.currentTimeMillis());
else
//4.如果已点赞,取消点赞
//4.1 数据库点赞数-1
boolean isSuccess = update().setSql("liked = liked - 1").eq("id", id).update();
//4.2 把用户从Redis的Zset集合移除
if (isSuccess)
stringRedisTemplate.opsForZSet().remove(key, userId.toString());
return Result.ok();
/**
* 查询 TOP5 点赞用户列表
* @param id
* @return
*/
@Override
public Result queryBlogLikes(Long id)
String key = BLOG_LIKED_KEY + id;
// 1.查询top5的点赞用户 zrange key 0 4
Set<String> top5 = stringRedisTemplate.opsForZSet().range(key, 0, 4);
if (top5 == null || top5.isEmpty())
return Result.ok(Collections.emptyList());
System.out.println(top5);
// 2.解析出其中的用户id
List<Long> ids = top5.stream().
map(Long::valueOf)
.collect(Collectors.toList());
String idStr = StrUtil.join(",", ids);
// 3.根据用户id查询用户 WHERE id IN ( 5 , 1 ) ORDER BY FIELD(id, 5, 1)
List<UserDTO> userDTOS = userService.query()
.in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list() // 这里是因为 in 查询顺序不能保证,所以使用 ORDER BY FIELD()
.stream()
.map(user -> BeanUtil.copyProperties(user, UserDTO.class))
.collect(Collectors.toList());
// 4.返回
return Result.ok(userDTOS);
private void queryBlogUser(Blog blog)
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
private void isBlogLiked(Blog blog)
// 1.获取登录用户
UserDTO user = UserHolder.getUser();
if(user == null)
// 用户未登录 无需查询
return;
Long userId = user.getId();
// 2.判断当前登录用户是否已经点赞
String key = BLOG_LIKED_KEY + blog.getId();
Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
blog.setIsLike(score != null );
以上是关于Redis帖子相关功能实现 (发布笔记查看笔记点赞功能点赞排行榜)的主要内容,如果未能解决你的问题,请参考以下文章