Browse Source

Merge branch 'hotfix/v1.1.26.2_ws' of BaseService/jyMicroservices into hotfix/v1.1.26.2

wangshan 2 năm trước cách đây
mục cha
commit
07981cb50f

+ 4 - 3
jyBXBase/rpc/etc/bxbase.yaml

@@ -13,16 +13,17 @@ BidSearchOldUserLimit: 1626105600
 FileSignBool: true
 UnitAppend: '+'
 NewsLimitNum: 50 # 移动端首页最新标讯数量限制
+NewsTimeOut: 7200
 NewsCache: # 下面timeout 单位是秒
   NoLogin: # 未登录用户缓存使用的key
     Key: p1_indexMessage_new_noLogin_%d_%d_%d
     Timeout: 86400  #
   LoginUser: # 登录用户有推送数据缓存使用的key
     Key: p1_indexMessage_new_%d_%d_%d_%d
-    Timeout: 900
+    Timeout: 86400
   Login: # 登录用户没有推送数据最新标讯缓存使用的key
     Key: p1_indexMessage_new_login_%d_%d_%d
-    Timeout: 900
+    Timeout: 86400
   Count: # 用户有没有推送数据缓存使用的key
     Key: push_count_%s_%s
-    Timeout: 60
+    Timeout: 7200

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

