鹰潭公司做网站,wordpress登陆sql代码,杭州网络,哪个地区的网站建设最好1 实现点赞功能显示哪些用户点赞过并安装时间顺序排序
使用sort_set 进行存储#xff0c;把博客id作为key#xff0c;用户id作为value#xff0c;时间戳作为score
但存储成功之后还是没有成功按照时间顺序排名#xff0c;因为sql语句#xff0c;比如最后in#xff08;5…1 实现点赞功能显示哪些用户点赞过并安装时间顺序排序
使用sort_set 进行存储把博客id作为key用户id作为value时间戳作为score
但存储成功之后还是没有成功按照时间顺序排名因为sql语句比如最后in51、
我们要按照用户id5和3和1来显示但sql会默认显示135要修改sql语句order byfiledid513按照自己定义的数据
Overridepublic Result like(Long id) {String key RedisConstants.BLOG_LIKED_KEY id;Blog blog getById(id);//获取登录用户Long userId UserHolder.getUser().getId();//判断当前登录用户是否点赞Double isMember stringRedisTemplate.opsForZSet().score(key,userId.toString());//如果未点赞可以点if(isMember null){//1boolean isUpdate update().set(liked, blog.getLiked() 1).eq(id, id).update();if(BooleanUtil.isTrue(isUpdate)){//zadd key value scorestringRedisTemplate.opsForZSet().add(key,userId.toString(),System.currentTimeMillis());}}else {stringRedisTemplate.opsForZSet().remove(key,userId.toString());//如果已经点赞则取消boolean isUpdate update().set(liked, blog.getLiked() - 1).eq(id, id).update();//-1//从redis中去除}return null;}
Overridepublic Result queryBlogLikes(Long id) {//查询前五点赞的人SetString top5 stringRedisTemplate.opsForZSet().range(RedisConstants.BLOG_LIKED_KEY id, 0, 4);if(top5 null || top5.isEmpty()){return Result.ok(Collections.emptyList());}//解析出用户idListLong ids top5.stream().map(Long::valueOf).collect(Collectors.toList());ListUserDTO userDTOS new ArrayList();//根据id查出用户ids.forEach(userId - {User user userService.getById(userId);UserDTO userDTO new UserDTO();BeanUtils.copyProperties(user,userDTO);userDTOS.add(userDTO);});return Result.ok(userDTOS);} 2 使用set集合记录共同关注
每个人关注时往以自己id为key的set集合里面添加被关注的人的id查看另一个用户的共同关注时可以使用set集合的intersect查看交集id再通过id流操作得到被关注的User对象 Overridepublic Result followCommons(Long id) {if (UserHolder.getUser() ! null) {Long userId UserHolder.getUser().getId();String key follows: userId;String key2 follows id;SetString intersect stringRedisTemplate.opsForSet().intersect(key,key2);if(intersect null || intersect.isEmpty()){return Result.ok(Collections.emptyList());}//解析id集//使用流操作ListLong ids intersect.stream().map(Long::valueOf).collect(Collectors.toList());ListUserDTO collects userService.listByIds(ids).stream().map(user - {return BeanUtil.copyProperties(user, UserDTO.class);}).collect(Collectors.toList());return Result.ok(collects);}return Result.fail(共同关注发生问题);}
} 3 使用sortedset记录滚动分页查询
修改代码在有用户保存发送新的博客时将查询数据库中他的所有粉丝并以粉丝为key博客id为value当前时间戳为为score进行保存在粉丝点击自己的关注时将按照时间戳的从大到小进行分页查询记录上一次查询到什么数据将其时间戳的下一个作为下一次的起始再加上偏移量zset默认按照score从小到大进行排序 SetZSetOperations.TypedTupleString typedTuples stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(RedisConstants.FEED_KEY userId, 0, max, offset, 2); 查找按照分数反向排序0 - max范围内的2个数据offset就是从第一个下面offset个开始比如为55421
按照当前查找是得到55第二个5
然后max 变成第二个5
然后后面再从中查找找到的是第一个5所以要加上偏移量1也就是从4开始
Overridepublic Result saveBlog(Blog blog) {UserDTO user UserHolder.getUser();blog.setUserId(user.getId());// 保存探店博文blogService.save(blog);//查询笔记作者的粉丝ListFollow follows followService.lambdaQuery().eq(Follow::getFollowUserId, user.getId()).list();//推送笔记id给所有粉丝for(Follow follow : follows){Long userId follow.getUserId();String key RedisConstants.FEED_KEY userId;stringRedisTemplate.opsForZSet().add(key,blog.getId().toString(),System.currentTimeMillis());}// 返回idreturn Result.ok(blog.getId());}Overridepublic Result queryBlogOfFollow(Long max, Integer offset) {Long userId UserHolder.getUser().getId();//查询收件箱SetZSetOperations.TypedTupleString typedTuples stringRedisTemplate.opsForZSet().reverseRangeByScoreWithScores(RedisConstants.FEED_KEY userId, 0, max, offset, 2);if(typedTuples null || typedTuples.isEmpty()){return Result.ok();}//解析数据 blogId,时间戳offsetListLong ids new ArrayList(typedTuples.size());Long minTime 0L;int os 1;for(ZSetOperations.TypedTupleString tuple : typedTuples){//获取idString idStr tuple.getValue();if (idStr ! null) {ids.add(Long.valueOf(idStr));}//获取分数时间戳long time Objects.requireNonNull(tuple.getScore()).longValue();if(time minTime){os;}else{minTime time;os 1;}}String idStr StrUtil.join(,,ids);//根据id查询blogListBlog blogs query().in(id, ids).last(ORDER BY FIELD(id, idStr )).list();for (Blog blog : blogs){User user userService.getById(userId);blog.setName(user.getNickName());blog.setIcon(user.getIcon());isLiked(blog);}ScrollResult r new ScrollResult();r.setList(blogs);r.setOffset(os);r.setMinTime(minTime);return Result.ok(r);}
}