-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
75 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters