Skip to content

Commit ad94d23

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 7c1b7b9 + 0250ce8 commit ad94d23

File tree

15 files changed

+122
-68
lines changed

15 files changed

+122
-68
lines changed

server/chat-center/conf/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ elasticsearch:
22
address: DouyinDBA.raqtpie.xyz
33
port: 9200
44
user: elastic
5-
password:
5+
password: elasticnode1
66

77
gRPC:
88
address: 127.0.0.1

server/gateway-center/cache/init.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func Init() {
2626
panic("redis ping error")
2727
}
2828
}
29+
2930
func RedisSetKey(ctx context.Context, key string, value interface{}) error {
3031
if _, err := RedisClient.Get(ctx, key).Result(); err != redis.Nil {
3132
fmt.Printf("Key is existed %s\n", key)
@@ -65,6 +66,7 @@ func RedisSetPublishListVideoList(ctx context.Context, key string, videoList res
6566
fmt.Printf("Redis client set successfully...%s\n", key)
6667
return nil
6768
}
69+
6870
func RedisGetPublishListVideoList(ctx context.Context, key string) (response.VideoArray, error) {
6971
videos := response.VideoArray{}
7072
err := RedisClient.Get(ctx, key).Scan(&videos)
@@ -77,6 +79,7 @@ func RedisGetPublishListVideoList(ctx context.Context, key string) (response.Vid
7779
fmt.Printf("Redis client get successfully... %s\n", key)
7880
return videos, nil
7981
}
82+
8083
func RedisDeleteKey(ctx context.Context, key string) {
8184
RedisClient.Del(ctx, key)
8285
}

server/gateway-center/conf/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ nacos:
22
Ip: 127.0.0.1
33
Port: 8848
44
redis:
5-
passwd:
5+
passwd: Rh2004==
66
host: r-bp12xmzrbjr36iq7lepd.redis.rds.aliyuncs.com:6379
77
db: 0
88
application:

server/gateway-center/controller/relationController.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func GetFollowList(ctx *gin.Context) {
3636
userId := util.StringToInt64(ctx.Query("user_id"))
3737
token := ctx.Query("token")
3838

39+
println(userId)
3940
resp, err := grpcClient.GetFollowList(ctx, userId, token)
4041
if err != nil || resp.StatusCode != e.Success {
4142
ctx.JSON(http.StatusInternalServerError, response.GetFollowListResponse{

server/gateway-center/grpcClient/Init.go

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func Init() {
7575
log.Fatalf("connect failed: %v", err)
7676
}
7777
UserClient = pb.NewUserServiceClient(UserConn)
78+
RelationClient = pb.NewRelationServiceClient(UserConn)
7879

7980
instances, err = nacosClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
8081
ServiceName: conf.VideoCenterServiceName,
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[error]: 2023/08/30 12:59:26 [<Grpc> rpc error: code = InvalidArgument desc = 参数错误]
2+
[error]: 2023/08/30 13:00:06 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:4199->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]
3+
[error]: 2023/08/30 13:00:41 [<Grpc> rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:8811: connectex: No connection could be made because the target machine actively refused it."]
4+
[error]: 2023/08/30 13:01:29 [<Grpc> rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:8811: connectex: No connection could be made because the target machine actively refused it."]
5+
[error]: 2023/08/30 13:02:00 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:10165->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]
6+
[error]: 2023/08/30 13:06:14 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:10404->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]
7+
[error]: 2023/08/30 13:06:55 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:9096->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]
8+
[error]: 2023/08/30 13:09:40 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:1457->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]
9+
[error]: 2023/08/30 13:19:28 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:8529->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]
10+
[error]: 2023/08/30 13:23:34 [<Grpc> rpc error: code = Unavailable desc = error reading from server: read tcp 127.0.0.1:12624->127.0.0.1:8811: wsarecv: An existing connection was forcibly closed by the remote host.]

server/user-center/cache/key.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cache
22

33
import (
4-
"fmt"
54
"user-center/pkg/util"
65
)
76

@@ -12,10 +11,15 @@ import (
1211
func GenFollowUserCacheKey(userId, followUserId uint) string {
1312
return "follow_user_" + util.UintToStr(userId) + "_" + util.UintToStr(followUserId)
1413
}
14+
1515
func GenUserInfoCacheKey(userId uint) string {
1616
return "user_info_" + util.UintToStr(userId)
1717
}
1818

19-
func CacheChangeUserCountKey(userID int64, category string) string {
20-
return fmt.Sprintf("user:%d:%s", userID, category)
19+
func GenFollowCountCacheKey(userId uint) string {
20+
return "count:follow:" + util.UintToStr(userId)
21+
}
22+
23+
func GenFollowerCountCacheKey(userId uint) string {
24+
return "count:follower:" + util.UintToStr(userId)
2125
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cache
2+
3+
import "context"
4+
5+
func NewRelationCache(ctx context.Context) *RedisCache {
6+
return &RedisCache{NewRedisClient(ctx)}
7+
}
8+
9+
// IncrFollowCountCache 更新关注数缓存
10+
func (c *RedisCache) IncrFollowCountCache(userId, incr int64) error {
11+
return c.IncrBy(context.Background(), GenFollowCountCacheKey(uint(userId)), incr).Err()
12+
}
13+
14+
// IncrFollowerCountCache 更新粉丝数缓存
15+
func (c *RedisCache) IncrFollowerCountCache(userId, incr int64) error {
16+
return c.IncrBy(context.Background(), GenFollowerCountCacheKey(uint(userId)), incr).Err()
17+
}
18+
19+
// GetFollowCountCache 获取关注数缓存
20+
func (c *RedisCache) GetFollowCountCache(userId int64) (int64, error) {
21+
return c.Get(context.Background(), GenFollowCountCacheKey(uint(userId))).Int64()
22+
}
23+
24+
// GetFollowerCountCache 获取粉丝数缓存
25+
func (c *RedisCache) GetFollowerCountCache(userId int64) (int64, error) {
26+
return c.Get(context.Background(), GenFollowerCountCacheKey(uint(userId))).Int64()
27+
}

server/user-center/cache/userCache.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@ import (
88
func NewUserCache(ctx context.Context) *RedisCache {
99
return &RedisCache{NewRedisClient(ctx)}
1010
}
11-
func (userCache *RedisCache) IsFollow(ctx context.Context, uId, followId uint) bool {
1211

13-
return userCache.
12+
// IsFollow 是否关注
13+
func (c *RedisCache) IsFollow(ctx context.Context, uId, followId uint) bool {
14+
return c.
1415
Exists(ctx, GenFollowUserCacheKey(uId, followId)).
1516
Val() == 1
1617
}
1718

1819
// AddFollow 关注关系缓存
19-
func (userCache *RedisCache) AddFollow(ctx context.Context, uId, followId uint) error {
20-
21-
return userCache.
20+
func (c *RedisCache) AddFollow(ctx context.Context, uId, followId uint) error {
21+
return c.
2222
Set(ctx, GenFollowUserCacheKey(uId, followId), 1, time.Hour).
2323
Err()
2424
}
2525

2626
// AddUser 用户信息缓存
27-
func (userCache *RedisCache) AddUser(ctx context.Context, uId uint, m map[string]interface{}) error {
28-
err := userCache.HSet(ctx, GenUserInfoCacheKey(uId), m).Err()
27+
func (c *RedisCache) AddUser(ctx context.Context, uId uint, m map[string]interface{}) error {
28+
err := c.HSet(ctx, GenUserInfoCacheKey(uId), m).Err()
2929
if err != nil {
3030
return err
3131
}
3232
// 设置键的过期时间为10天
33-
return userCache.Expire(ctx, GenUserInfoCacheKey(uId), 10*24*3600*time.Second).Err()
33+
return c.Expire(ctx, GenUserInfoCacheKey(uId), 10*24*3600*time.Second).Err()
3434
}
3535

3636
// HasUser 判断Redis中是否存在某个用户信息缓存
37-
func (userCache *RedisCache) HasUser(ctx context.Context, uId uint) (cacheData map[string]string, err error) {
37+
func (c *RedisCache) HasUser(ctx context.Context, uId uint) (cacheData map[string]string, err error) {
3838
// 获取一个哈希表中的所有字段和值
39-
cacheData, err = userCache.HGetAll(ctx, GenUserInfoCacheKey(uId)).Result()
39+
cacheData, err = c.HGetAll(ctx, GenUserInfoCacheKey(uId)).Result()
4040
if err != nil {
4141
return nil, err
4242
}

server/user-center/conf/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ mysql:
33
DbHost: DouyinDBA.raqtpie.xyz
44
DbPort: 3306
55
DbUser: DouyinDBA
6-
DbPassword:
6+
DbPassword: nibainimane
77
DbName: DouyinDB
88

99
redis:
1010
RedisDb: redis
1111
RedisAddr: r-bp12xmzrbjr36iq7lepd.redis.rds.aliyuncs.com:6379
12-
RedisPw:
12+
RedisPw: Rh2004==
1313
RedisDbName: 2
1414

1515
nacos:

server/user-center/dao/relation.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,40 @@ package dao
22

33
import (
44
"context"
5-
"user-center/cache"
6-
7-
"github.com/go-redis/redis/v8"
5+
"fmt"
86
"gorm.io/gorm"
97

108
"user-center/model"
119
)
1210

1311
// RelationDao TODO:去除无用ctx
1412
type RelationDao struct {
15-
db *gorm.DB
16-
rdb *redis.Client
13+
*gorm.DB
1714
}
1815

19-
func NewRelationDao(db *gorm.DB, rdb *redis.Client) *RelationDao {
20-
return &RelationDao{db: db, rdb: rdb}
16+
func NewRelationDao(ctx context.Context) *RelationDao {
17+
return &RelationDao{NewDBClient(ctx)}
2118
}
2219

2320
// Follow 关注
2421
func (d *RelationDao) Follow(ctx context.Context, userId, toUserId int64) error {
25-
return d.db.Model(&model.User{}).Where("id = ?", userId).Association("Follows").Append(&model.User{
22+
fmt.Printf("userid:%v,toUserId:%v", userId, toUserId)
23+
return d.DB.Model(&model.User{}).Where("id = ?", userId).Association("Follows").Append(&model.User{
2624
Model: gorm.Model{ID: uint(toUserId)},
2725
})
2826
}
2927

3028
// Unfollow 取消关注
3129
func (d *RelationDao) Unfollow(userId, toUserId int64) error {
32-
return d.db.Model(&model.User{}).Where("id = ?", userId).Association("Follows").Delete(&model.User{
30+
return d.DB.Model(&model.User{}).Where("id = ?", userId).Association("Follows").Delete(&model.User{
3331
Model: gorm.Model{ID: uint(toUserId)},
3432
})
3533
}
3634

3735
// GetFollowList 获取关注列表
3836
func (d *RelationDao) GetFollowList(ctx context.Context, userId int64) ([]*model.User, error) {
3937
var users []*model.User
40-
if err := d.db.Model(&model.User{}).Where("id = ?", userId).Association("Follows").Find(&users); err != nil {
38+
if err := d.DB.Model(&model.User{}).Where("id = ?", userId).Association("Follows").Find(&users); err != nil {
4139
return nil, err
4240
}
4341
return users, nil
@@ -46,7 +44,7 @@ func (d *RelationDao) GetFollowList(ctx context.Context, userId int64) ([]*model
4644
// GetFollowerList 获取粉丝列表
4745
func (d *RelationDao) GetFollowerList(ctx context.Context, userId int64) ([]*model.User, error) {
4846
var users []*model.User
49-
if err := d.db.Model(&model.User{}).Where("id = ?", userId).Association("Fans").Find(&users); err != nil {
47+
if err := d.DB.Model(&model.User{}).Where("id = ?", userId).Association("Fans").Find(&users); err != nil {
5048
return nil, err
5149
}
5250
return users, nil
@@ -56,31 +54,31 @@ func (d *RelationDao) GetFollowerList(ctx context.Context, userId int64) ([]*mod
5654
func (d *RelationDao) GetFriendList(userId int64) ([]*model.User, error) {
5755
// 通过双向关联查询获取互相关注好友
5856
var friends []*model.User
59-
if err := d.db.Model(&model.User{}).
60-
Where("id IN (?)", d.db.Model(&model.User{}).Where("id = ?", userId).Select("follow_id")).
61-
Where("follow_id IN (?)", d.db.Model(&model.User{}).Where("id = ?", userId).Select("id")).
57+
if err := d.DB.Model(&model.User{}).
58+
Where("id IN (?)", d.DB.Model(&model.User{}).Where("id = ?", userId).Select("follow_id")).
59+
Where("follow_id IN (?)", d.DB.Model(&model.User{}).Where("id = ?", userId).Select("id")).
6260
Find(&friends).Error; err != nil {
6361
return nil, err
6462
}
6563
return friends, nil
6664
}
6765

68-
// IncrFollowCountCache 更新关注数缓存
69-
func (d *RelationDao) IncrFollowCountCache(userId, incr int64) error {
70-
return d.rdb.IncrBy(context.Background(), cache.GenUserInfoCacheKey(uint(userId)), incr).Err()
71-
}
72-
73-
// IncrFollowerCountCache 更新粉丝数缓存
74-
func (d *RelationDao) IncrFollowerCountCache(userId, incr int64) error {
75-
return d.rdb.IncrBy(context.Background(), cache.GenUserInfoCacheKey(uint(userId)), incr).Err()
76-
}
77-
78-
// GetFollowCountCache 获取关注数缓存
79-
func (d *RelationDao) GetFollowCountCache(userId int64) (int64, error) {
80-
return d.rdb.Get(context.Background(), cache.GenUserInfoCacheKey(uint(userId))).Int64()
81-
}
82-
83-
// GetFollowerCountCache 获取粉丝数缓存
84-
func (d *RelationDao) GetFollowerCountCache(userId int64) (int64, error) {
85-
return d.rdb.Get(context.Background(), cache.GenUserInfoCacheKey(uint(userId))).Int64()
86-
}
66+
//// IncrFollowCountCache 更新关注数缓存
67+
//func (d *RelationDao) IncrFollowCountCache(userId, incr int64) error {
68+
// return d.rdb.IncrBy(context.Background(), cache.GenUserInfoCacheKey(uint(userId)), incr).Err()
69+
//}
70+
//
71+
//// IncrFollowerCountCache 更新粉丝数缓存
72+
//func (d *RelationDao) IncrFollowerCountCache(userId, incr int64) error {
73+
// return d.rdb.IncrBy(context.Background(), cache.GenUserInfoCacheKey(uint(userId)), incr).Err()
74+
//}
75+
//
76+
//// GetFollowCountCache 获取关注数缓存
77+
//func (d *RelationDao) GetFollowCountCache(userId int64) (int64, error) {
78+
// return d.rdb.Get(context.Background(), cache.GenUserInfoCacheKey(uint(userId))).Int64()
79+
//}
80+
//
81+
//// GetFollowerCountCache 获取粉丝数缓存
82+
//func (d *RelationDao) GetFollowerCountCache(userId int64) (int64, error) {
83+
// return d.rdb.Get(context.Background(), cache.GenUserInfoCacheKey(uint(userId))).Int64()
84+
//}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[error]: 2023/08/30 12:59:26 [<Register> 用户名格式出错 [be from req]:&{ }]

server/user-center/server/relationServer.go

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func (s *RelationServer) RelationAction(ctx context.Context, req *pb.RelationAct
2323
if err1 != nil {
2424
return nil, err1
2525
}
26-
2726
var err error
2827
if req.ActionType == 1 {
2928
err = s.relationService.Follow(ctx, int64(parseToken.ID), req.ToUserId)

0 commit comments

Comments
 (0)