Przeglądaj źródła

Merge branch '1.1.30_ws' of BaseService/jyMicroservices into feature/v1.1.30

wangshan 1 rok temu
rodzic
commit
6bf672848a

+ 6 - 0
jyBXBase/rpc/etc/bxbase.yaml

@@ -18,12 +18,18 @@ NewsCache: # 下面timeout 单位是秒
   NoLogin: # 未登录用户缓存使用的key
     Key: p1_indexMessage_new_noLogin_%d_%d_%d
     Timeout: 86400  #
+    CacheUpdateKey: noLogin
+    CacheUpdateTimeout: 900  #
   LoginUser: # 登录用户有推送数据缓存使用的key
     Key: p1_indexMessage_new_%d_%d_%d_%d
     Timeout: 86400
+    CacheUpdateKey: subscribe
+    CacheUpdateTimeout: 900  #
   Login: # 登录用户没有推送数据最新标讯缓存使用的key
     Key: p1_indexMessage_new_login_%d_%d_%d
     Timeout: 86400
+    CacheUpdateKey: login
+    CacheUpdateTimeout: 900  #
   Count: # 用户有没有推送数据缓存使用的key
     Key: push_count_%s_%s
     Timeout: 7200

+ 9 - 7
jyBXBase/rpc/internal/config/config.go

@@ -18,15 +18,17 @@ type Config struct {
 	NewsLimitNum          int64    // 移动端首页最新标讯数量限制
 	NewsTimeOut           int64    //更新数据时间
 	NewsCache             struct { // 最新标讯缓存配置
-		NoLogin   cacheConfig // 未登录用户缓存key
-		LoginUser cacheConfig // 登录用户有推送数据使用的key
-		Login     cacheConfig // 登录用户没有推送数据最新标讯使用的key
-		Count     cacheConfig // 查询用户有没有推送数据使用的key
+		NoLogin   CacheConfig // 未登录用户缓存key
+		LoginUser CacheConfig // 登录用户有推送数据使用的key
+		Login     CacheConfig // 登录用户没有推送数据最新标讯使用的key
+		Count     CacheConfig // 查询用户有没有推送数据使用的key
 	}
 }
