From 23e9767746a9e8cc088c63bcc31f55079c49168a Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Sat, 7 Dec 2024 17:58:43 +0800 Subject: [PATCH] add cache for access records --- app/hdl/comm/hdl_comm.go | 55 +++++++++++++++------- app/hdl/friend_circle/hdl_friend_circle.go | 17 +++++++ app/hdl/hdl_im.go | 3 ++ app/md/app_redis_key.go | 1 + app/svc/svc_im.go | 15 ++++++ go.mod | 2 +- 6 files changed, 75 insertions(+), 18 deletions(-) diff --git a/app/hdl/comm/hdl_comm.go b/app/hdl/comm/hdl_comm.go index 2aa86aa..d8b6943 100644 --- a/app/hdl/comm/hdl_comm.go +++ b/app/hdl/comm/hdl_comm.go @@ -3,12 +3,15 @@ package comm import ( "applet/app/db" "applet/app/e" + "applet/app/md" "applet/app/md/comm" "applet/app/svc" + "applet/app/utils" "applet/app/utils/cache" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" + "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/gin-gonic/gin" "time" @@ -103,24 +106,42 @@ func AccessRecords(c *gin.Context) { user := svc.GetUser(c) date := time.Now().Format("2006-01-02") - recordsDb := implement.NewEggAccessRecordsDb(db.Db) - exist, err := recordsDb.EggAccessRecordsExist(user.Id, date, req.Index) - if err != nil { - e.OutErr(c, e.ERR_DB_ORM, nil) - } - if exist { - e.OutSuc(c, "data exist", nil) - return - } - m := model.EggAccessRecords{ - Uid: user.Id, - PageIndex: req.Index, - Date: date, - } - _, err = recordsDb.EggAccessRecordsInsert(&m) - if err != nil { - e.OutErr(c, e.ERR_DB_ORM, nil) + cacheKey := fmt.Sprintf(md.UserAccessRecordsKey, user.Id, date, req.Index) + get, err1 := cache.GetString(cacheKey) + if err1 != nil || get == "" { + recordsDb := implement.NewEggAccessRecordsDb(db.Db) + exist, err := recordsDb.EggAccessRecordsExist(user.Id, date, req.Index) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, nil) + } + if exist { + e.OutSuc(c, "success", nil) + return + } + + m := model.EggAccessRecords{ + Uid: user.Id, + PageIndex: req.Index, + Date: date, + } + _, err = recordsDb.EggAccessRecordsInsert(&m) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, nil) + return + } + ttl := 3 * 60 * 60 // 记录保存三小时 + + _, err = cache.SetEx(cacheKey, "1", ttl) + if err != nil { + fmt.Println("AccessRecordsRedis_ERR:::::", err.Error()) + utils.FilePutContents("AccessRecordsRedis_ERR", utils.SerializeStr(map[string]interface{}{ + "cacheKey": cacheKey, + "err": err.Error(), + })) + } + } else { + e.OutSuc(c, "success", nil) return } diff --git a/app/hdl/friend_circle/hdl_friend_circle.go b/app/hdl/friend_circle/hdl_friend_circle.go index 16c9ccd..1426d18 100644 --- a/app/hdl/friend_circle/hdl_friend_circle.go +++ b/app/hdl/friend_circle/hdl_friend_circle.go @@ -8,6 +8,7 @@ import ( "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" svc3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc" + es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es" "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es" "context" "errors" @@ -112,6 +113,22 @@ func Publish(c *gin.Context) { }) fmt.Printf("CreateDoc ==> %+v \n\n", createDocRet) + // 更新发朋友圈次数 + year, week := time.Now().ISOWeek() + yearStr := utils.IntToStr(year) + weekStr := utils.IntToStr(week) + index := es2.GetAppointIndexFromAlias(yearStr, weekStr) + id := fmt.Sprintf("%d%d-%d", year, week, user.Id) + script := elastic.NewScript("ctx._source.send_circle_of_friend_nums += params.inc").Param("inc", 1) + _, err = es.EsClient.Update(). + Index(index). + Id(id). + Script(script). + Do(context.Background()) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } e.OutSuc(c, "success", nil) } diff --git a/app/hdl/hdl_im.go b/app/hdl/hdl_im.go index 0c7145c..df503c1 100644 --- a/app/hdl/hdl_im.go +++ b/app/hdl/hdl_im.go @@ -227,6 +227,9 @@ func SendRedPackage(c *gin.Context) { RedPackageId: redPackageId, } + // 处理统计发红包数 推到 mq 处理 + go svc.AddSendRedPackageTimesPushToMQ(user.Id) + e.OutSuc(c, resp, nil) return } diff --git a/app/md/app_redis_key.go b/app/md/app_redis_key.go index a1a6003..7340f35 100644 --- a/app/md/app_redis_key.go +++ b/app/md/app_redis_key.go @@ -6,4 +6,5 @@ const ( DealAppLimiterRequestIdPrefix = "%s:egg_app_limiter_request_id:%s" AppLimiterLock = "%s:egg_app_limiter_lock:%s" // 限流器锁 CfgCacheTime = 86400 + UserAccessRecordsKey = "AccessRecords:%d:%s:%s" // 用户访问记录 uid index ) diff --git a/app/svc/svc_im.go b/app/svc/svc_im.go index ce10e55..4e84f1e 100644 --- a/app/svc/svc_im.go +++ b/app/svc/svc_im.go @@ -14,7 +14,9 @@ import ( enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" + md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc" + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "errors" "github.com/google/uuid" "github.com/shopspring/decimal" @@ -377,3 +379,16 @@ func GetImUser(userId int64, phone string) (user *pb.User, err error) { } return resp.User, nil } + +func AddSendRedPackageTimesPushToMQ(uid int64) { + ch, err := rabbit.Cfg.Pool.GetChannel() + if err != nil { + return + } + defer ch.Release() + + ch.Publish(md3.IMEggEnergyExchange, md3.IMEggEnergyStructForSendRedPackageData{ + Uid: uid, + }, md3.IMEggEnergyRoutKeyForSendRedPackage) + +} diff --git a/go.mod b/go.mod index 86b03ae..1791791 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( require ( code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241206115326-8cbc93c7c64d - code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241205061938-91f42710d6cd + code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241207095445-64c8aa0b486e code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible