Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
239115ead8 | ||
|
|
02b3e275d0 | ||
|
|
9810e55a15 | ||
|
|
03e4ad5db5 | ||
|
|
eb94e50f02 | ||
|
|
26444905cf | ||
|
|
d82b12ddaf | ||
|
|
b9a7970699 | ||
|
|
6f25e7baf3 | ||
| 513ed653e2 | |||
| 31cd9896b6 | |||
| 9c99508bdd | |||
|
|
f3e1ad74b5 |
6
go.mod
6
go.mod
@@ -12,8 +12,8 @@ require (
|
||||
github.com/go-pay/gopay v1.5.114
|
||||
github.com/go-pay/util v0.0.4
|
||||
github.com/goccy/go-json v0.10.5
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.3
|
||||
github.com/gogf/gf/v2 v2.9.3
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.4
|
||||
github.com/gogf/gf/v2 v2.9.4
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20250916043522-9a14e3273609
|
||||
github.com/minio/minio-go/v7 v7.0.95
|
||||
@@ -72,7 +72,7 @@ require (
|
||||
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
|
||||
github.com/olekukonko/errors v1.1.0 // indirect
|
||||
github.com/olekukonko/ll v0.1.1 // indirect
|
||||
github.com/olekukonko/tablewriter v1.0.9 // indirect
|
||||
github.com/olekukonko/tablewriter v1.1.0 // indirect
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
|
||||
12
go.sum
12
go.sum
@@ -151,10 +151,10 @@ github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlnd
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.3 h1:/IcE+DmlQDWeDjnkKWr1Rql7VCLUxEqz7gXoQKZHd8o=
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.3/go.mod h1:mek4CnNsvdum4jYhf0PzaIYzoSBuSvVXyuCZG4I9qvw=
|
||||
github.com/gogf/gf/v2 v2.9.3 h1:qjN4s55FfUzxZ1AE8vUHNDX3V0eIOUGXhF2DjRTVZQ4=
|
||||
github.com/gogf/gf/v2 v2.9.3/go.mod h1:w6rcfD13SmO7FKI80k9LSLiSMGqpMYp50Nfkrrc2sEE=
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.4 h1:l2vkPpqteotzpq0h5Qg6S/GK84WpPUZlRwPVfZSDbYs=
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.9.4/go.mod h1:mTfSf2Zm8+C49+rM7pK6AC8cwDSDxxwO8sLMNOl0yCI=
|
||||
github.com/gogf/gf/v2 v2.9.4 h1:6vleEWypot9WBPncP2GjbpgAUeG6Mzb1YESb9nPMkjY=
|
||||
github.com/gogf/gf/v2 v2.9.4/go.mod h1:Ukl+5HUH9S7puBmNLR4L1zUqeRwi0nrW4OigOknEztU=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
@@ -321,8 +321,8 @@ github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5
|
||||
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
|
||||
github.com/olekukonko/ll v0.1.1 h1:9Dfeed5/Mgaxb9lHRAftLK9pVfYETvHn+If6lywVhJc=
|
||||
github.com/olekukonko/ll v0.1.1/go.mod h1:2dJo+hYZcJMLMbKwHEWvxCUbAOLc/CXWS9noET22Mdo=
|
||||
github.com/olekukonko/tablewriter v1.0.9 h1:XGwRsYLC2bY7bNd93Dk51bcPZksWZmLYuaTHR0FqfL8=
|
||||
github.com/olekukonko/tablewriter v1.0.9/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
|
||||
github.com/olekukonko/tablewriter v1.1.0 h1:N0LHrshF4T39KvI96fn6GT8HEjXRXYNDrDjKFDB7RIY=
|
||||
github.com/olekukonko/tablewriter v1.1.0/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
|
||||
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
|
||||
@@ -26,6 +26,7 @@ var (
|
||||
Name = "game_act"
|
||||
ActList = gset.New(true)
|
||||
RunTimeMax *gtime.Time
|
||||
TaskMax int64 = 5
|
||||
)
|
||||
|
||||
type sGameAct struct {
|
||||
@@ -77,7 +78,7 @@ func (s *sGameAct) Info(uid int64, actId int) (data *g.Var, err error) {
|
||||
_, err = g.Redis().Set(ctx, keyCache, data)
|
||||
|
||||
var CacheKey = fmt.Sprintf("act:update:%d", uid)
|
||||
pkg.Cache("redis").Set(ctx, CacheKey, uid, time.Hour*24*1)
|
||||
pkg.Cache("redis").Set(ctx, CacheKey, uid, time.Hour*24*3+time.Hour*12)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -236,7 +237,7 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
|
||||
}
|
||||
|
||||
//删除缓存
|
||||
go s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
//s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
|
||||
updateCount++
|
||||
update = make([]*entity.GameAct, 0)
|
||||
@@ -264,10 +265,10 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, v2 := range add {
|
||||
//删除缓存
|
||||
go s.DelCacheKey(ctx, v2.ActId, v2.Uid)
|
||||
}
|
||||
//for _, v2 := range add {
|
||||
// //删除缓存
|
||||
// s.DelCacheKey(ctx, v2.ActId, v2.Uid)
|
||||
//}
|
||||
|
||||
//g.Log().Debugf(ctx, "当前 %v 写入数据库: %v 条", actId, count)
|
||||
add = make([]*entity.GameAct, 0)
|
||||
@@ -311,7 +312,7 @@ func (s *sGameAct) SavesV2() (err error) {
|
||||
go func() {
|
||||
scanErr := tools.Redis.RedisScanV2("act:*", func(keys []string) error {
|
||||
if gtime.Now().After(RunTimeMax) {
|
||||
return errors.New("Redis扫描超时")
|
||||
return errors.New("redis扫描超时")
|
||||
}
|
||||
for _, key := range keys {
|
||||
if keyErr := s.SaveV2(ctx, key, addChan, updateChan); keyErr != nil {
|
||||
@@ -421,12 +422,18 @@ func (s *sGameAct) SaveV2(ctx context.Context, cacheKey string, addChan, updateC
|
||||
// @param update []*entity.GameAct: 更新数据
|
||||
// @return err error: 返回错误信息
|
||||
func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct) {
|
||||
tx, err := g.DB().Begin(ctx)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
//批量写入数据库
|
||||
updateCount := 0
|
||||
var updateCount int64
|
||||
if len(update) > 0 {
|
||||
for _, v := range update {
|
||||
v.UpdatedAt = gtime.Now()
|
||||
updateRes, err2 := g.Model(Name).Where(do.GameAct{
|
||||
updateRes, err2 := tx.Model(Name).Where(do.GameAct{
|
||||
Uid: v.Uid,
|
||||
ActId: v.ActId,
|
||||
}).Data(v).Update()
|
||||
@@ -438,10 +445,22 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
|
||||
g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v)
|
||||
continue
|
||||
}
|
||||
//删除缓存
|
||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
|
||||
updateCount++
|
||||
if updateCount > TaskMax {
|
||||
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
g.Log().Debugf(ctx, "act当前更新数据库失败:%v", err)
|
||||
return
|
||||
}
|
||||
updateCount = 0
|
||||
tx, err = g.DB().Begin(ctx)
|
||||
}
|
||||
//删除缓存
|
||||
//s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
}
|
||||
//循环结束了,最后写入一波
|
||||
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
|
||||
update = (update)[:0]
|
||||
}
|
||||
@@ -449,7 +468,7 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
|
||||
var addCount int64
|
||||
if len(add) > 0 {
|
||||
for _, v := range add {
|
||||
addRes, err2 := g.Model(Name).Data(v).Insert()
|
||||
addRes, err2 := tx.Model(Name).Data(v).Insert()
|
||||
if err2 != nil {
|
||||
g.Log().Error(ctx, err2)
|
||||
continue
|
||||
@@ -459,12 +478,26 @@ func (s *sGameAct) Cache2Sql(ctx context.Context, add, update []*entity.GameAct)
|
||||
continue
|
||||
}
|
||||
addCount++
|
||||
if addCount > TaskMax {
|
||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err)
|
||||
return
|
||||
}
|
||||
addCount = 0
|
||||
tx, err = g.DB().Begin(ctx)
|
||||
}
|
||||
//删除缓存
|
||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
//s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
}
|
||||
|
||||
//循环结束了,最后写入一波
|
||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
||||
add = (add)[:0]
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -478,8 +511,14 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
||||
addClosed := false
|
||||
updateClosed := false
|
||||
|
||||
tx, err := g.DB().Begin(ctx)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 使用链式安全模式
|
||||
var db = g.Model(Name).Safe()
|
||||
//var db = tx.Model(Name).Safe()
|
||||
|
||||
for {
|
||||
//检查是否两个通道都已关闭且为空
|
||||
@@ -493,7 +532,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
||||
addClosed = true // 仅标记关闭,不立即日志
|
||||
continue
|
||||
}
|
||||
addRes, err2 := db.Data(v).Insert()
|
||||
addRes, err2 := tx.Model(Name).Data(v).Insert()
|
||||
if err2 != nil {
|
||||
g.Log().Error(ctx, err2)
|
||||
continue
|
||||
@@ -504,8 +543,20 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
||||
}
|
||||
row, _ := addRes.RowsAffected()
|
||||
addCount += row
|
||||
|
||||
if addCount > TaskMax {
|
||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err)
|
||||
return
|
||||
}
|
||||
addCount = 0
|
||||
tx, err = g.DB().Begin(ctx)
|
||||
}
|
||||
|
||||
//删除缓存
|
||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
//s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
|
||||
case v, ok := <-updateChan:
|
||||
if !ok {
|
||||
@@ -513,7 +564,7 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
||||
continue
|
||||
}
|
||||
v.UpdatedAt = gtime.Now()
|
||||
updateRes, err2 := db.Where(do.GameAct{
|
||||
updateRes, err2 := tx.Model(Name).Where(do.GameAct{
|
||||
Uid: v.Uid,
|
||||
ActId: v.ActId,
|
||||
}).Data(v).Update()
|
||||
@@ -525,16 +576,29 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
||||
//g.Log().Error(ctx, "本次更新为0,更新数据失败: %v", v)
|
||||
continue
|
||||
}
|
||||
//删除缓存
|
||||
s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
updateCount++
|
||||
|
||||
if updateCount > TaskMax {
|
||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
g.Log().Debugf(ctx, "act当前写入数据库失败:%v", err)
|
||||
return
|
||||
}
|
||||
updateCount = 0
|
||||
tx, err = g.DB().Begin(ctx)
|
||||
}
|
||||
|
||||
//删除缓存
|
||||
//s.DelCacheKey(ctx, v.ActId, v.Uid)
|
||||
|
||||
case <-ctx.Done():
|
||||
g.Log().Debug(ctx, "act协程被上下文取消")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
// 仅在所有通道处理完毕后打印最终计数(移除中间冗余日志)
|
||||
g.Log().Debugf(ctx, "act当前写入数据库: %v 条", addCount)
|
||||
g.Log().Debugf(ctx, "act当前更新数据库: %v 条", updateCount)
|
||||
@@ -543,17 +607,19 @@ func (s *sGameAct) Cache2SqlChan(ctx context.Context, addChan, updateChan chan *
|
||||
|
||||
// 删除缓存key
|
||||
func (s *sGameAct) DelCacheKey(ctx context.Context, aid int, uid int64) {
|
||||
//如果有活跃,跳过删除
|
||||
if getBool, _ := pkg.Cache("redis").
|
||||
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
//如果有活跃,跳过删除
|
||||
if getBool, _ := pkg.Cache("redis").
|
||||
Contains(ctx, fmt.Sprintf("act:update:%d", uid)); getBool {
|
||||
return
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("act:%v:%v", aid, uid)
|
||||
_, err := g.Redis().Del(ctx, cacheKey)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
cacheKey := fmt.Sprintf("act:%v:%v", aid, uid)
|
||||
_, err := g.Redis().Del(ctx, cacheKey)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 清空GetRedDot缓存
|
||||
|
||||
@@ -2,6 +2,7 @@ package systemLog
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
v1 "github.com/ayflying/utility_go/api/admin/v1"
|
||||
"github.com/ayflying/utility_go/service"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
@@ -72,6 +72,13 @@ var safePropertyRE = regexp.MustCompile(`[/"'\\\/]`)
|
||||
|
||||
// 设置某些字段只允许包含字母、数字和下划线
|
||||
var onlyWordRE = regexp.MustCompile(`\W`)
|
||||
var nonWordCharRes = regexp.MustCompile(`[^\w]`)
|
||||
|
||||
func hasNonWordChar(s string) bool {
|
||||
// 匹配非 \w 字符的正则表达式
|
||||
return nonWordCharRes.MatchString(s)
|
||||
}
|
||||
|
||||
var onlyWordPropertyNames = map[string]struct{}{
|
||||
"nickname": {},
|
||||
}
|
||||
@@ -83,16 +90,21 @@ func SetOnlyWordProperty(propertyNames ...string) {
|
||||
}
|
||||
|
||||
func safeProperty(property map[string]any) {
|
||||
delkeys := []string{}
|
||||
for k, v := range property {
|
||||
if hasNonWordChar(k) {
|
||||
delkeys = append(delkeys, k)
|
||||
continue
|
||||
}
|
||||
if _, ok := onlyWordPropertyNames[k]; ok {
|
||||
if _, ok := v.(string); ok {
|
||||
property[k] = onlyWordRE.ReplaceAllString(gconv.String(v), "*")
|
||||
}
|
||||
} else {
|
||||
property[k] = safePropertyRE.ReplaceAllString(gconv.String(v), "*")
|
||||
|
||||
}
|
||||
}
|
||||
for _, delkey := range delkeys {
|
||||
delete(property, delkey)
|
||||
}
|
||||
}
|
||||
|
||||
func getLocationMapValue(key string) *time.Location {
|
||||
@@ -390,7 +402,7 @@ func (sdk *SDK) flush() {
|
||||
|
||||
// 发送消息
|
||||
func (sdk *SDK) send(logs []GameLog) {
|
||||
waitSecond := time.Duration(sdk.sdkConfig.FlushInterval/4) * time.Second
|
||||
waitSecond := time.Duration(sdk.sdkConfig.FlushInterval/4)*time.Second + time.Second*time.Duration(gconv.Int(len(logs)/5000))
|
||||
timeoutCtx, cancel := context.WithTimeout(context.Background(), waitSecond)
|
||||
defer cancel()
|
||||
data := make([][]any, 0, len(logs))
|
||||
|
||||
@@ -14,8 +14,9 @@ import (
|
||||
func TestGamelog(t *testing.T) {
|
||||
glsdk, err := gamelog.INIT(&gamelog.SDKConfig{
|
||||
// 必填
|
||||
Pid: "test5", // 项目ID
|
||||
BaseUrl: "http://47.76.178.47:10101", // 香港测试服上报地址
|
||||
Pid: "test5", // 项目ID
|
||||
// BaseUrl: "http://47.76.178.47:10101", // 香港测试服上报地址
|
||||
BaseUrl: "http://101.37.28.111:10101", // 香港测试服上报地址
|
||||
// BaseUrl: "http://127.0.0.1:10101", // 本次测试上报地址
|
||||
ReportSk: "sngame2025", // xor混淆key
|
||||
FlushInterval: 5, // 上报间隔
|
||||
|
||||
@@ -2,9 +2,10 @@ package rank
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
v1 "github.com/ayflying/utility_go/api/pkg/v1"
|
||||
"time"
|
||||
|
||||
v1 "github.com/ayflying/utility_go/api/pkg/v1"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
@@ -98,6 +99,53 @@ func (r *F64CountRank) IncrScore(id int64, score int64) (curScore float64, err e
|
||||
return
|
||||
}
|
||||
|
||||
// SetScore 对指定ID的分数进行赋值,这样同分情况下先完成的在前面。
|
||||
// 该方法首先更新成员的更新时间戳,然后更新成员的分数。
|
||||
//
|
||||
// 参数:
|
||||
//
|
||||
// id - 要操作的成员ID。
|
||||
// score - 要更新的分数。
|
||||
//
|
||||
// 返回值:
|
||||
//
|
||||
// err - 操作过程中可能发生的错误。
|
||||
//
|
||||
// @Description:
|
||||
// @receiver r
|
||||
// @param id
|
||||
// @param score
|
||||
// @return err
|
||||
func (r *F64CountRank) SetScore(id int64, score int) (err error) {
|
||||
//如果分数小于0,则删除
|
||||
if score <= 0 {
|
||||
err = r.DelScore(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
// 记录当前时间戳,用于更新成员的最新活动时间。
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
// 将成员的更新时间戳加入到Redis的有序集合中,确保成员的排序依据是最新的活动时间。
|
||||
_, err = g.Redis().ZAdd(ctx, r.updateTs, &gredis.ZAddOption{}, gredis.ZAddMember{
|
||||
Score: float64(now),
|
||||
Member: id,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 覆盖成员的分数
|
||||
_, err = g.Redis().ZAdd(ctx, r.name, &gredis.ZAddOption{}, gredis.ZAddMember{
|
||||
Score: float64(score) + (3*1e13-float64(now))/1e14,
|
||||
Member: id,
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// todo暂时未使用
|
||||
func (r *F64CountRank) GetCount() {
|
||||
count, _ := g.Redis().ZCard(ctx, r.name)
|
||||
@@ -141,6 +189,35 @@ func (r *F64CountRank) DelScore(id int64) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// DelByStopRank 删除指定名次后的元素
|
||||
func (r *F64CountRank) DelByStopRank(stop int64) (err error) {
|
||||
// 初始化一个空的int64切片,用于存储指定排名范围内的元素。
|
||||
var members []int64
|
||||
|
||||
// 使用Redis的ZRange命令获取指定排名范围内的元素。
|
||||
// 选项Rev设置为true,表示按照分数从高到低的顺序返回元素。
|
||||
get, err := g.Redis().ZRange(ctx, r.name, stop, 9999999,
|
||||
gredis.ZRangeOption{
|
||||
Rev: true,
|
||||
})
|
||||
|
||||
// 使用Scan方法将获取到的元素扫描到members切片中。
|
||||
err = get.Scan(&members)
|
||||
// 如果扫描过程中出现错误,直接返回错误。
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 遍历members切片,对于每个元素,使用ZRem命令从更新时间集合中删除对应的成员。
|
||||
for _, member := range members {
|
||||
_, err = g.Redis().ZRem(ctx, r.updateTs, member)
|
||||
// 忽略ZRem操作的错误,因为即使元素不存在,ZRem也不会返回错误。
|
||||
}
|
||||
//删除超过9999的数据
|
||||
g.Redis().ZRemRangeByRank(ctx, r.name, 0, -(stop + 1))
|
||||
return
|
||||
}
|
||||
|
||||
// DelByRank 根据排名范围删除元素。
|
||||
// 该方法使用了Redis的有序集合数据结构,通过ZRange和ZRemRangeByRank命令来实现。
|
||||
// 参数start和stop定义了要删除的排名范围,从start到stop(包括start和stop)。
|
||||
|
||||
Reference in New Issue
Block a user