2025-05-31 23:34:58 +08:00
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"game/common/proto/pb"
|
|
|
|
|
"game/common/serialization"
|
2025-06-02 01:07:35 +08:00
|
|
|
|
"game/common/utils"
|
2025-05-31 23:34:58 +08:00
|
|
|
|
"github.com/fox/fox/log"
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-06-02 01:07:35 +08:00
|
|
|
|
TableExpire = 7 * 24 * time.Hour // 七天后过期
|
2025-05-31 23:34:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type resultT[T any] struct {
|
|
|
|
|
ret T
|
|
|
|
|
//err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type iTable interface {
|
|
|
|
|
GetId() uint
|
2025-06-04 01:56:38 +08:00
|
|
|
|
TableName() string
|
2025-05-31 23:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
T:Table,如果不想操作redis则将rds设置为nil
|
|
|
|
|
*/
|
|
|
|
|
type TableOp[T iTable] struct {
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
rds *redis.Client
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 01:07:35 +08:00
|
|
|
|
func NewTableOp[T iTable](db *gorm.DB, rds *redis.Client) *TableOp[T] {
|
2025-05-31 23:34:58 +08:00
|
|
|
|
return &TableOp[T]{db: db, rds: rds}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) tableName() string {
|
|
|
|
|
var result resultT[T]
|
2025-06-04 01:56:38 +08:00
|
|
|
|
return result.ret.TableName()
|
2025-05-31 23:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) redisKey(id uint) string {
|
|
|
|
|
return fmt.Sprintf("%s:%d", s.tableName(), id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) findByRedis(id uint) *T {
|
|
|
|
|
if s.rds == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
maps, err := s.rds.HGetAll(context.Background(), s.redisKey(id)).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ErrorF("redis-key:%v HGetAll err: %v", s.redisKey(id), err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if len(maps) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-06-04 23:21:21 +08:00
|
|
|
|
us, err := serialization.MapToStruct[T](maps)
|
2025-06-04 02:40:13 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.ErrorF("serialization map to struct err: %v", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-06-05 00:05:09 +08:00
|
|
|
|
//log.DebugF("findByRedis redis-key:%v result:%v", s.redisKey(id), us)
|
|
|
|
|
return us
|
2025-05-31 23:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) writeRedis(id uint, t *T) {
|
|
|
|
|
if s.rds == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
maps := serialization.StructToMap(t)
|
|
|
|
|
if len(maps) == 0 {
|
|
|
|
|
log.ErrorF("table struct is empty:%v", s.tableName())
|
|
|
|
|
}
|
|
|
|
|
s.updateRedis(id, maps)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) updateRedis(id uint, maps map[string]any) {
|
|
|
|
|
if s.rds == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := s.rds.HMSet(context.Background(), s.redisKey(id), maps).Err(); err != nil {
|
|
|
|
|
log.ErrorF("redis-key:%v HMSet err: %v", s.redisKey(id), err)
|
|
|
|
|
}
|
2025-06-02 01:07:35 +08:00
|
|
|
|
_ = s.rds.Expire(context.Background(), s.redisKey(id), TableExpire).Err()
|
2025-05-31 23:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) deleteRedis(id uint) {
|
|
|
|
|
if s.rds == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_ = s.rds.Del(context.Background(), s.redisKey(id)).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) Create(t *T) (*T, pb.ErrCode) {
|
|
|
|
|
if err := s.db.Create(t).Error; err != nil {
|
|
|
|
|
log.ErrorF("create table:%v err:%v", s.tableName(), err)
|
|
|
|
|
return nil, pb.ErrCode_SystemErr
|
|
|
|
|
}
|
2025-06-02 01:07:35 +08:00
|
|
|
|
s.writeRedis((*t).GetId(), t)
|
2025-05-31 23:34:58 +08:00
|
|
|
|
return t, pb.ErrCode_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) Find(id uint) (*T, pb.ErrCode) {
|
|
|
|
|
// 先从redis中查询,redis中没有则从mysql中查询
|
|
|
|
|
if table := s.findByRedis(id); table != nil {
|
|
|
|
|
return table, pb.ErrCode_OK
|
|
|
|
|
}
|
|
|
|
|
var result resultT[T]
|
|
|
|
|
err := s.db.Where("id = ?", id).First(&result.ret).Error
|
|
|
|
|
if err != nil {
|
2025-06-04 01:56:38 +08:00
|
|
|
|
log.DebugF("find table:%v id:%v err:%v", s.tableName(), id, err)
|
2025-05-31 23:34:58 +08:00
|
|
|
|
return nil, pb.ErrCode_SystemErr
|
|
|
|
|
}
|
|
|
|
|
return &result.ret, pb.ErrCode_OK
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 01:07:35 +08:00
|
|
|
|
// 根据条件查询,只在mysql中查询,无法在redis中查询
|
|
|
|
|
func (s *TableOp[T]) FindCondition(condition map[string]any) (*T, error) {
|
|
|
|
|
var result resultT[T]
|
|
|
|
|
err := s.db.Where(condition).First(&result.ret).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ErrorF("find table:%v condition:%v err:%v", s.tableName(), utils.JsonMarshal(condition), err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
// 查看redis中是否存在该键,不存在则写入数据
|
|
|
|
|
exist, _ := s.rds.Exists(context.Background(), s.redisKey(result.ret.GetId())).Result()
|
|
|
|
|
if exist != 1 {
|
|
|
|
|
s.writeRedis(result.ret.GetId(), &result.ret)
|
|
|
|
|
}
|
|
|
|
|
return &result.ret, nil
|
|
|
|
|
}
|
2025-05-31 23:34:58 +08:00
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) Update(id uint, updates map[string]any) (*T, pb.ErrCode) {
|
|
|
|
|
var result resultT[T]
|
|
|
|
|
err := s.db.Model(&result.ret).Where("id = ?", id).Updates(updates).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ErrorF("update table:%v id:%v err:%v", s.tableName(), id, err)
|
|
|
|
|
return nil, pb.ErrCode_SystemErr
|
|
|
|
|
}
|
|
|
|
|
s.updateRedis(id, updates)
|
|
|
|
|
return &result.ret, pb.ErrCode_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *TableOp[T]) Delete(id uint) (*T, pb.ErrCode) {
|
|
|
|
|
var result resultT[T]
|
|
|
|
|
err := s.db.Delete(&result.ret, id).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ErrorF("delete table:%v err:%v", s.tableName(), err)
|
|
|
|
|
return nil, pb.ErrCode_SystemErr
|
|
|
|
|
}
|
|
|
|
|
s.deleteRedis(id)
|
|
|
|
|
return &result.ret, pb.ErrCode_OK
|
|
|
|
|
}
|