浏览代码

add cache for access records

tmp
shenjiachi 3 周前
父节点
当前提交
23e9767746
共有 6 个文件被更改,包括 75 次插入18 次删除
  1. +38
    -17
      app/hdl/comm/hdl_comm.go
  2. +17
    -0
      app/hdl/friend_circle/hdl_friend_circle.go
  3. +3
    -0
      app/hdl/hdl_im.go
  4. +1
    -0
      app/md/app_redis_key.go
  5. +15
    -0
      app/svc/svc_im.go
  6. +1
    -1
      go.mod

+ 38
- 17
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
}



+ 17
- 0
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)
}



+ 3
- 0
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
}

+ 1
- 0
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
)

+ 15
- 0
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)

}

+ 1
- 1
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


正在加载...
取消
保存