Browse Source

新增定时刷新

longyu 2 years ago
parent
commit
5cf6484a2e
5 changed files with 51 additions and 2 deletions
  1. 1 1
      core/run_server.go
  2. 6 1
      global/global.go
  3. 12 0
      init_serve/redis.go
  4. 8 0
      services/index.go
  5. 24 0
      services/queue.go

+ 1 - 1
core/run_server.go

@@ -14,7 +14,7 @@ func RunServe() {
 	//如果配置了redis,那么链接redis
 	//如果配置了redis,那么链接redis
 	//if global.CONFIG.Serve.UseRedis {
 	//if global.CONFIG.Serve.UseRedis {
 	//	//初始化redis
 	//	//初始化redis
-	//	init_serve.Redis()
+	//	init_serve.RedisTool()
 	//}
 	//}
 	//初始化验证器
 	//初始化验证器
 	//if err := global.InitTrans("zh"); err != nil {
 	//if err := global.InitTrans("zh"); err != nil {

+ 6 - 1
global/global.go

@@ -11,6 +11,8 @@ import (
 	"hongze/mysteel_watch/config"
 	"hongze/mysteel_watch/config"
 	"hongze/mysteel_watch/utils"
 	"hongze/mysteel_watch/utils"
 	"io"
 	"io"
+
+	"github.com/rdlucklib/rdluck_tools/cache"
 )
 )
 
 
 var (
 var (
@@ -21,9 +23,12 @@ var (
 	DEFAULT_MYSQL *gorm.DB      //默认数据库连接配置
 	DEFAULT_MYSQL *gorm.DB      //默认数据库连接配置
 	Redis         *redis.Client //redis链接
 	Redis         *redis.Client //redis链接
 	EsClient      *elastic.Client
 	EsClient      *elastic.Client
+
+	Rc *cache.Cache //redis缓存
+	Re error        //redis错误
 )
 )
 
 
-const ConfigFile = "config/config.yaml"                               //本地(测试)环境下的配置文件地址
+const ConfigFile = "config/config.yaml"    //本地(测试)环境下的配置文件地址
 const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
 const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
 
 
 func init() {
 func init() {

+ 12 - 0
init_serve/redis.go

@@ -2,8 +2,11 @@ package init_serve
 
 
 import (
 import (
 	"context"
 	"context"
+	"fmt"
 	"github.com/go-redis/redis/v8"
 	"github.com/go-redis/redis/v8"
 	"hongze/mysteel_watch/global"
 	"hongze/mysteel_watch/global"
+
+	"github.com/rdlucklib/rdluck_tools/cache"
 )
 )
 
 
 func Redis() {
 func Redis() {
@@ -23,3 +26,12 @@ func Redis() {
 	//全局赋值redis链接
 	//全局赋值redis链接
 	global.Redis = client
 	global.Redis = client
 }
 }
+
+func RedisTool() {
+	REDIS_CACHE:=""
+	global.Rc, global.Re = cache.NewCache(REDIS_CACHE) //初始化缓存
+	if global.Re != nil {
+		fmt.Println(global.Re)
+		panic(global.Re)
+	}
+}

+ 8 - 0
services/index.go

@@ -11,6 +11,7 @@ import (
 	"os"
 	"os"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+	"sync"
 	"time"
 	"time"
 )
 )
 
 
@@ -134,8 +135,11 @@ func IndexRefreshAll() {
 	return
 	return
 }
 }
 
 
+var lock sync.RWMutex
+
 //刷新周度指标数据
 //刷新周度指标数据
 func IndexRefreshWeek() {
 func IndexRefreshWeek() {
+	lock.Lock()
 	nowWeek := time.Now().Weekday().String()
 	nowWeek := time.Now().Weekday().String()
 	nowWeekZn := utils.GetWeekZn(nowWeek)
 	nowWeekZn := utils.GetWeekZn(nowWeek)
 	fmt.Println("nowWeekZn:" + nowWeekZn)
 	fmt.Println("nowWeekZn:" + nowWeekZn)
@@ -172,5 +176,9 @@ func IndexRefreshWeek() {
 			global.LOG.Info("无效频度:" + v.IndexCode + ";" + v.Frequency)
 			global.LOG.Info("无效频度:" + v.IndexCode + ";" + v.Frequency)
 		}
 		}
 	}
 	}
+	lock.Unlock()
 	return
 	return
 }
 }
+
+//使用redis队列
+

+ 24 - 0
services/queue.go

@@ -0,0 +1,24 @@
+package services
+
+import (
+	"fmt"
+	"hongze/mysteel_watch/global"
+)
+
+var (
+	REFRESH_INDEX_CODE = "REFRESH_INDEX_CODE_"
+)
+
+// the service for log
+func AutoInsertLogToDB() {
+	defer func() {
+		if err := recover(); err != nil {
+			fmt.Println("[AutoInsertLogToDB]", err)
+		}
+	}()
+	for {
+		global.Rc.Brpop(REFRESH_INDEX_CODE, func(b []byte) {
+			fmt.Println("result:" + string(b))
+		})
+	}
+}