Redis实现社交粉丝功能

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 好友相关的功能至少包含关注 / 取关我(他)的关注我(他)的粉丝共同关注我关注的人也关注他

好友相关的功能至少包含

  • 关注 / 取关
  • 我(他)的关注
  • 我(他)的粉丝
  • 共同关注
  • 我关注的人也关注他

这样的功能如果采用数据库,只是单纯得到用户的一些粉丝或者关注列表,也很简单、易实现,但若我想查出两个甚至多个用户共同关注人或想查询两个或者多个用户的共同粉丝,就会很麻烦,效率也不会很高。


但如果用 redis 去做的话就会相当的简单且高效。因为 redis 自己本身带有专门针对于这种集合的交集、并集、差集的一些操作。

总体思路我们采用 MySQL + Redis 的方式结合完成。

  • MySQL 保存落地数据
  • Redis 的 Sets 进行集合操作

数据表设计

CREATE TABLE `t_follow` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`diner_id`  int(11) NULL DEFAULT NULL COMMENT '用户外键' ,
`follow_diner_id`  int(11) NULL DEFAULT NULL COMMENT '用户食客外键' ,
`is_valid`  tinyint(1) NULL DEFAULT NULL ,
`create_date`  datetime NULL DEFAULT NULL ,
`update_date`  datetime NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=6
ROW_FORMAT=COMPACT;

创建代码模块 ms-follow

<dependencies>
    <!-- eureka client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- spring web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- spring data redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <!-- swagger -->
    <dependency>
        <groupId>com.battcn</groupId>
        <artifactId>swagger-spring-boot-starter</artifactId>
    </dependency>
</dependencies>

配置文件

server:
  port: 8084 # 端口
spring:
  application:
    name: ms-follow # 应用名
  # 数据库
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/db_redis?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false
  # Redis
  redis:
    port: 6379
    host: 192.168.10.101
    timeout: 3000
    password: 123456
    database: 2
  # Swagger
  swagger:
    base-package: com.javaedge.follow
    title: 慕课美食社交食客API接口文档
# 配置 Eureka Server 注册中心
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
service:
  name:
    ms-oauth-server: http://ms-oauth2-server/
    ms-diners-server: http://ms-diners/
mybatis:
  configuration:
    map-underscore-to-camel-case: true # 开启驼峰映射
logging:
  pattern:
    console: '%d{HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n'

实体类

@ApiModel(description = "食客关注实体类")
@Getter
@Setter
public class Follow extends BaseModel {
    @ApiModelProperty("用户ID")
    private int dinerId;
    @ApiModelProperty("关注用户ID")
    private Integer followDinerId;
}

业务流程

6.png

共同关注

Sets 拥有去重 (我们不能多次关注同一用户) 功能 。一个用户我们存贮两个集合:一个是保存用户关注的人 另一个是保存关注用户的人。

RedisKeyConstant

following(“following:”, “关注集合Key”),

followers(“followers:”, “粉丝集合Key”),

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
1月前
|
存储 缓存 NoSQL
深入了解Redis键管理:探索Redis键命令及其功能与应用场景
深入了解Redis键管理:探索Redis键命令及其功能与应用场景
|
1月前
|
消息中间件 存储 缓存
探索Redis CLI:功能强大的Redis命令行工具及其应用场景
探索Redis CLI:功能强大的Redis命令行工具及其应用场景
|
12天前
|
缓存 NoSQL Java
【亮剑】分布式锁是保证多服务实例同步的关键机制,常用于互斥访问共享资源、控制访问顺序和系统保护,如何使用注解来实现 Redis 分布式锁的功能?
【4月更文挑战第30天】分布式锁是保证多服务实例同步的关键机制,常用于互斥访问共享资源、控制访问顺序和系统保护。基于 Redis 的分布式锁利用 SETNX 或 SET 命令实现,并考虑自动过期、可重入及原子性以确保可靠性。在 Java Spring Boot 中,可通过 `@EnableCaching`、`@Cacheable` 和 `@CacheEvict` 注解轻松实现 Redis 分布式锁功能。
|
17天前
|
存储 监控 NoSQL
|
17天前
|
存储 NoSQL API
Redis入门到通关之GEO实现附近的人功能
Redis入门到通关之GEO实现附近的人功能
11 0
|
17天前
|
存储 NoSQL Redis
Redis入门到通关之Set实现点赞功能
Redis入门到通关之Set实现点赞功能
15 0
|
1月前
|
存储 NoSQL 数据处理
Redis Lua脚本:赋予Redis更强大的逻辑与功能
Redis Lua脚本:赋予Redis更强大的逻辑与功能
|
2月前
|
缓存 NoSQL Shell
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(持久化功能分析)
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(持久化功能分析)
166 0
|
2月前
|
NoSQL Shell 网络安全
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)(二)
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)
23 0
|
2月前
|
存储 缓存 NoSQL
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)(一)
【Redis深度专题】「核心技术提升」探究Redis服务启动的过程机制的技术原理和流程分析的指南(集群功能分析)
394 0