Skip to content

Commit

Permalink
update feed middleware (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiifong authored Aug 31, 2023
1 parent 44c8734 commit 9f373ca
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 25 deletions.
2 changes: 1 addition & 1 deletion conf/tiktok.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ redis:
host: 127.0.0.1
port: 6379
password:
poolSize: 10
poolSize: 1000

oss:
type: minio # minio,cos 等等
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7
github.com/redis/go-redis/v9 v9.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
Expand Down
4 changes: 1 addition & 3 deletions module/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ type Client struct {

func NewRedisClients(clients map[RDB]*Client) {
poolSize, _ := strconv.ParseInt(config.GetString("redis.poolSize"), 10, 64)
n := 0
for k := range clients {
r := redis.NewClient(&redis.Options{ //nolint:typecheck
Addr: fmt.Sprintf("%v:%v", config.Get("redis.host"), config.Get("redis.port")),
Password: fmt.Sprintf("%v", config.Get("redis.password")),
DB: n + 1,
DB: int(k) + 1,
PoolSize: int(poolSize),
})
n++
ctx := context.Background()
clients[k] = &Client{C: r, Ctx: ctx}
}
Expand Down
60 changes: 60 additions & 0 deletions module/middleware/cache/feed.go
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
package cache

import (
"github.com/Biu-X/TikTok/module/cache"
"github.com/Biu-X/TikTok/module/log"
"github.com/Biu-X/TikTok/module/response"
"github.com/gin-gonic/gin"
"github.com/pquerna/ffjson/ffjson"
"time"
)

// FeedMiddleware feed 缓存装饰器
func FeedMiddleware(rc *cache.Client, service gin.HandlerFunc, empty interface{}) gin.HandlerFunc {
return func(c *gin.Context) {
// 从连接池获取一个连接
conn := rc.C.Conn()
defer conn.Close()

// latest time 作为 key
latestTime := c.Query("latest_time")
if latestTime == "" {
latestTime = "0"
}
log.Logger.Infof("----------------> feed latest time: %v", latestTime)
result, err := conn.Get(rc.Ctx, latestTime).Result()
if err != nil {
// 缓存没有存储该key
log.Logger.Infof("get feed data from MySQL database, due to %v", err)
service(c)
ret, exists := c.Get("feed")
log.Logger.Debugf("ret: %v", ret)
if !exists {
return
}
retData, _ := ffjson.Marshal(ret)
conn.SetNX(rc.Ctx, latestTime, retData, 1*time.Hour)
nt, exists := c.Get("nextTime")
log.Logger.Debugf("next_time: %v", nt)
if exists {
conn.SetNX(rc.Ctx, latestTime+"-0", nt, 1*time.Hour)
}
return
}
log.Logger.Info("get feed data from redis database")
err = ffjson.Unmarshal([]byte(result), &empty)
if err != nil {
log.Logger.Errorf("unmarshal result error: %v", err)
response.ErrRespWithMsg(c, "unmarshal result error")
return
}

nextTime, err := conn.Get(rc.Ctx, latestTime+"-0").Result()
if err != nil {
log.Logger.Error(err)
}
response.OKRespWithData(c, map[string]interface{}{
"next_time": nextTime,
"video_list": empty,
})
}
}
1 change: 1 addition & 0 deletions module/middleware/cache/feed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cache
22 changes: 3 additions & 19 deletions module/middleware/cache/publish.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
package cache

import (
"bytes"
"encoding/json"
"github.com/Biu-X/TikTok/module/cache"

"github.com/Biu-X/TikTok/module/log"
"github.com/gin-gonic/gin"
)

func PublishMiddleware() gin.HandlerFunc {
func PublishMiddleware(rc *cache.Client, service gin.HandlerFunc, empty interface{}) gin.HandlerFunc {
return func(c *gin.Context) {
log.Logger.Infof("in publish")
writer := responseWriter{
c.Writer,
bytes.NewBuffer([]byte{}),
}
c.Writer = writer
var m map[string]interface{}
err := json.Unmarshal(writer.b.Bytes(), &m)
if err != nil {
return
}
c.Next()
log.Logger.Infof("map ------------> %v", m)
log.Logger.Infof("response body: %v", writer.b.String())
log.Logger.Infof("out publish")

}
}
5 changes: 4 additions & 1 deletion router/api/v1/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"github.com/Biu-X/TikTok/module/response"
"net/http"
"time"

Expand Down Expand Up @@ -35,7 +36,9 @@ func NewAPI() *gin.Engine {
tiktok := r.Group("/douyin/")
{
// 视频流接口
tiktok.GET("feed/", jwt.RequireAuthWithoutLogin(), feed_service.List)
tiktok.GET("feed/", jwt.RequireAuthWithoutLogin(),
middleware_cache.FeedMiddleware(middleware_cache.Clients[cache.Feed], feed_service.List, []response.VideoResponse{}),
)

user := tiktok.Group("user/")
{
Expand Down
3 changes: 2 additions & 1 deletion service/feed/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func List(c *gin.Context) {
nextTime = strconv.FormatInt(t.UnixMilli(), 10)
}
log.Logger.Debugf("nextTime: %v", nextTime)

c.Set("nextTime", nextTime)
c.Set("feed", videoList)
response.OKRespWithData(c, map[string]interface{}{
"next_time": nextTime,
"video_list": videoList,
Expand Down

0 comments on commit 9f373ca

Please sign in to comment.