Skip to content

Commit d677044

Browse files
committed
添加cache
1 parent d8d9c89 commit d677044

File tree

7 files changed

+98
-11
lines changed

7 files changed

+98
-11
lines changed

server/gateway-center/cache/init.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"gateway-center/conf"
7+
"github.com/go-redis/redis/v8"
8+
"time"
9+
)
10+
11+
// todo 后续可加入Feed流缓存、PublishList缓存等等
12+
13+
var RedisClient *redis.Client
14+
15+
func Init() {
16+
RedisClient = redis.NewClient(&redis.Options{
17+
Password: conf.RedisPasswd,
18+
Addr: conf.RedisHost,
19+
DB: conf.RedisDB,
20+
})
21+
22+
_, err := RedisClient.Ping(context.Background()).Result()
23+
if err != nil {
24+
fmt.Println(err)
25+
panic("redis ping error")
26+
}
27+
}
28+
func RedisSetKey(ctx context.Context, key string, value interface{}) error {
29+
if _, err := RedisClient.Get(ctx, key).Result(); err != redis.Nil {
30+
fmt.Printf("Key is existed %s\n", key)
31+
}
32+
err := RedisClient.Set(ctx, key, value, 3*time.Minute).Err()
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
fmt.Printf("Redis client set successfully...%s\n", key)
38+
return nil
39+
}
40+
41+
func RedisGetKey(ctx context.Context, key string) (string, error) {
42+
value, err := RedisClient.Get(ctx, key).Result()
43+
44+
if err == redis.Nil {
45+
return value, err
46+
}
47+
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
fmt.Printf("Redis client get successfully... %s\n", key)
53+
return value, nil
54+
}

server/gateway-center/conf/config.go

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ var (
1717
VideoCenterServiceName string
1818
)
1919

20+
// cache
21+
var (
22+
RedisPasswd string
23+
RedisHost string
24+
RedisDB int
25+
)
26+
2027
// Init 初始化配置文件与引擎
2128
func Init() error {
2229

@@ -41,5 +48,9 @@ func Init() error {
4148
UserCenterServiceName = viper.GetString("application.user-center.ServiceName")
4249
VideoCenterServiceName = viper.GetString("application.video-center.ServiceName")
4350

51+
RedisPasswd = viper.GetString("redis.passwd")
52+
RedisHost = viper.GetString("redis.host")
53+
RedisDB = viper.GetInt("redis.db")
54+
4455
return nil
4556
}

server/gateway-center/conf/config.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
nacos:
22
Ip: 127.0.0.1
33
Port: 8848
4-
4+
redis:
5+
passwd:
6+
host: r-bp12xmzrbjr36iq7lepd.redis.rds.aliyuncs.com:6379
7+
db: 0
58
application:
69
chat-center:
710
ServiceName: chat-center

server/gateway-center/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ require (
1919
github.com/bytedance/sonic v1.9.1 // indirect
2020
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2121
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
22+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2223
github.com/fsnotify/fsnotify v1.6.0 // indirect
2324
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2425
github.com/gin-contrib/sse v0.1.0 // indirect
2526
github.com/go-playground/locales v0.14.1 // indirect
2627
github.com/go-playground/universal-translator v0.18.1 // indirect
2728
github.com/go-playground/validator/v10 v10.14.0 // indirect
29+
github.com/go-redis/redis/v8 v8.11.5 // indirect
2830
github.com/goccy/go-json v0.10.2 // indirect
2931
github.com/golang/mock v1.6.0 // indirect
3032
github.com/golang/protobuf v1.5.3 // indirect

server/gateway-center/go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
7979
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8080
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
8181
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
82+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
83+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
8284
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
8385
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
8486
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -110,6 +112,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
110112
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
111113
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
112114
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
115+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
116+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
113117
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
114118
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
115119
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=

server/gateway-center/grpcClient/VideoClient.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package grpcClient
22

33
import (
44
"context"
5+
"fmt"
6+
"gateway-center/cache"
57
"gateway-center/pkg/e"
68
"gateway-center/util"
79
"github.com/OrionLi/douyin-backend/pkg/pb"
@@ -115,15 +117,25 @@ func Feed(ctx context.Context, req *pb.DouyinFeedRequest) ([]*pb.Video, int64, e
115117
// GetPublishListCount 通过Token获取userId对应的VideoCount
116118
func GetPublishListCount(ctx context.Context, token string) (count int64, err error) {
117119
userId := ValidateToken(token)
118-
req := &pb.DouyinPublishListRequest{UserId: userId, Token: token}
119-
r, err := VideoClient.PublishList(ctx, req)
120-
if err != nil {
121-
return 0, err
122-
}
123-
if r.StatusCode != 0 {
124-
return 0, e.NewCustomError(int64(r.StatusCode), *r.StatusMsg)
125-
}
126-
return int64(len(r.VideoList)), nil
120+
PACountKey := fmt.Sprintf("publishlist:%d", userId)
121+
vCount, err := cache.RedisGetKey(ctx, PACountKey)
122+
if err != nil { //出错,没找到,查询数据库,并写入cache
123+
util.LogrusObj.Errorf("Cache get PublishListCount Error ErrorMSG:%s", err.Error())
124+
req := &pb.DouyinPublishListRequest{UserId: userId, Token: token}
125+
r, err := VideoClient.PublishList(ctx, req)
126+
if err != nil {
127+
return 0, err
128+
}
129+
if r.StatusCode != 0 {
130+
return 0, e.NewCustomError(int64(r.StatusCode), *r.StatusMsg)
131+
}
132+
err = cache.RedisSetKey(ctx, PACountKey, int64(len(r.VideoList)))
133+
if err != nil {
134+
util.LogrusObj.Errorf("Cache set PublishListCount Error ErrorMSG:%s", err.Error())
135+
}
136+
return int64(len(r.VideoList)), nil
137+
} //找到返回结果
138+
return util.StringToInt64(vCount), nil
127139
}
128140

129141
// PublishAction 投稿

server/gateway-center/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package main
22

33
import (
4+
"gateway-center/cache"
45
"gateway-center/conf"
56
"gateway-center/grpcClient"
67
)
78

89
func main() {
910
conf.Init()
1011
grpcClient.Init()
12+
cache.Init()
1113
NewRouter().Run(":3010")
12-
1314
}

0 commit comments

Comments
 (0)