瀏覽代碼

查询数据走缓存处理

WH01243 1 年之前
父節點
當前提交
77b2906aca
共有 3 個文件被更改,包括 25 次插入10 次删除
  1. 2 1
      api/etc/db.yaml
  2. 2 5
      api/internal/logic/sqlmanagelogic.go
  3. 21 4
      api/internal/service/sqlService.go

+ 2 - 1
api/etc/db.yaml

@@ -1,6 +1,7 @@
 redis:
     addr:
-        - session=192.168.3.149:1713
+        - session=192.168.3.149:1713,
+        - other=192.168.3.149:1712,
 mongo:
     bidding:
         address: 192.168.3.206:27002

+ 2 - 5
api/internal/logic/sqlmanagelogic.go

@@ -28,10 +28,7 @@ func (l *SqlManageLogic) SqlManage(req *types.SqlManageReq) (resp *types.Reply,
 	// todo: add your logic here and delete this line
 	resp = &types.Reply{}
 	sqlService := &service.SqlService{}
-	data, count := sqlService.SqlManage(req)
-	resp.Data = map[string]interface{}{
-		"data":  data,
-		"count": count,
-	}
+	data := sqlService.SqlManage(req)
+	resp.Data = data
 	return
 }

+ 21 - 4
api/internal/service/sqlService.go

@@ -4,34 +4,51 @@ import (
 	"app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/encrypt"
 	elastic "app.yhyue.com/moapp/jybase/es"
+	"app.yhyue.com/moapp/jybase/redis"
 	MC "bp.jydev.jianyu360.cn/BaseService/biCenter/api/common"
 	"bp.jydev.jianyu360.cn/BaseService/biCenter/api/internal/types"
 	"fmt"
+	"github.com/gogf/gf/v2/util/gconv"
 )
 
 type SqlService struct {
 }
 
-func (l SqlService) SqlManage(in *types.SqlManageReq) (*[]map[string]interface{}, int64) {
+func (l SqlService) SqlManage(in *types.SqlManageReq) map[string]interface{} {
 	sqlData := MC.BaseMysql.FindOne("sql_manage", map[string]interface{}{
 		"id": common.Int64All(in.Id),
 	}, "", "")
 	if sqlData == nil || len(*sqlData) == 0 {
-		return &[]map[string]interface{}{}, 0
+		return map[string]interface{}{}
 	}
 	dbType := (*sqlData)["db_type"]
+	cache_time := common.IntAll((*sqlData)["cache_time"])
+	key := common.GetMd5String(gconv.String(in))
+	if cache_time != 0 {
+		dataStr := redis.Get("other", key)
+		if dataStr != nil {
+			data, _ := dataStr.(map[string]interface{})
+			return data
+		}
+	}
 	rs := &[]map[string]interface{}{}
 	count := int64(0)
 	switch dbType {
 	case "es":
 		count, rs = elastic.GetWithCount(common.InterfaceToStr((*sqlData)["db_name"]), common.InterfaceToStr((*sqlData)["db_name"]), "", Sqlformat(common.InterfaceToStr((*sqlData)["content"]), *in))
 	}
-
 	for k, v := range *rs {
 		(*rs)[k]["id"] = encrypt.EncodeArticleId2ByCheck(common.InterfaceToStr(v["id"]))
 		delete((*rs)[k], "_id")
 	}
-	return rs, count
+	result := map[string]interface{}{
+		"data":  rs,
+		"count": count,
+	}
+	if count > 0 && cache_time != 0 {
+		redis.Put("other", key, result, cache_time)
+	}
+	return result
 }
 func Sqlformat(sqlStr string, sqlData types.SqlManageReq) string {
 	data := []interface{}{}