Redis实现社交粉丝功能

LIKE.TG | 发现全球营销软件&服务汇聚顶尖互联网营销和AI营销产品,提供一站式出海营销解决方案。唯一官网:www.like.tg
好友相关的功能至少包含
- 关注 / 取关
- 我(他)的关注
- 我(他)的粉丝
- 共同关注
- 我关注的人也关注他
这样的功能如果采用数据库,只是单纯得到用户的一些粉丝或者关注列表,也很简单、易实现,但若我想查出两个甚至多个用户共同关注人或想查询两个或者多个用户的共同粉丝,就会很麻烦,效率也不会很高。
但如果用 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>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.battcngroupId>
<artifactId>swagger-spring-boot-starterartifactId>
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;
}
业务流程
共同关注
Sets 拥有去重 (我们不能多次关注同一用户) 功能 。一个用户我们存贮两个集合:一个是保存用户关注的人 另一个是保存关注用户的人。
RedisKeyConstant
following(“following:”, “关注集合Key”), followers(“followers:”, “粉丝集合Key”),

LIKE.TG:汇集全球营销软件&服务,助力出海企业营销增长。提供最新的“私域营销获客”“跨境电商”“全球客服”“金融支持”“web3”等一手资讯新闻。
点击【联系客服】 🎁 免费领 1G 住宅代理IP/proxy, 即刻体验 WhatsApp、LINE、Telegram、Twitter、ZALO、Instagram、signal等获客系统,社媒账号购买 & 粉丝引流自助服务或关注【LIKE.TG出海指南频道】、【LIKE.TG生态链-全球资源互联社区】连接全球出海营销资源。


























