Przeglądaj źródła

fix:supply默认搜索存redis

duxin 3 lat temu
rodzic
commit
a2a77a79c6

+ 1 - 0
rpc/consumer/etc/consumer.yaml

@@ -9,6 +9,7 @@ Etcd:
   Key: consumer.rpc
 Webrpcport: 8013
 Timeout:  10000
+SupplyDay: 1
 MysqlMain:
   dbName: base_service
   address: 192.168.3.11:3366

+ 4 - 0
rpc/consumer/init/init.go

@@ -36,6 +36,10 @@ func init() {
 	pc := C.PublishCity
 	model.PublishCityInfo(pc)
 
+	//初始redis 时间
+	sd := C.SupplyDay
+	model.SupplyDay(sd)
+
 	//初始es
 	es := C.Es
 	model.InitEs(&es)

+ 1 - 0
rpc/consumer/internal/config/config.go

@@ -16,4 +16,5 @@ type Config struct {
 	Es          entity.EsStruct
 	Oss         entity.OssStruct
 	PublishCity map[string]string
+	SupplyDay   int64
 }

+ 13 - 7
rpc/model/db.go

@@ -4,6 +4,7 @@
 package model
 
 import (
+	mc "app.yhyue.com/moapp/jybase/common"
 	"app.yhyue.com/moapp/jybase/mongodb"
 	"fmt"
 	"log"
@@ -16,12 +17,13 @@ import (
 )
 
 var (
-	Mysql       *mysql.Mysql
-	NsqConfig   *entity.NsqStruct
-	SupplyTotal int
-	Sensitive   *entity.Sensitive
-	Mgo         *mongodb.MongodbSim
-	PublishCity *map[string]string
+	Mysql        *mysql.Mysql
+	NsqConfig    *entity.NsqStruct
+	SupplyTotal  int
+	SupplyRedDay int
+	Sensitive    *entity.Sensitive
+	Mgo          *mongodb.MongodbSim
+	PublishCity  *map[string]string
 )
 
 func SupplyTotalConfig(mm int) {
@@ -33,6 +35,11 @@ func PublishCityInfo(pb map[string]string) {
 	PublishCity = &pb
 }
 
+func SupplyDay(pb int64) {
+	log.Println("--初始化 supplyDay--")
+	SupplyRedDay = mc.IntAll(pb)
+}
+
 func InitNsqConfig(mm *entity.NsqStruct) {
 	if mm.Ip != "" {
 		log.Println("--初始化 nsq--")
@@ -45,7 +52,6 @@ func InitNsqConfig(mm *entity.NsqStruct) {
 
 func InitMgoConfig(mm *entity.Mgo) {
 	if mm.Address != "" {
-		log.Println("--初始化 nsq--")
 		log.Println("初始化 mongodb main")
 		Mgo = &mongodb.MongodbSim{
 			MongodbAddr: mm.Address,

+ 49 - 2
rpc/model/es/es.go

@@ -1,6 +1,9 @@
 package model
 
 import (
+	"app.yhyue.com/moapp/jyInfo/rpc/model"
+	mc "app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/redis"
 	"fmt"
 	"log"
 	"strconv"
@@ -8,7 +11,6 @@ import (
 	"time"
 
 	"app.yhyue.com/moapp/jyInfo/rpc/consumer/consumerinfo"
-
 	"app.yhyue.com/moapp/jybase/esv1"
 )
 
@@ -32,6 +34,13 @@ func GetSupplyInfoList(in *consumerinfo.SupplyInfoSearchReq, b bool) (list *[]ma
 	} else {
 		in.SearchType = `"title"`
 	}
+
+	//默认信息查询redis
+	if in.Keywords == "" && in.City == "" && in.SearchType == "title" && in.Province == "" && in.Time == "" && in.Status == "0" && in.PageIndex <= 10 {
+		total, list = SupplyRedis(in)
+		return
+	}
+
 	qstr := GetSearchQuery(in)
 	if in.PageIndex == 1 {
 		total = elastic.Count(INDEX, TYPE, qstr)
@@ -46,7 +55,45 @@ func GetSupplyInfoList(in *consumerinfo.SupplyInfoSearchReq, b bool) (list *[]ma
 	return
 }
 
-//
+//SupplyRedis 默认搜索时添加redis
+func SupplyRedis(in *consumerinfo.SupplyInfoSearchReq) (int64, *[]map[string]interface{}) {
+	var (
+		listData []map[string]interface{}
+		total    int64
+		list     []map[string]interface{}
+	)
+	supplyData := redis.Get("other", "supply_search_key")
+	if supplyData == nil {
+		qstr := GetSearchQuery(in)
+		if in.PageIndex == 1 {
+			total = elastic.Count(INDEX, TYPE, qstr)
+		}
+		lists := elastic.GetAllByNgram(INDEX, TYPE, qstr, ``, Search_sort, Search_field, 0, 500, 0, false)
+		if lists == nil || len(*lists) == 0 {
+			return total, &list
+		}
+		listData = *lists
+		redisData := map[string]interface{}{
+			"total": total,
+			"list":  listData,
+		}
+		redis.Put("other", "supply_search_key", redisData, model.SupplyRedDay)
+	} else {
+		data, _ := supplyData.(map[string]interface{})
+		if in.PageIndex == 1 {
+			total = mc.Int64All(data["total"])
+		}
+		listData = data["list"].([]map[string]interface{})
+	}
+	if in.PageSize*in.PageIndex <= mc.Int64All(len(listData)) {
+		list = listData[(in.PageSize-1)*in.PageIndex : in.PageSize*in.PageIndex]
+	} else if (in.PageSize-1)*in.PageIndex <= mc.Int64All(len(listData)) {
+		list = listData[(in.PageSize-1)*in.PageIndex:]
+	}
+
+	return total, &list
+}
+
 func GetSearchQuery(in *consumerinfo.SupplyInfoSearchReq) (qstr string) {
 	query := `{"query":{"bool":{"must":[%s],"must_not":[%s]}}}`
 	multi_match := `{"multi_match": {"query": "%s","type": "phrase", "fields": [%s]}}`