Browse Source

feat:并发处理

wangshan 3 years ago
parent
commit
32beef7eb8
1 changed files with 72 additions and 60 deletions
  1. 72 60
      jyBXSubscribe/rpc/model/push.go

+ 72 - 60
jyBXSubscribe/rpc/model/push.go

@@ -18,6 +18,7 @@ import (
 	"log"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 )
 
@@ -739,71 +740,82 @@ func (s *subscribePush) DefaultDatas(spqp *SubPushQueryParam) (hasNextPage bool,
 		logx.Info(time.Since(t1), "count:", len(*list))
 		if list != nil && len(*list) > 0 {
 			now := time.Now()
+			var (
+				wg = &sync.WaitGroup{}
+				wc = make(chan bool, 20)
+			)
 			for _, v := range *list {
-				redisKey := fmt.Sprintf("pushinfo_%s_%s", spqp.UserId, common.ObjToString(v["_id"]))
-				title := strings.Replace(common.ObjToString(v["title"]), "\n", "", -1)
-				var myInsert = make(map[string]interface{})
-				myInsert["userid"] = spqp.UserId
-				myInsert["infoid"] = common.ObjToString(v["_id"])
-				myInsert["date"] = now.Unix()
-				matchkeys := getKeys(title, scd.Keyword)
-				myInsert["matchkeys"] = strings.Join(matchkeys, " ")
-				if v["area"] != nil {
-					myInsert["area"] = common.If(common.ObjToString(v["area"]) == "A", 0, common.If(IC.PushMapping.Area[common.ObjToString(v["area"])] > 0, IC.PushMapping.Area[common.ObjToString(v["area"])], 0).(int)).(int)
-				}
-				if v["city"] != nil {
-					myInsert["city"] = common.If(IC.PushMapping.City[common.ObjToString(v["city"])] > 0, IC.PushMapping.City[common.ObjToString(v["city"])], 0).(int)
-				}
-				if v["subtype"] != nil {
-					myInsert["subtype"] = common.If(IC.PushMapping.Subtype[common.ObjToString(v["subtype"])] > 0, IC.PushMapping.Subtype[common.ObjToString(v["subtype"])], 0).(int)
-				}
-				if v["toptype"] != nil {
-					myInsert["toptype"] = common.If(IC.PushMapping.Toptype[common.ObjToString(v["toptype"])] > 0, IC.PushMapping.Toptype[common.ObjToString(v["toptype"])], 0).(int)
-				}
-				if v["buyerclass"] != nil {
-					myInsert["buyerclass"] = common.If(IC.PushMapping.Buyerclass[common.ObjToString(v["buyerclass"])] > 0, IC.PushMapping.Buyerclass[common.ObjToString(v["buyerclass"])], 0).(int)
-				}
-				if v["filetext"] != nil {
-					myInsert["attachment_count"] = common.If(v["filetext"] != nil, 1, 0).(int) //附件 :检索库 只有附件字段,无法识别附件数量 暂定为1;为识别有附件
-				}
-				if v["bidamount"] != nil {
-					myInsert["bidamount"] = common.Int64All(v["bidamount"])
-				}
-				if v["budget"] != nil {
-					myInsert["budget"] = common.Int64All(v["budget"])
-				}
-				if s_subscopeclass := common.ObjToString(v["s_subscopeclass"]); s_subscopeclass != "" {
-					subscopeclass := []string{}
-					for _, v := range strings.Split(s_subscopeclass, ",") {
-						if subscopeclass_mapping, ok := IC.PushMapping.Subscopeclass[v]; ok {
-							subscopeclass = append(subscopeclass, fmt.Sprint(subscopeclass_mapping))
+				go func(v map[string]interface{}) {
+					wg.Add(1)
+					wc <- true
+					defer func() {
+						wg.Done()
+						<-wc
+					}()
+					redisKey := fmt.Sprintf("pushinfo_%s_%s", spqp.UserId, common.ObjToString(v["_id"]))
+					title := strings.Replace(common.ObjToString(v["title"]), "\n", "", -1)
+					var myInsert = make(map[string]interface{})
+					myInsert["userid"] = spqp.UserId
+					myInsert["infoid"] = common.ObjToString(v["_id"])
+					myInsert["date"] = now.Unix()
+					matchkeys := getKeys(title, scd.Keyword)
+					myInsert["matchkeys"] = strings.Join(matchkeys, " ")
+					if v["area"] != nil {
+						myInsert["area"] = common.If(common.ObjToString(v["area"]) == "A", 0, common.If(IC.PushMapping.Area[common.ObjToString(v["area"])] > 0, IC.PushMapping.Area[common.ObjToString(v["area"])], 0).(int)).(int)
+					}
+					if v["city"] != nil {
+						myInsert["city"] = common.If(IC.PushMapping.City[common.ObjToString(v["city"])] > 0, IC.PushMapping.City[common.ObjToString(v["city"])], 0).(int)
+					}
+					if v["subtype"] != nil {
+						myInsert["subtype"] = common.If(IC.PushMapping.Subtype[common.ObjToString(v["subtype"])] > 0, IC.PushMapping.Subtype[common.ObjToString(v["subtype"])], 0).(int)
+					}
+					if v["toptype"] != nil {
+						myInsert["toptype"] = common.If(IC.PushMapping.Toptype[common.ObjToString(v["toptype"])] > 0, IC.PushMapping.Toptype[common.ObjToString(v["toptype"])], 0).(int)
+					}
+					if v["buyerclass"] != nil {
+						myInsert["buyerclass"] = common.If(IC.PushMapping.Buyerclass[common.ObjToString(v["buyerclass"])] > 0, IC.PushMapping.Buyerclass[common.ObjToString(v["buyerclass"])], 0).(int)
+					}
+					if v["filetext"] != nil {
+						myInsert["attachment_count"] = common.If(v["filetext"] != nil, 1, 0).(int) //附件 :检索库 只有附件字段,无法识别附件数量 暂定为1;为识别有附件
+					}
+					if v["bidamount"] != nil {
+						myInsert["bidamount"] = common.Int64All(v["bidamount"])
+					}
+					if v["budget"] != nil {
+						myInsert["budget"] = common.Int64All(v["budget"])
+					}
+					if s_subscopeclass := common.ObjToString(v["s_subscopeclass"]); s_subscopeclass != "" {
+						subscopeclass := []string{}
+						for _, v := range strings.Split(s_subscopeclass, ",") {
+							if subscopeclass_mapping, ok := IC.PushMapping.Subscopeclass[v]; ok {
+								subscopeclass = append(subscopeclass, fmt.Sprint(subscopeclass_mapping))
+							}
+						}
+						if len(subscopeclass) > 0 {
+							myInsert["subscopeclass"] = strings.Join(subscopeclass, ",")
 						}
 					}
-					if len(subscopeclass) > 0 {
-						myInsert["subscopeclass"] = strings.Join(subscopeclass, ",")
+					//商机管理用户
+					if s.ModuleFlag == "eType" {
+						myInsert["userid"] = spqp.EntUserId
+						myInsert["entid"] = spqp.EntId
+						myInsert["deptid"] = spqp.DeptId
 					}
-				}
-				//商机管理用户
-				if s.ModuleFlag == "eType" {
-					myInsert["userid"] = spqp.EntUserId
-					myInsert["entid"] = spqp.EntId
-					myInsert["deptid"] = spqp.DeptId
-				}
-				//保存推送表
-				id := spqp.PushMysql.Insert(aboutDbMsg[s.ModuleFlag].MysqlTable, myInsert)
-				if id > 0 {
-					redis.Put("pushcache_2_a", redisKey, 1, 86400)
-				} else {
-					continue
-				}
-				result = append(result, s.InfoFormat(&PushCa{
-					InfoId:     common.ObjToString(v["_id"]),
-					Date:       now.Unix(),
-					Index:      id,
-					Keys:       matchkeys,
-					FileExists: v["filetext"] != nil,
-				}, &v))
+					//保存推送表
+					id := spqp.PushMysql.Insert(aboutDbMsg[s.ModuleFlag].MysqlTable, myInsert)
+					if id > 0 {
+						redis.Put("pushcache_2_a", redisKey, 1, 86400)
+						result = append(result, s.InfoFormat(&PushCa{
+							InfoId:     common.ObjToString(v["_id"]),
+							Date:       now.Unix(),
+							Index:      id,
+							Keys:       matchkeys,
+							FileExists: v["filetext"] != nil,
+						}, &v))
+					}
+				}(v)
 			}
+			wg.Wait()
 		}
 	}
 	logx.Info("请求耗时:", time.Since(t2))