-type cacheConfig struct {
-	Key     string // 缓存key
-	Timeout int    // 缓存时间
+type CacheConfig struct {
+	Key                string `json:"Key,optional"`                // 缓存key
+	Timeout            int    `json:"Timeout,optional"`            // 缓存时间
+	CacheUpdateKey     string `json:"CacheUpdateKey,optional"`     //
+	CacheUpdateTimeout int    `json:"CacheUpdateTimeout,optional"` //
 
 }
 type Db struct {

+ 17 - 61
jyBXBase/rpc/internal/logic/newestbiddinglogic.go

@@ -1,12 +1,9 @@
 package logic
 
 import (
-	"app.yhyue.com/moapp/jybase/redis"
 	"context"
-	IC "jyBXBase/rpc/init"
 	"jyBXBase/rpc/model"
 	"log"
-	"sort"
 	"time"
 
 	"jyBXBase/rpc/internal/svc"
@@ -29,18 +26,11 @@ 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, n := func(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, *newSet) {
+	r, n := func(in *bxbase.NewestBiddingReq) (*bxbase.NewsetBiddingResp, *model.NewSet) {
 		var res = &bxbase.NewsetBiddingResp{
 			Data: &bxbase.NewsetBidding{
 				List: []*bxbase.NewestList{},
@@ -57,35 +47,35 @@ func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase
 			subtype := `"预告","公告","结果","其它"`
 			query := model.NewestQuery("", "", subtype)
 			res.Data.ShowTip = 1
-			redisKey, _ := model.GetRedisKeyTimeout(model.StatusNoLogin, in.PositionId)
-			list, err := model.GetNewsCache(redisKey)
+			redisKeyModel := model.GetRedisKeyTimeout(model.StatusNoLogin, in.PositionId)
+			list, err := model.GetNewsCache(redisKeyModel)
 			if err == nil && list != nil && len(list) > 0 { // 缓存有数据可以直接返回
 				res.Data.List = list
 				res.Data.Count = int64(len(list))
-				return res, &newSet{model.StatusCache, redisKey, model.StatusNoLogin, query}
+				return res, &model.NewSet{Status: model.StatusCache, RedisKeyModel: redisKeyModel, RedisStatus: model.StatusNoLogin, Query: query}
 			}
 			noLoginTime := time.Now()
 			list = model.NewestES(query)
 			res.Data.List = list
 			res.Data.Count = int64(len(list))
 			log.Println("未登录获es取数据耗时:", time.Since(noLoginTime).Seconds())
-			return res, &newSet{model.StatusNoLogin, redisKey, model.StatusNoLogin, query}
+			return res, &model.NewSet{Status: model.StatusNoLogin, RedisKeyModel: redisKeyModel, RedisStatus: model.StatusNoLogin, Query: query}
 		}
-
 		// 登录用户
 		roleNewestInfo, flag := model.GetRoleNewestInfoService(in.AppId, in.MgoUserId, in.NewUserId, in.AccountId, in.EntId, in.EntUserId, in.PositionType, in.PositionId)
+		roleNewestInfo.IsEnt = in.PositionType > 0 //是否是企业
 		res.Data.SubFlag = flag
 		// 查count判断
 		existData := roleNewestInfo.GetPushHistoryCount()
 		if existData > 0 { // 存在推送 查推送缓存  缓存没有数据则查 推送数据
 			res.Data.ShowTip = 1 // count>0说明存在数据不用显示引导用户设置关键词
 			// 查缓存
-			redisKey, _ := model.GetRedisKeyTimeout(model.StatusLoginUser, in.PositionId)
-			list, err := model.GetNewsCache(redisKey)
+			redisKeyModel := model.GetRedisKeyTimeout(model.StatusLoginUser, in.PositionId)
+			list, err := model.GetNewsCache(redisKeyModel)
 			if err == nil && list != nil && len(list) > 0 {
 				res.Data.List = list
 				res.Data.Count = int64(len(list))
-				return res, &newSet{model.StatusCache, redisKey, model.StatusLoginUser, ""}
+				return res, &model.NewSet{Status: model.StatusCache, RedisKeyModel: redisKeyModel, RedisStatus: model.StatusLoginUser}
 			}
 			//  查推送
 			subscribeTime := time.Now()
@@ -94,7 +84,7 @@ func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase
 			if list != nil && len(list) > 0 {
 				res.Data.List = list
 				res.Data.Count = int64(len(list))
-				return res, &newSet{model.StatusLoginUser, redisKey, model.StatusLoginUser, ""}
+				return res, &model.NewSet{Status: model.StatusLoginUser, RedisKeyModel: redisKeyModel, RedisStatus: model.StatusLoginUser}
 			}
 		}
 		// 等0则说明推送里面没有数据 所以去查最新标讯信息缓存 返回  缓存没有则查最新标讯信息 存到缓存里面
@@ -102,62 +92,28 @@ func (l *NewestBiddingLogic) NewestBidding(in *bxbase.NewestBiddingReq) (*bxbase
 		//  查最新标讯
 		query := model.NewestQuery("", "", "")
 		// 查缓存
-		redisKey, _ := model.GetRedisKeyTimeout(model.StatusLogin, in.PositionId)
-		list, err := model.GetNewsCache(redisKey)
+		redisKeyModel := model.GetRedisKeyTimeout(model.StatusLogin, in.PositionId)
+		list, err := model.GetNewsCache(redisKeyModel)
 		if err == nil && list != nil && len(list) > 0 {
 			res.Data.List = list
 			res.Data.Count = int64(len(list))
-			return res, &newSet{model.StatusCache, redisKey, model.StatusLogin, query}
+			return res, &model.NewSet{Status: model.StatusCache, RedisKeyModel: redisKeyModel, RedisStatus: model.StatusLogin, Query: query}
 		}
 		esTime := time.Now()
 		list = model.NewestES(query)
 		log.Println(in.PositionId, "---获取es数据耗时:", time.Since(esTime).Seconds())
 		res.Data.List = list
 		res.Data.Count = int64(len(list))
-		return res, &newSet{model.StatusLogin, redisKey, model.StatusLogin, query}
+		return res, &model.NewSet{Status: model.StatusLogin, RedisKeyModel: redisKeyModel, RedisStatus: model.StatusLogin, Query: query}
 	}(in)
 	if r.Data.Count == 0 {
 		return r, nil
 	}
-	// status : 0 -拿到的是缓存 不用再处理也不用存缓存 1-存到未登录用户 2-存到用户自己的key 3-存到登录用户最新标讯
-	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, positionId)
-		go model.PutNewsCache(redisKey, timeout, r.Data.List)
-	}
-	if n.status != model.StatusCache {
-		go sortRedis(r, n.status, in.PositionId)
+	if n.Status != model.StatusCache {
+		model.DataSortInRedis(r, n.Status, in.PositionId)
 	} else {
-		//获取最新数据 --  延长缓存时间
-		go func(n *newSet, in *bxbase.NewestBiddingReq) {
-			if n.redisKey != "" {
-				//剩余时间 在 IC.C.NewsTimeOut 之内
-				ttl := redis.GetTTL("new", n.redisKey)
-				log.Println(in.PositionId, "---缓存剩余时间:", ttl)
-				if 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)
+		go model.ExtendNewListCache(n, in, r.Data.List)
 	}
-
 	model.MakeCollection(in.UserId, r.Data.List)
 	log.Println(in.PositionId, "--接口耗时:", time.Since(t).Seconds())
 	return r, nil

+ 106 - 13
jyBXBase/rpc/model/newestBidding.go

@@ -6,7 +6,9 @@ import (
 	"fmt"
 	"jyBXBase/rpc/bxbase"
 	IC "jyBXBase/rpc/init"
+	"jyBXBase/rpc/internal/config"
 	"log"
+	"sort"
 	"strings"
 	"time"
 
@@ -31,10 +33,10 @@ const (
 	query_city_hkeys    = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match":%d}},"highlight": {"pre_tags": ["<a>"],"post_tags": ["</a>"],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":%d}`
 	Pushbidding         = "global_common_data.dws_f_bid_baseinfo"
 	Province            = "province"
-	StatusNoLogin       = 1 // 1-未登录用户
-	StatusLoginUser     = 2 // 2-用户自己的key
-	StatusLogin         = 3 // 3-登录用户最新标讯
-	StatusCache         = 0 //   0 -不存缓存 本来查的就是缓存的数据
+	StatusCache         = iota //   0 -不存缓存 本来查的就是缓存的数据
+	StatusNoLogin              // 1-未登录用户
+	StatusLoginUser            // 2-用户自己的key
+	StatusLogin                // 3-登录用户最新标讯
 )
 
 type NewestInfo struct {
@@ -44,6 +46,7 @@ type NewestInfo struct {
 	NewUserId    int64
 	PushMysql    *mysql.Mysql
 	NewsLimitNum int64 // 最新标讯数量条数限制
+	IsEnt        bool
 }
 
 var mysqlTables = map[string]string{
@@ -97,15 +100,22 @@ func (n *NewestInfo) GetPushHistoryCount() int64 {
 	timeOut := IC.C.NewsCache.Count.Timeout
 	c := n.MysqlDb.CountBySql(countSql)
 	if c > 0 {
-		timeOut = timeOut * 4
+		timeOut = timeOut * 7
 	} else {
 		c = -1
 	}
-	redis.Put("new", countKey, c, IC.C.NewsCache.Count.Timeout) //过期时间走配置,比最新标讯列表缓存时间 要短1/10 尽量不超过两分钟
+	redis.Put("new", countKey, c, timeOut) //过期时间走配置,比最新标讯列表缓存时间 要短1/10 尽量不超过两分钟
 	return c
 }
 
+// 判断当前用户是否有最新推送信息
+func (n *NewestInfo) IsHasNewPushData(time int64) bool {
+	return n.MysqlDb.CountBySql(fmt.Sprintf("SELECT COUNT(1) FROM (SELECT 1 FROM %s WHERE userid = %d AND date > %d LIMIT 1) AS ph", n.TableName, n.NewUserId, time)) > 0
+}
+
+// GetPushHistory
 func (n *NewestInfo) GetPushHistory() (res []*bxbase.NewestList) {
+	//
 	findSQL := "select a.infoid,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.attachment_count,a.budget,a.bidamount from %s  a  where a.userid=%d   order by a.id desc limit ?"
 	findSQL = fmt.Sprintf(findSQL, n.TableName, n.NewUserId)
 	logx.Info(n.TableName, "-------", n.NewUserId, ",findSQL:", findSQL)
@@ -264,22 +274,37 @@ func NewestES(doSearchStr string) (res []*bxbase.NewestList) {
 }
 
 // GetRedisKeyTimeout 获取缓存的key 和超时时间
-func GetRedisKeyTimeout(status int, positionId int64) (redisKey string, timeout int) {
+func GetRedisKeyTimeout(status int, positionId int64) config.CacheConfig {
+	var (
+		redisKey, cuKey    string
+		timeOut, cuTimeOut int
+	)
 	switch status {
 	case StatusNoLogin:
 		redisKey = IC.C.NewsCache.NoLogin.Key
 		redisKey = fmt.Sprintf(redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
-		timeout = IC.C.NewsCache.NoLogin.Timeout
+		timeOut = IC.C.NewsCache.NoLogin.Timeout
+		cuKey = fmt.Sprintf("%s_%s", redisKey, IC.C.NewsCache.NoLogin.CacheUpdateKey)
+		cuTimeOut = IC.C.NewsCache.NoLogin.CacheUpdateTimeout
 	case StatusLoginUser:
 		redisKey = IC.C.NewsCache.LoginUser.Key // 登录用户使用的缓存key
 		redisKey = fmt.Sprintf(redisKey, positionId, time.Now().Year(), time.Now().Month(), time.Now().Day())
-		timeout = IC.C.NewsCache.LoginUser.Timeout
+		timeOut = IC.C.NewsCache.LoginUser.Timeout
+		cuKey = fmt.Sprintf("%s_%s", redisKey, IC.C.NewsCache.LoginUser.CacheUpdateKey)
+		cuTimeOut = IC.C.NewsCache.LoginUser.CacheUpdateTimeout
 	case StatusLogin:
 		redisKey = IC.C.NewsCache.Login.Key // 登录用户使用的最新标讯缓存key
 		redisKey = fmt.Sprintf(redisKey, time.Now().Year(), time.Now().Month(), time.Now().Day())
-		timeout = IC.C.NewsCache.Login.Timeout
+		timeOut = IC.C.NewsCache.Login.Timeout
+		cuKey = fmt.Sprintf("%s_%s", redisKey, IC.C.NewsCache.Login.CacheUpdateKey)
+		cuTimeOut = IC.C.NewsCache.Login.CacheUpdateTimeout
+	}
+	return config.CacheConfig{
+		Key:                redisKey,
+		Timeout:            timeOut,
+		CacheUpdateKey:     cuKey,
+		CacheUpdateTimeout: cuTimeOut,
 	}
-	return
 }
 
 // PutNewsCache 存缓存
@@ -296,8 +321,8 @@ func PutNewsCache(redisKey string, redisTimeout int, list []*bxbase.NewestList)
 }
 
 // GetNewsCache 取缓存
-func GetNewsCache(redisKey string) (list []*bxbase.NewestList, err error) {
-	redisByte, err := redis.GetBytes("new", redisKey)
+func GetNewsCache(cc config.CacheConfig) (list []*bxbase.NewestList, err error) {
+	redisByte, err := redis.GetBytes("new", cc.Key)
 	if err != nil || redisByte == nil || len(*redisByte) == 0 {
 		return list, err
 	}
@@ -308,3 +333,71 @@ func GetNewsCache(redisKey string) (list []*bxbase.NewestList, err error) {
 	}
 	return list, nil
 }
+
+type NewSet struct {
+	Status        int
+	RedisKeyModel config.CacheConfig
+	RedisStatus   int
+	Query         string
+}
+
+// 排序 入缓存
+// status : 0 -拿到的是缓存 不用再处理也不用存缓存 1-存到未登录用户 2-存到用户自己的key 3-存到登录用户最新标讯
+func DataSortInRedis(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
+	})
+	redisKeyModel := GetRedisKeyTimeout(status, positionId)
+	go PutNewsCache(redisKeyModel.Key, redisKeyModel.Timeout, r.Data.List)
+}
+
+// 延长缓存
+// 获取最新数据 --  延长缓存时间7天---待调整: 1、判断是否需要更新;2、判断订阅信息是否存在,如果存在是否是最新推送,3、订阅信息查推送缓存(只有个人版有缓存:pushcache_2_a )
+func ExtendNewListCache(n *NewSet, in *bxbase.NewestBiddingReq, list []*bxbase.NewestList) {
+	if n.RedisKeyModel.Key != "" {
+		now := time.Now()
+		if n.RedisKeyModel.CacheUpdateKey != "" {
+			var (
+				res = &bxbase.NewsetBiddingResp{
+					Data: &bxbase.NewsetBidding{
+						List: []*bxbase.NewestList{},
+					},
+				}
+				updateTime = redis.GetInt("new", n.RedisKeyModel.CacheUpdateKey)
+			)
+			switch n.RedisStatus {
+			case StatusLoginUser:
+				//十五分钟内 更新过一次 不再更新
+				if int(now.Unix())-updateTime > n.RedisKeyModel.CacheUpdateTimeout {
+					// 登录用户
+					roleNewestInfo, _ := GetRoleNewestInfoService(in.AppId, in.MgoUserId, in.NewUserId, in.AccountId, in.EntId, in.EntUserId, in.PositionType, in.PositionId)
+					//当前用户有最新推送信息
+					lastPublishTime := list[len(list)-1].PublishTime
+					if roleNewestInfo.IsHasNewPushData(lastPublishTime) {
+						//  查推送
+						subscribeTime := time.Now()
+						res.Data.List = roleNewestInfo.GetPushHistory()
+						log.Println(in.PositionId, "获取订阅数据 存缓存 耗时:", time.Since(subscribeTime).Seconds())
+					} else {
+						res.Data.List = list
+					}
+				}
+			default:
+				//十五分钟内 更新过一次 不再更新
+				if int(now.Unix())-updateTime > n.RedisKeyModel.CacheUpdateTimeout {
+					res.Data.List = NewestES(n.Query)
+				}
+			}
+			if len(res.Data.List) > 0 {
+				//更新update time
+				redis.Put("new", n.RedisKeyModel.CacheUpdateKey, now.Unix(), n.RedisKeyModel.CacheUpdateTimeout)
+				go DataSortInRedis(res, n.RedisStatus, in.PositionId)
+			}
+		}
+		////剩余时间 在 IC.C.NewsTimeOut 之内
+		//if ttl := redis.GetTTL("new", n.RedisKeyModel.Key); ttl-IC.C.NewsTimeOut < 0 {
+		//
+		//}
+	}
+}