Skip to content

Commit

Permalink
add sub cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sec-an committed Jan 20, 2023
1 parent edb55c8 commit fa3d10f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Redis:
Database: 0
IdleTimeout: 5 #
PoolSize: 100
SubCacheTime: 15 # 分钟,订阅缓存时间
Log:
Path: ./logs
FilePrefix: tvh
Expand Down
33 changes: 29 additions & 4 deletions internal/parser/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/go-redis/redis/v8"

"go.uber.org/zap"

Expand Down Expand Up @@ -45,15 +48,37 @@ func ConfigHandler(c *gin.Context) {
return
}

subscribe := &common.Config{} // 最终订阅
var subscribe common.Config

if global.RedisSetting.Running {
keyName := strings.Join([]string{"config", filename}, "_")
cacheStr, err := global.RedisClient.Get(c, keyName).Result()
if err == redis.Nil {
global.Logger.Debug(keyName, zap.Error(err))
subscribe = getScribe(parser)
data, _ := json.Marshal(subscribe)
_ = global.RedisClient.Set(c, keyName, data, global.RedisSetting.SubCacheTime).Err()
} else if err != nil {
global.Logger.Error(keyName, zap.Error(err))
} else {
_ = json.Unmarshal([]byte(cacheStr), &subscribe)
}
} else {
subscribe = getScribe(parser)
}

c.PureJSON(200, subscribe)
}

func getScribe(parser Parser) (subscribe common.Config) {
for _, itemSubscribe := range parser.Subscribe {
tmpSubscribe := &common.Config{}
// 已存在有效订阅,且当前订阅非强制展示
if len(subscribe.Sites) != 0 && !itemSubscribe.AlwaysOn {
continue
}
if data := getJson(itemSubscribe.Url); data != "" {
err = json.Unmarshal([]byte(data), tmpSubscribe)
err := json.Unmarshal([]byte(data), tmpSubscribe)
if err != nil {
// 订阅失效,更换下一订阅
global.Logger.Error(itemSubscribe.Url+":订阅失效", zap.Error(err))
Expand Down Expand Up @@ -81,7 +106,7 @@ func ConfigHandler(c *gin.Context) {
}
if len(subscribe.Sites) == 0 {
// 第一个有效订阅
subscribe = tmpSubscribe
subscribe = *tmpSubscribe
} else {
// 合并订阅
subscribe.Ads = append(subscribe.Ads, tmpSubscribe.Ads...)
Expand Down Expand Up @@ -119,5 +144,5 @@ func ConfigHandler(c *gin.Context) {
}
subscribe.Sites = sitesFinal
}
c.PureJSON(200, subscribe)
return
}
1 change: 0 additions & 1 deletion internal/routers/doubanhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func douBanHandler(c *gin.Context) {
_ = global.RedisClient.Set(c, "real_time_hotest", data, 30*time.Minute).Err()
} else if err != nil {
global.Logger.Error("subjectRealTimeHotest", zap.Error(err))
panic(err)
} else {
_ = json.Unmarshal([]byte(realTimeHotestStr), &subjectRealTimeHotest)
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func setupSetting() error {
global.ServerSetting.ReadTimeout *= time.Second
global.ServerSetting.WriteTimeout *= time.Second
global.RedisSetting.IdleTimeout *= time.Second
global.RedisSetting.SubCacheTime *= time.Minute
global.SpiderSetting.DouBanClientTimeout *= time.Millisecond
global.SpiderSetting.ParserClientTimeout *= time.Millisecond
return nil
Expand Down
15 changes: 8 additions & 7 deletions pkg/setting/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ type SpiderSettingS struct {
}

type RedisSettingS struct {
Running bool
Host string
Port uint16
Auth string
Database int
IdleTimeout time.Duration
PoolSize int
Running bool
Host string
Port uint16
Auth string
Database int
IdleTimeout time.Duration
PoolSize int
SubCacheTime time.Duration
}

func (s *Setting) ReadSection(k string, v interface{}) error {
Expand Down

0 comments on commit fa3d10f

Please sign in to comment.