亞寵展、全球寵物產業風向標——亞洲寵物展覽會深度解析
875
2022-05-30
好友相關的功能至少包含
關注 / 取關
我(他)的關注
我(他)的粉絲
共同關注
我關注的人也關注他
這樣的功能如果采用數據庫,只是單純得到用戶的一些粉絲或者關注列表,也很簡單、易實現,但若我想查出兩個甚至多個用戶共同關注人或想查詢兩個或者多個用戶的共同粉絲,就會很麻煩,效率也不會很高。
但如果用 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;
1
2
3
4
5
6
7
8
9
10
11
12
13
創建代碼模塊 ms-follow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
配置文件
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'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
實體類
@ApiModel(description = "食客關注實體類") @Getter @Setter public class Follow extends BaseModel { @ApiModelProperty("用戶ID") private int dinerId; @ApiModelProperty("關注用戶ID") private Integer followDinerId; }
1
2
3
4
5
6
7
8
9
10
11
業務流程
共同關注
Sets 擁有去重 (我們不能多次關注同一用戶) 功能 。一個用戶我們存貯兩個集合:一個是保存用戶關注的人 另一個是保存關注用戶的人。
RedisKeyConstant
following(“following:”, “關注集合Key”),
followers(“followers:”, “粉絲集合Key”),
Redis
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。