添加db系统过期机制

This commit is contained in:
liwei1dao 2022-08-23 18:42:59 +08:00
parent 6bbdda1924
commit 2949f8880f
5 changed files with 204 additions and 72 deletions

View File

@ -95,6 +95,6 @@ func (this *apiComp) Practice(session comm.IUserSession, req *pb.MartialhallPrac
this.module.modelMartialhall.Change(session.GetUserId(), map[string]interface{}{
filed: pillar,
})
session.SendMsg(string(this.module.GetType()), "practice", &pb.MartialhallPracticeResp{Pillar: req.Pillar, Hero: req.Hero, Issucc: true})
session.SendMsg(string(this.module.GetType()), "practice", &pb.MartialhallPracticeResp{Issucc: true, Info: mart})
return
}

View File

@ -177,9 +177,8 @@ type MartialhallPracticeResp struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Pillar int32 `protobuf:"varint,1,opt,name=pillar,proto3" json:"pillar"` //柱子
Hero string `protobuf:"bytes,2,opt,name=hero,proto3" json:"hero"` //英雄
Issucc bool `protobuf:"varint,3,opt,name=issucc,proto3" json:"issucc"` //是否成功
Issucc bool `protobuf:"varint,1,opt,name=issucc,proto3" json:"issucc"` //是否成功
Info *DBMartialhall `protobuf:"bytes,2,opt,name=info,proto3" json:"info"`
}
func (x *MartialhallPracticeResp) Reset() {
@ -214,20 +213,6 @@ func (*MartialhallPracticeResp) Descriptor() ([]byte, []int) {
return file_martialhall_martialhall_msg_proto_rawDescGZIP(), []int{3}
}
func (x *MartialhallPracticeResp) GetPillar() int32 {
if x != nil {
return x.Pillar
}
return 0
}
func (x *MartialhallPracticeResp) GetHero() string {
if x != nil {
return x.Hero
}
return ""
}
func (x *MartialhallPracticeResp) GetIssucc() bool {
if x != nil {
return x.Issucc
@ -235,6 +220,13 @@ func (x *MartialhallPracticeResp) GetIssucc() bool {
return false
}
func (x *MartialhallPracticeResp) GetInfo() *DBMartialhall {
if x != nil {
return x.Info
}
return nil
}
///领取 请求
type MartialhallReceiveReq struct {
state protoimpl.MessageState
@ -531,32 +523,31 @@ var file_martialhall_martialhall_msg_proto_rawDesc = []byte{
0x52, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x65, 0x72, 0x6f,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x12, 0x12, 0x0a, 0x04,
0x74, 0x69, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x69, 0x6d, 0x65,
0x22, 0x5d, 0x0a, 0x17, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x50,
0x72, 0x61, 0x63, 0x74, 0x69, 0x63, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x70,
0x69, 0x6c, 0x6c, 0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x69, 0x6c,
0x6c, 0x61, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x04, 0x68, 0x65, 0x72, 0x6f, 0x12, 0x16, 0x0a, 0x06, 0x69, 0x73, 0x73, 0x75, 0x63,
0x63, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x69, 0x73, 0x73, 0x75, 0x63, 0x63, 0x22,
0x2f, 0x0a, 0x15, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x52, 0x65,
0x63, 0x65, 0x69, 0x76, 0x65, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x69, 0x6c, 0x6c,
0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61, 0x72,
0x22, 0x18, 0x0a, 0x16, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x52,
0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x52, 0x65, 0x73, 0x70, 0x22, 0x17, 0x0a, 0x15, 0x4d, 0x61,
0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65,
0x52, 0x65, 0x71, 0x22, 0x3c, 0x0a, 0x16, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61,
0x6c, 0x6c, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x22, 0x0a,
0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x44, 0x42,
0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x52, 0x04, 0x69, 0x6e, 0x66,
0x6f, 0x22, 0x2e, 0x0a, 0x14, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c,
0x55, 0x6e, 0x4c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x69, 0x6c,
0x6c, 0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61,
0x72, 0x22, 0x53, 0x0a, 0x15, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c,
0x55, 0x6e, 0x4c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x69, 0x73,
0x73, 0x75, 0x63, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x69, 0x73, 0x73, 0x75,
0x63, 0x63, 0x12, 0x22, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x0e, 0x2e, 0x44, 0x42, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c,
0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x22, 0x55, 0x0a, 0x17, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x50,
0x72, 0x61, 0x63, 0x74, 0x69, 0x63, 0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x69,
0x73, 0x73, 0x75, 0x63, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x69, 0x73, 0x73,
0x75, 0x63, 0x63, 0x12, 0x22, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x0e, 0x2e, 0x44, 0x42, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c,
0x6c, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x22, 0x2f, 0x0a, 0x15, 0x4d, 0x61, 0x72, 0x74, 0x69,
0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x52, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x52, 0x65, 0x71,
0x12, 0x16, 0x0a, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05,
0x52, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61, 0x72, 0x22, 0x18, 0x0a, 0x16, 0x4d, 0x61, 0x72, 0x74,
0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x52, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x52, 0x65,
0x73, 0x70, 0x22, 0x17, 0x0a, 0x15, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c,
0x6c, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x52, 0x65, 0x71, 0x22, 0x3c, 0x0a, 0x16, 0x4d,
0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64,
0x65, 0x52, 0x65, 0x73, 0x70, 0x12, 0x22, 0x0a, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x18, 0x01, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x44, 0x42, 0x4d, 0x61, 0x72, 0x74, 0x69, 0x61, 0x6c, 0x68,
0x61, 0x6c, 0x6c, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x22, 0x2e, 0x0a, 0x14, 0x4d, 0x61, 0x72,
0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x55, 0x6e, 0x4c, 0x6f, 0x63, 0x6b, 0x52, 0x65,
0x71, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28,
0x05, 0x52, 0x06, 0x70, 0x69, 0x6c, 0x6c, 0x61, 0x72, 0x22, 0x53, 0x0a, 0x15, 0x4d, 0x61, 0x72,
0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x55, 0x6e, 0x4c, 0x6f, 0x63, 0x6b, 0x52, 0x65,
0x73, 0x70, 0x12, 0x16, 0x0a, 0x06, 0x69, 0x73, 0x73, 0x75, 0x63, 0x63, 0x18, 0x01, 0x20, 0x01,
0x28, 0x08, 0x52, 0x06, 0x69, 0x73, 0x73, 0x75, 0x63, 0x63, 0x12, 0x22, 0x0a, 0x04, 0x69, 0x6e,
0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x44, 0x42, 0x4d, 0x61, 0x72,
0x74, 0x69, 0x61, 0x6c, 0x68, 0x61, 0x6c, 0x6c, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x42, 0x06,
0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -587,13 +578,14 @@ var file_martialhall_martialhall_msg_proto_goTypes = []interface{}{
}
var file_martialhall_martialhall_msg_proto_depIdxs = []int32{
10, // 0: MartialhallInfoResp.info:type_name -> DBMartialhall
10, // 1: MartialhallUpgradeResp.info:type_name -> DBMartialhall
10, // 2: MartialhallUnLockResp.info:type_name -> DBMartialhall
3, // [3:3] is the sub-list for method output_type
3, // [3:3] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
10, // 1: MartialhallPracticeResp.info:type_name -> DBMartialhall
10, // 2: MartialhallUpgradeResp.info:type_name -> DBMartialhall
10, // 3: MartialhallUnLockResp.info:type_name -> DBMartialhall
4, // [4:4] is the sub-list for method output_type
4, // [4:4] is the sub-list for method input_type
4, // [4:4] is the sub-list for extension type_name
4, // [4:4] is the sub-list for extension extendee
0, // [0:4] is the sub-list for field type_name
}
func init() { file_martialhall_martialhall_msg_proto_init() }

View File

@ -6,8 +6,13 @@ import (
)
func newSys(options *Options) (sys *DB, err error) {
sys = &DB{options: options}
err = sys.init()
sys = &DB{
options: options,
data: make(map[string]*ModelDataExpired),
}
if err = sys.init(); err != nil {
go sys.run()
}
return
}

View File

@ -12,6 +12,7 @@ import (
"go_dreamfactory/lego/utils/codec/codecore"
"go_dreamfactory/lego/utils/codec/json"
"reflect"
"time"
"unsafe"
"github.com/go-redis/redis/v8"
@ -72,6 +73,7 @@ type DBConn struct {
func NewDBModel(TableName string, conn *DBConn) *DBModel {
return &DBModel{
TableName: TableName,
Expired: 2 * time.Minute,
Redis: conn.Redis,
DB: conn.Mgo,
}
@ -80,6 +82,7 @@ func NewDBModel(TableName string, conn *DBConn) *DBModel {
//DB模型
type DBModel struct {
TableName string
Expired time.Duration //过期时间
Redis lgredis.ISys
DB mgo.ISys
}
@ -154,8 +157,9 @@ func (this *DBModel) Add(uid string, data interface{}, opt ...DBOption) (err err
if option.IsMgoLog {
err = this.InsertModelLogs(this.TableName, uid, []interface{}{data})
}
if option.Expire > 0 {
err = this.Redis.Expire(this.ukey(uid), option.Expire)
if this.Expired > 0 {
// err = this.Redis.Expire(this.ukey(uid), option.Expire)
UpDateModelExpired(this.ukey(uid), nil, this.Expired)
}
return
}
@ -173,8 +177,9 @@ func (this *DBModel) AddList(uid string, id string, data interface{}, opt ...DBO
if option.IsMgoLog {
err = this.InsertModelLogs(this.TableName, uid, []interface{}{data})
}
if option.Expire > 0 {
err = this.Redis.Expire(this.ukey(uid), option.Expire)
if this.Expired > 0 {
// err = this.Redis.Expire(this.ukey(uid), option.Expire)
UpDateModelExpired(this.ukey(uid), nil, this.Expired)
}
return
}
@ -210,11 +215,13 @@ func (this *DBModel) AddLists(uid string, data interface{}, opt ...DBOption) (er
if option.IsMgoLog {
err = this.InsertModelLogs(this.TableName, uid, lists)
}
if option.Expire > 0 {
this.Redis.Expire(this.ukey(uid), option.Expire)
if this.Expired > 0 {
childs := make(map[string]struct{}, len(listskeys))
for _, v := range listskeys {
this.Redis.Expire(v, option.Expire)
childs[v] = struct{}{}
}
UpDateModelExpired(this.ukey(uid), childs, this.Expired)
// this.Redis.Expire(this.ukey(uid), option.Expire)
}
return
}
@ -267,8 +274,9 @@ func (this *DBModel) Change(uid string, data map[string]interface{}, opt ...DBOp
if option.IsMgoLog {
err = this.UpdateModelLogs(this.TableName, uid, bson.M{"uid": uid}, data)
}
if option.Expire > 0 {
this.Redis.Expire(this.ukey(uid), option.Expire)
if this.Expired > 0 {
UpDateModelExpired(this.ukey(uid), nil, this.Expired)
// this.Redis.Expire(this.ukey(uid), option.Expire)
}
return nil
}
@ -283,8 +291,9 @@ func (this *DBModel) ChangeList(uid string, _id string, data map[string]interfac
if option.IsMgoLog {
err = this.UpdateModelLogs(this.TableName, uid, bson.M{"_id": _id, "uid": uid}, data)
}
if option.Expire > 0 {
this.Redis.Expire(this.ukey(uid), option.Expire)
if this.Expired > 0 {
UpDateModelExpired(this.ukey(uid), nil, this.Expired)
// this.Redis.Expire(this.ukey(uid), option.Expire)
}
return nil
}
@ -300,9 +309,10 @@ func (this *DBModel) Get(uid string, data interface{}, opt ...DBOption) (err err
}
err = this.Redis.HMSet(this.ukey(uid), data)
}
option := newDBOption(opt...)
if option.Expire > 0 {
this.Redis.Expire(this.ukey(uid), option.Expire)
// option := newDBOption(opt...)
if this.Expired > 0 {
// this.Redis.Expire(this.ukey(uid), option.Expire)
UpDateModelExpired(this.ukey(uid), nil, this.Expired)
}
return
}
@ -411,7 +421,14 @@ func (this *DBModel) GetList(uid string, data interface{}) (err error) {
}
}
}
if this.Expired > 0 {
childs := make(map[string]struct{}, len(keys))
for _, v := range keys {
childs[v] = struct{}{}
}
UpDateModelExpired(this.ukey(uid), childs, this.Expired)
// this.Redis.Expire(this.ukey(uid), option.Expire)
}
return err
}
@ -487,19 +504,130 @@ func (this *DBModel) GetQueues(key string, count int, data interface{}) (err err
//读取单个数据中 多个字段数据
func (this *DBModel) GetFields(uid string, data interface{}, fields ...string) (err error) {
this.Redis.HMGet(this.ukey(uid), data, fields...)
if err = this.Redis.HMGet(this.ukey(uid), data, fields...); err != nil && err != lgredis.RedisNil {
return
}
if err == lgredis.RedisNil {
if err = this.DB.FindOne(core.SqlTable(this.TableName), bson.M{"uid": uid}).Decode(data); err != nil {
return err
}
if err = this.Redis.HMSet(this.ukey(uid), data); err != nil {
return
}
}
if this.Expired > 0 {
UpDateModelExpired(this.ukey(uid), nil, this.Expired)
}
return
}
//读取List列表中单个数据中 多个字段数据
func (this *DBModel) GetListFields(uid string, id string, data interface{}, fields ...string) (err error) {
this.Redis.HMGet(this.ukeylist(uid, id), data, fields...)
var (
c *mongo.Cursor
keys map[string]string
tempdata map[string]string
)
if err = this.Redis.HMGet(this.ukeylist(uid, id), data, fields...); err != nil && err != lgredis.RedisNil {
return
}
if err == lgredis.RedisNil {
if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{"uid": uid}); err != nil {
return err
} else {
pipe := this.Redis.RedisPipe(context.TODO())
dtype := reflect2.TypeOf(data).(*reflect2.UnsafeStructType)
for c.Next(context.Background()) {
_id := c.Current.Lookup("_id").StringValue()
if _id != id {
temp := dtype.New()
if err = c.Decode(temp); err != nil {
return
}
if tempdata, err = json.MarshalMap(temp); err != nil {
return
}
} else {
if err = c.Decode(data); err != nil {
return
}
if tempdata, err = json.MarshalMap(data); err != nil {
return
}
}
key := this.ukeylist(uid, _id)
pipe.HMSetForMap(key, tempdata)
keys[_id] = key
}
if len(keys) > 0 {
pipe.HMSetForMap(this.ukey(uid), keys)
_, err = pipe.Exec()
}
}
}
if this.Expired > 0 {
childs := make(map[string]struct{}, len(keys))
for _, v := range keys {
childs[v] = struct{}{}
}
UpDateModelExpired(this.ukey(uid), childs, this.Expired)
}
return
}
//读取列表数据中单个数据
func (this *DBModel) GetListObj(uid string, id string, data interface{}) (err error) {
err = this.Redis.HGetAll(this.ukeylist(uid, id), data)
var (
c *mongo.Cursor
keys map[string]string
tempdata map[string]string
)
keys = make(map[string]string)
if err = this.Redis.HGetAll(this.ukeylist(uid, id), data); err != nil && err != lgredis.RedisNil {
return
}
if err == lgredis.RedisNil {
if c, err = this.DB.Find(core.SqlTable(this.TableName), bson.M{"uid": uid}); err != nil {
return err
} else {
pipe := this.Redis.RedisPipe(context.TODO())
dtype := reflect2.TypeOf(data).(*reflect2.UnsafeStructType)
for c.Next(context.Background()) {
_id := c.Current.Lookup("_id").StringValue()
if _id != id {
temp := dtype.New()
if err = c.Decode(temp); err != nil {
return
}
if tempdata, err = json.MarshalMap(temp); err != nil {
return
}
} else {
if err = c.Decode(data); err != nil {
return
}
if tempdata, err = json.MarshalMap(data); err != nil {
return
}
}
key := this.ukeylist(uid, _id)
pipe.HMSetForMap(key, tempdata)
keys[_id] = key
}
if len(keys) > 0 {
pipe.HMSetForMap(this.ukey(uid), keys)
_, err = pipe.Exec()
}
}
}
if this.Expired > 0 {
childs := make(map[string]struct{}, len(keys))
for _, v := range keys {
childs[v] = struct{}{}
}
UpDateModelExpired(this.ukey(uid), childs, this.Expired)
}
return
}

View File

@ -11,7 +11,14 @@ func (this *DB) UpDateModelExpired(key string, childs map[string]struct{}, expir
exp, ok := this.data[key]
this.mu.RUnlock()
if ok {
exp.keys = childs
if childs != nil {
if exp.keys == nil {
exp.keys = make(map[string]struct{})
}
for k, _ := range childs {
exp.keys[k] = struct{}{}
}
}
exp.expired = time.Now().Add(expired)
} else {
exp = &ModelDataExpired{