Redis 笔记 03:高级结构和实战
这是本人根据黑马视频学习 Redis 的相关笔记,系列文章导航:《Redis设计与实现》笔记与汇总
点赞功能:Set
基本功能实现
需求:
实体类 Blog
:添加一个字段, 注解是 MyBatisPlus 的注解,表示不在表中
/**
* 是否点赞过了
*/
@TableField(exist = false)
private Boolean isLike;
Controller
:
@PutMapping("/like/{id}")
public Result likeBlog(@PathVariable("id") Long id) {
// 修改点赞数量
return blogService.likeBlog(id);
}
Service
:
@Override
public Result likeBlog(Long id) {
// 1. 获取登录用户
Long userId = UserHolder.getUser().getId();
// 2. 判断是否点赞
String key = RedisConstants.BLOG_LIKED_KEY + id;
Boolean isMember = stringRedistemplate.opsForSet().isMember(key, userId.toString());
// 3. 未点赞,点赞,修改数据库,保存用户到 Redis 中
if (BooleanUtil.isFalse(isMember)) {
boolean isSuccess = update().setsql("liked = liked + 1").eq("id", id).update();
if (isSuccess) {
stringRedistemplate.opsForSet().add(key, userId.toString());
}
} else {
// 4. 已经点赞,取消,修改数据库,删除用户
boolean isSuccess = update().setsql("liked = liked - 1").eq("id", id).update();
stringRedistemplate.opsForSet().remove(key, userId.toString());
}
return Result.ok();
}
点赞排行榜
按点赞时间先后排序,返回 Top5 的用户
ZADD
Zscore
ZRANGE
Service
:
@Override
public Result queryBlogLikes(Long id) {
// 1. 查询 top 5
// ZRANGE KEY 0 4
String key = RedisConstants.BLOG_LIKED_KEY + id;
Set<String> top5 = stringRedistemplate.opsForZSet().range(key, 0, 4);
if (top5 == null || top5.isEmpty()) {
return Result.ok();
}
// 2. 解析其中的用户id
List<Long> ids = top5.stream().map(Long::valueOf).collect(Collectors.toList());
// 3. 根据id查询用户
String join = StrUtil.join(",", ids);
List<UserDTO> userDTOS = userService.query().in("id", ids).last("ORDER BY FIELD (id," + join + ")").list()
.stream()
.map(user -> BeanUtil.copyProperties(user, UserDTO.class))
.collect(Collectors.toList());
// 4. 反回
return Result.ok(userDTOS);
}
好友关注:SortedSet
关注和取关
这里是用了一个 follow 表用来记录关注和被关注者的 id 信息
Service
:
@Override
public Result follow(Long followUserId, Boolean isFollow) {
// 判断是关注还是取关
Long userId = UserHolder.getUser().getId();
// 关注,新增
if (isFollow) {
Follow follow = new Follow();
follow.setUserId(userId);
follow.setFollowUserId(followUserId);
save(follow);
} else {
remove(new QueryWrapper<Follow>()
.eq("user_id", userId).eq("follow_user_id", followUserId));
}
// 取关,删除
return Result.ok();
}
@Override
public Result isFollow(Long followUserId) {
Long userId = UserHolder.getUser().getId();
Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
return Result.ok(count > 0);
}
共同关注
可以用 Set 结构进行判断
@Override
public Result follow(Long followUserId, Boolean isFollow) {
// 判断是关注还是取关
Long userId = UserHolder.getUser().getId();
// 关注,新增
String key = RedisConstants.FOLLOW_KEY + userId;
if (isFollow) {
Follow follow = new Follow();
follow.setUserId(userId);
follow.setFollowUserId(followUserId);
boolean isSuccess = save(follow);
if (isSuccess) {
stringRedistemplate.opsForSet().add(key, followUserId.toString());
}
} else {
remove(new QueryWrapper<Follow>()
.eq("user_id", userId).eq("follow_user_id", followUserId));
stringRedistemplate.opsForSet().remove(key, followUserId.toString());
}
// 取关,删除
return Result.ok();
}
判断的逻辑
:
@Override
public Result followCommons(Long id) {
Long userId = UserHolder.getUser().getId();
String key = RedisConstants.FOLLOW_KEY + userId;
String key2 = RedisConstants.FOLLOW_KEY + id;
Set<String> intersect = stringRedistemplate.opsForSet().intersect(key, key2);
if (intersect == null || intersect.isEmpty()) {
System.out.println("111");
return Result.ok(Collections.emptyList());
}
List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
Stream<UserDTO> users = userService.listByIds(ids)
.stream()
.map(user -> BeanUtil.copyProperties(user, UserDTO.class));
return Result.ok(users);
}
关注推送
需求
分析
用什么结构?
用 SortedSet
实现滚动分页
如果没有新数据的插入,那么正常的分页查询即可以,即利用 List 也可以完成这个工作
但是由于可能会有新数据的插入,数据角标也在不断变化中,所以需要使用滚动分页
MAX
:如果是第一次查询,则返回当前时间戳,否则应为上一次查询的最小值MIN
:可以设为 0,无需变动OFFSET
:相对于 MAX 的偏移量,第一次查询时设为 0 即可,之后需要设为上一次查询的最小值的数量SIZE
:大小,一般是固定的,比如 10 条数据/页
实现
@Override
public Result saveBlog(Blog blog) {
// 获取登录用户
UserDTO user = UserHolder.getUser();
blog.setUserId(user.getId());
// 保存探店博文
save(blog);
// 查询笔记作者的所有粉丝
List<Follow> follows = followService.query().eq("follow_user_id", user.getId()).list();
for (Follow f : follows) {
Long userId = f.getUserId();
// 推送
String key = RedisConstants.Feed_KEY + userId;
stringRedistemplate.opsForZSet().add(key, blog.getId().toString(), System.currentTimeMillis());
}
return Result.ok(blog.getId());
}
@Override
public Result queryBlogofFollow(Long max, Integer offset) {
// 获取当前用户
Long userId = UserHolder.getUser().getId();
String key = RedisConstants.Feed_KEY + userId;
// 找到收件箱
Set<ZSetoperations.TypedTuple<String>> typedTuples = stringRedistemplate.opsForZSet()
.reverseRangeByscoreWithscores(key, 0, max, offset, 2);
// 判断
if (typedTuples == null || typedTuples.isEmpty()) {
return Result.ok();
}
// 解析数据
ArrayList<Long> ids = new ArrayList<>();
long minTime = 0;
int os = 1;
for (ZSetoperations.TypedTuple<String> tuple : typedTuples) {
ids.add(Long.valueOf(tuple.getValue()));
if (tuple.getscore().longValue() == minTime) {
os++;
} else {
minTime = tuple.getscore().longValue();
}
}
// 根据 id 查询 blog
String idStr = StrUtil.join(",", ids);
List<Blog> blogs = query().in("id", ids).last("ORDER BY FIELD(id," + idStr + ")").list();
for (Blog blog : blogs) {
queryBlogUser(blog);
isBlogLiked(blog);
}
ScrollResult scrollResult = new ScrollResult();
scrollResult.setList(blogs);
scrollResult.setoffset(os);
scrollResult.setMinTime(minTime);
return Result.ok(scrollResult);
}
附近用户:GEO
基本使用
预先准备
@Test
public void loadShopData() {
List<Shop> list = shopService.list();
Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
for (Map.Entry<Long, List<Shop>> longListEntry : map.entrySet()) {
Long typeId = longListEntry.getKey();
String key = RedisConstants.SHOP_GEO_KEY + typeId;
List<Shop> shops = longListEntry.getValue();
List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(shops.size());
for (Shop shop : shops) {
locations.add(new RedisGeoCommands.GeoLocation<>(
shop.getId().toString(),
new Point(shop.getX(), shop.getY())
));
}
stringRedistemplate.opsForGeo().add(key, locations);
}
}
依赖问题
SpringDataRedis2.3.9 版本并不支持 Redis 6.2 提供的 GEOSEARCH
命令,我们要修改其版本
下载 IDEA 的插件 Maven Helper
移除默认的版本的包:
再手动添加新版本:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.1.6.RELEASE</version>
</dependency>
代码实现
思路:
@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
// 1. 判断是否需要根据坐标查询
if (x == null || y == null) {
// 根据类型分页查询
Page<Shop> page = query()
.eq("type_id", typeId)
.page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));
// 返回数据
return Result.ok(page.getRecords());
}
// 2. 计算分页参数
int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;
int end = (current) * SystemConstants.DEFAULT_PAGE_SIZE;
// 3. 查询 Redis、按照距离排序、分页
String key = RedisConstants.SHOP_GEO_KEY + typeId;
GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedistemplate.opsForGeo().search(key, GeoReference.fromCoordinate(x, y),
new distance(5000),
RedisGeoCommands.GeoSearchCommandArgs
.newGeoSearchArgs().includedistance().limit(end));
if (results == null) {
return Result.ok(Collections.emptyList());
}
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();
if (list.size() <= from) {
return Result.ok(Collections.emptyList());
}
// 截取
List<Long> ids = new ArrayList<>(list.size());
HashMap<String, distance> distanceMap = new HashMap<>(list.size());
list.stream().skip(from).forEach(result -> {
String shopIdStr = result.getContent().getName();
ids.add(Long.valueOf(shopIdStr));
distance distance = result.getdistance();
distanceMap.put(shopIdStr, distance);
});
String idStr = StrUtil.join(",", ids);
List<Shop> shops = query().in("id", ids).last("ORDER BY FIELD (id, " + idStr + ")").list();
for (Shop shop : shops) {
shop.setdistance(distanceMap.get(shop.getId().toString()).getValue());
}
return Result.ok(shops);
}
用户签到:BitMap
基本使用
签到功能
直接上服务层代码:
@Override
public Result sign() {
Long userId = UserHolder.getUser().getId();
LocalDateTime Now = LocalDateTime.Now();
String keySuffix = Now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
String key = USER_SIGN_KEY + userId + keySuffix;
int dayOfMonth = Now.getDayOfMonth();
stringRedistemplate.opsForValue().setBit(key, dayOfMonth - 1, true);
return Result.ok();
}
签到统计
@Override
public Result signCount() {
// 获取记录
Long userId = UserHolder.getUser().getId();
LocalDateTime Now = LocalDateTime.Now();
String keySuffix = Now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
String key = USER_SIGN_KEY + userId + keySuffix;
int dayOfMonth = Now.getDayOfMonth();
List<Long> result = stringRedistemplate.opsForValue().bitField(
key,
BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth))
.valueAt(0)
);
if (result == null || result.isEmpty()) {
return Result.ok(0);
}
Long num = result.get(0);
if (num == null || num == 0) {
return Result.ok(0);
}
int count = 0;
while (true) {
if ((num & 1) == 0) {
break;
} else {
count++;
}
num >>>= 1;
}
return Result.ok(count);
}
UV统计:HLL
一个测试:
插入 100 万条数据,模仿 1000 人访问,看最后结果如何
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。