@@ -16,6 +16,7 @@ type Config struct {
 	PowerCheckCenterKey   string
 	EntManageApplication  string
 	NewsLimitNum          int64    // 移动端首页最新标讯数量限制
+	NewsTimeOut           int64    //更新数据时间
 	NewsCache             struct { // 最新标讯缓存配置
 		NoLogin   cacheConfig // 未登录用户缓存key
 		LoginUser cacheConfig // 登录用户有推送数据使用的key

+ 58 - 15
jyBXBase/rpc/internal/logic/newestbiddinglogic.go

@@ -1,7 +1,9 @@
 package logic
 
 import (
+	"app.yhyue.com/moapp/jybase/redis"
 	"context"
+	IC "jyBXBase/rpc/init"
 	"jyBXBase/rpc/model"
 	"log"
 	"sort"
@@ -27,11 +29,18 @@ func NewNewestBiddingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *New
 	}
 }
 
+type newSet struct {
+	status      int
+	redisKey    string
+	redisStatus int
+	query       string
+}
+
 // 首页最新招标信息
 func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, error) {
 	t := time.Now()
 	//  status : 0 -不存缓存 1-未登录用户 2-用户自己的key 3-登录用户最新标讯
-	r, status := func(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, int) {
+	r, n := func(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, *newSet) {
 		var res = &bxbase.NewsetBiddingResp{
 			Data: &bxbase.NewsetBidding{
 				List: []*bxbase.NewestList{},
@@ -43,22 +52,24 @@ func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase
 		*/
 		// 未登录用户
 		if in.UserId == "" {
+			// 没数据查最新数据返回
+			//未登录用户访问全部信息类型 需要过滤掉 拟建和采购意向
+			subtype := `"预告","公告","结果","其它"`
+			query := model.NewestQuery("", "", subtype)
 			res.Data.ShowTip = 1
 			redisKey, _ := model.GetRedisKeyTimeout(model.StatusNoLogin, in.PositionId)
 			list, err := model.GetNewsCache(redisKey)
 			if err == nil && list != nil && len(list) > 0 { // 缓存有数据可以直接返回
 				res.Data.List = list
 				res.Data.Count = int64(len(list))
-				return res, model.StatusCache
+				return res, &newSet{model.StatusCache, redisKey, model.StatusNoLogin, query}
 			}
-			// 没数据查最新数据返回
-			//未登录用户访问全部信息类型 需要过滤掉 拟建和采购意向
-			subtype := `"预告","公告","结果","其它"`
-			query := model.NewestQuery("", "", subtype)
+			noLoginTime := time.Now()
 			list = model.NewestES(query)
 			res.Data.List = list
 			res.Data.Count = int64(len(list))
-			return res, model.StatusNoLogin
+			log.Println("未登录获es取数据耗时:", time.Since(noLoginTime).Seconds())
+			return res, &newSet{model.StatusNoLogin, redisKey, model.StatusNoLogin, query}
 		}
 
 		// 登录用户
@@ -74,45 +85,77 @@ func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase
 			if err == nil && list != nil && len(list) > 0 {
 				res.Data.List = list
 				res.Data.Count = int64(len(list))
-				return res, model.StatusCache
+				return res, &newSet{model.StatusCache, redisKey, model.StatusLoginUser, ""}
 			}
 			//  查推送
+			subscribeTime := time.Now()
 			list = roleNewestInfo.GetPushHistory()
+			log.Println("获取订阅数据耗时:", time.Since(subscribeTime).Seconds())
 			if list != nil && len(list) > 0 {
 				res.Data.List = list
 				res.Data.Count = int64(len(list))
-				return res, model.StatusLoginUser
+				return res, &newSet{model.StatusLoginUser, redisKey, model.StatusLoginUser, ""}
 			}
 		}
 		// 等0则说明推送里面没有数据 所以去查最新标讯信息缓存 返回  缓存没有则查最新标讯信息 存到缓存里面
 		res.Data.ShowTip = 2 // 显示
+		//  查最新标讯
+		query := model.NewestQuery("", "", "")
 		// 查缓存
 		redisKey, _ := model.GetRedisKeyTimeout(model.StatusLogin, in.PositionId)
 		list, err := model.GetNewsCache(redisKey)
 		if err == nil && list != nil && len(list) > 0 {
 			res.Data.List = list
 			res.Data.Count = int64(len(list))
-			return res, model.StatusCache
+			return res, &newSet{model.StatusCache, redisKey, model.StatusLogin, query}
 		}
-		//  查最新标讯
-		query := model.NewestQuery("", "", "")
+		esTime := time.Now()
 		list = model.NewestES(query)
+		log.Println("获取es数据耗时:", time.Since(esTime).Seconds())
 		res.Data.List = list
 		res.Data.Count = int64(len(list))
-		return res, model.StatusLogin
+		return res, &newSet{model.StatusLogin, redisKey, model.StatusLogin, query}
 	}(in)
 	if r.Data.Count == 0 {
 		return r, nil
 	}
 	// status : 0 -拿到的是缓存 不用再处理也不用存缓存 1-存到未登录用户 2-存到用户自己的key 3-存到登录用户最新标讯
-	if status != model.StatusCache {
+	var sortRedis = func(r *bxbase.NewsetBiddingResp, status int, positionId int64) {
 		//排序
 		sort.Slice(r.Data.List, func(i, j int) bool {
 			return r.Data.List[i].PublishTime > r.Data.List[j].PublishTime
 		})
-		redisKey, timeout := model.GetRedisKeyTimeout(status, in.PositionId)
+		redisKey, timeout := model.GetRedisKeyTimeout(status, positionId)
 		go model.PutNewsCache(redisKey, timeout, r.Data.List)
 	}
+	if n.status != model.StatusCache {
+		go sortRedis(r, n.status, in.PositionId)
+	} else {
+		//获取最新数据 --  延长缓存时间
+		go func(n *newSet, in *bxbase.NewestBiddingReq) {
+			if n.redisKey != "" {
+				//剩余时间 在 IC.C.NewsTimeOut 之内
+				if ttl := redis.GetTTL("new", n.redisKey); ttl-IC.C.NewsTimeOut < 0 {
+					var res = &bxbase.NewsetBiddingResp{
+						Data: &bxbase.NewsetBidding{
+							List: []*bxbase.NewestList{},
+						},
+					}
+					switch n.redisStatus {
+					case model.StatusLoginUser:
+						// 登录用户
+						roleNewestInfo, _ := model.GetRoleNewestInfoService(in.AppId, in.MgoUserId, in.NewUserId, in.AccountId, in.EntId, in.EntUserId, in.PositionType, in.PositionId)
+						//  查推送
+						res.Data.List = roleNewestInfo.GetPushHistory()
+					default:
+						res.Data.List = model.NewestES(n.query)
+					}
+					go sortRedis(res, n.redisStatus, in.PositionId)
+				}
+			}
+		}(n, in)
+	}
+
 	model.MakeCollection(in.UserId, r.Data.List)
 	log.Println("接口耗时:", time.Since(t).Seconds())
 	return r, nil

+ 10 - 6
jyBXBase/rpc/model/newestBidding.go

@@ -88,17 +88,21 @@ func GetNewestInfo(userId, userType string, newUserId int64) *NewestInfo {
 // 根据GetPushHistoryCount 返回值 作为列表缓存key中一个参数
 func (n *NewestInfo) GetPushHistoryCount() int64 {
 	countKey := fmt.Sprintf(IC.C.NewsCache.Count.Key, n.TableName, n.UserId)
-	if b, err := redis.Exists("new", countKey); err == nil && b {
-		return 1
+	if b := redis.GetInt("new", countKey); b != 0 {
+		return int64(b)
 	}
 	countSql := "SELECT COUNT(1) FROM (SELECT 1 FROM %s WHERE userid = %d LIMIT 1) AS ph"
 	//countSql := "select count(1) from %s  a  where a.userid=%d order by a.id desc"
 	countSql = fmt.Sprintf(countSql, n.TableName, n.NewUserId)
-	if c := n.MysqlDb.CountBySql(countSql); c > 0 {
-		redis.Put("new", countKey, c, IC.C.NewsCache.Count.Timeout) //过期时间走配置,比最新标讯列表缓存时间 要短1/10 尽量不超过两分钟
-		return c
+	timeOut := IC.C.NewsCache.Count.Timeout
+	c := n.MysqlDb.CountBySql(countSql)
+	if c > 0 {
+		timeOut = timeOut * 4
+	} else {
+		c = -1
 	}
-	return 0
+	redis.Put("new", countKey, c, IC.C.NewsCache.Count.Timeout) //过期时间走配置,比最新标讯列表缓存时间 要短1/10 尽量不超过两分钟
+	return c
 }
 
 func (n *NewestInfo) GetPushHistory() (res []*bxbase.NewestList) {