Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14de836089 | ||
|
|
1c4f804738 |
1
go.mod
1
go.mod
@@ -28,6 +28,7 @@ require (
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect
|
||||
github.com/emersion/go-imap/v2 v2.0.0-beta.5 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -77,6 +77,8 @@ github.com/elastic/elastic-transport-go/v8 v8.6.1 h1:h2jQRqH6eLGiBSN4eZbQnJLtL4b
|
||||
github.com/elastic/elastic-transport-go/v8 v8.6.1/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.17.1 h1:bOXChDoCMB4TIwwGqKd031U8OXssmWLT3UrAr9EGs3Q=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.17.1/go.mod h1:MVJCtL+gJJ7x5jFeUmA20O7rvipX8GcQmo5iBcmaJn4=
|
||||
github.com/emersion/go-imap/v2 v2.0.0-beta.5 h1:H3858DNmBuXyMK1++YrQIRdpKE1MwBc+ywBtg3n+0wA=
|
||||
github.com/emersion/go-imap/v2 v2.0.0-beta.5/go.mod h1:BZTFHsS1hmgBkFlHqbxGLXk2hnRqTItUgwjSSCsYNAk=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"github.com/ayflying/utility_go/internal/model/do"
|
||||
"github.com/ayflying/utility_go/internal/model/entity"
|
||||
"github.com/ayflying/utility_go/package/aycache"
|
||||
"github.com/ayflying/utility_go/pgk"
|
||||
"github.com/ayflying/utility_go/pkg"
|
||||
service2 "github.com/ayflying/utility_go/service"
|
||||
@@ -72,7 +71,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*3)
|
||||
pkg.Cache("redis").Set(ctx, CacheKey, uid, time.Hour*24*1)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -138,29 +137,22 @@ func (s *sGameAct) Save(actId int) (err error) {
|
||||
}
|
||||
|
||||
cacheGet, _ := g.Redis().Get(ctx, cacheKey)
|
||||
//最后删除key
|
||||
delKey = append(delKey, cacheKey)
|
||||
|
||||
if uid == 0 {
|
||||
//跳过为空的用户缓存
|
||||
continue
|
||||
}
|
||||
if cacheGet.IsEmpty() {
|
||||
//空数据也不巴保存
|
||||
//空数据也不保存
|
||||
continue
|
||||
}
|
||||
|
||||
var CacheKey = fmt.Sprintf("act:update:%d", uid)
|
||||
//如果有活跃,跳过持久化
|
||||
if getBool, _ := aycache.New("redis").Contains(ctx, CacheKey); getBool {
|
||||
if getBool, _ := pkg.Cache("redis").Contains(ctx,
|
||||
fmt.Sprintf("act:update:%d", uid)); getBool {
|
||||
continue
|
||||
}
|
||||
|
||||
////如果1天没有活跃,跳过
|
||||
//user, _ := service.MemberUser().Info(uid)
|
||||
//if user.UpdatedAt.Seconds < gtime.Now().Add(consts.ActSaveTime).Unix() {
|
||||
// continue
|
||||
//}
|
||||
|
||||
//获取数据库数据
|
||||
var data *entity.GameAct
|
||||
// 从数据库中查询活动信息
|
||||
@@ -185,7 +177,8 @@ func (s *sGameAct) Save(actId int) (err error) {
|
||||
data.Action = actionData
|
||||
add = append(add, data)
|
||||
}
|
||||
|
||||
//最后删除key
|
||||
delKey = append(delKey, cacheKey)
|
||||
}
|
||||
|
||||
//批量写入数据库
|
||||
|
||||
125
internal/logic/gameKv/gameKv.go
Normal file
125
internal/logic/gameKv/gameKv.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package gameKv
|
||||
|
||||
import (
|
||||
"github.com/ayflying/utility_go/service"
|
||||
"github.com/ayflying/utility_go/tools"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
Name = "game_kv"
|
||||
)
|
||||
|
||||
type sGameKv struct {
|
||||
Lock sync.Mutex
|
||||
}
|
||||
|
||||
func New() *sGameKv {
|
||||
return &sGameKv{}
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterGameKv(New())
|
||||
|
||||
//支付钩子
|
||||
//task.Task.Trigger(tasks.TaskType_PAY, service.GameKv().HookPay)
|
||||
//task.Task.Trigger(tasks.TaskType_WARDROBE_LEVEL, service.GameKv().HookLevelRwd)
|
||||
}
|
||||
|
||||
// SavesV1 方法
|
||||
//
|
||||
// @Description: 保存用户KV数据列表。
|
||||
// @receiver s: sGameKv的实例。
|
||||
// @return err: 错误信息,如果操作成功,则为nil。
|
||||
func (s *sGameKv) SavesV1() (err error) {
|
||||
// 从Redis列表中获取所有用户KV索引的键
|
||||
//keys, err := utils.RedisScan("user:kv:*")
|
||||
err = tools.Redis.RedisScanV2("user:kv:*", func(keys []string) (err error) {
|
||||
// 定义用于存储用户数据的结构体
|
||||
type ListData struct {
|
||||
Uid int64 `json:"uid"`
|
||||
Kv interface{} `json:"kv"`
|
||||
}
|
||||
var list []*ListData
|
||||
// 初始化列表,长度与keys数组一致
|
||||
list = make([]*ListData, 0)
|
||||
//需要删除的key
|
||||
var delKey []string
|
||||
// 遍历keys,获取每个用户的数据并填充到list中
|
||||
for _, cacheKey := range keys {
|
||||
//g.Log().Infof(ctx, "保存用户kv数据%v", v)
|
||||
//uid := v.Int64()
|
||||
//cacheKey = "user:kv:" + strconv.FormatInt(uid, 10)
|
||||
result := strings.Split(cacheKey, ":")
|
||||
var uid int64
|
||||
uid, err = strconv.ParseInt(result[2], 10, 64)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
g.Redis().Del(ctx, cacheKey)
|
||||
continue
|
||||
}
|
||||
|
||||
////如果1天没有活跃,跳过
|
||||
//user, _ := service.MemberUser().Info(uid)
|
||||
//if user.UpdatedAt.Seconds < gtime.Now().Add(consts.ActSaveTime).Unix() {
|
||||
// continue
|
||||
//}
|
||||
|
||||
get, _ := g.Redis().Get(ctx, cacheKey)
|
||||
var data interface{}
|
||||
get.Scan(&data)
|
||||
list = append(list, &ListData{
|
||||
Uid: int64(uid),
|
||||
Kv: data,
|
||||
})
|
||||
|
||||
delKey = append(delKey, cacheKey)
|
||||
}
|
||||
|
||||
// 将列表数据保存到数据库
|
||||
if len(list) > 0 {
|
||||
_, err2 := g.Model("game_kv").Batch(30).Data(list).Save()
|
||||
list = make([]*ListData, 0)
|
||||
if err2 != nil {
|
||||
g.Log().Error(ctx, err2)
|
||||
return
|
||||
}
|
||||
|
||||
//批量删除key
|
||||
for _, v := range delKey {
|
||||
_, err2 = g.Redis().Del(ctx, v)
|
||||
if err2 != nil {
|
||||
g.Log().Errorf(ctx, "删除存档错误:%v,err=%v", v, err2)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
delKey = make([]string, 0)
|
||||
|
||||
}
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, "当前kv数据入库失败: %v", err)
|
||||
}
|
||||
|
||||
return
|
||||
})
|
||||
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
////跳过
|
||||
//if len(keys) == 0 {
|
||||
// return
|
||||
//}
|
||||
////一次最多处理10w条
|
||||
//if len(keys) > 10000 {
|
||||
// keys = keys[:10000]
|
||||
//}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -6,6 +6,7 @@ package logic
|
||||
|
||||
import (
|
||||
_ "github.com/ayflying/utility_go/internal/logic/gameAct"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/gameKv"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/ip2region"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/logData"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/systemCron"
|
||||
|
||||
32
service/game_kv.go
Normal file
32
service/game_kv.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// ================================================================================
|
||||
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||
// You can delete these comments if you wish manually maintain this interface file.
|
||||
// ================================================================================
|
||||
|
||||
package service
|
||||
|
||||
type (
|
||||
IGameKv interface {
|
||||
// SavesV1 方法
|
||||
//
|
||||
// @Description: 保存用户KV数据列表。
|
||||
// @receiver s: sGameKv的实例。
|
||||
// @return err: 错误信息,如果操作成功,则为nil。
|
||||
SavesV1() (err error)
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
localGameKv IGameKv
|
||||
)
|
||||
|
||||
func GameKv() IGameKv {
|
||||
if localGameKv == nil {
|
||||
panic("implement not found for interface IGameKv, forgot register?")
|
||||
}
|
||||
return localGameKv
|
||||
}
|
||||
|
||||
func RegisterGameKv(i IGameKv) {
|
||||
localGameKv = i
|
||||
}
|
||||
@@ -15,7 +15,7 @@ type randMod struct {
|
||||
// RandomAll 按权重随机选取 N 个不重复的元素
|
||||
func (m *randMod) RandomAll(data map[int]int, n int) []int {
|
||||
if n > len(data) {
|
||||
return nil
|
||||
n = len(data)
|
||||
}
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
// 复制权重映射,避免修改原始数据
|
||||
|
||||
Reference in New Issue
Block a user