71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package user
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/db"
|
|
)
|
|
|
|
type ModelSession struct {
|
|
modules.MCompModel
|
|
module *User
|
|
}
|
|
|
|
func (this *ModelSession) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
err = this.MCompModel.Init(service, module, comp, options)
|
|
this.module = module.(*User)
|
|
this.TableName = comm.TableSession
|
|
this.Expired = 0 //不自动过期
|
|
return
|
|
}
|
|
|
|
func (this *ModelSession) Start() (err error) {
|
|
this.MCompModel.Start()
|
|
this.clean(this.module.service.GetTag())
|
|
return
|
|
}
|
|
|
|
//获取用户
|
|
func (this *ModelSession) getUserSession(uid string) (user *pb.CacheUser) {
|
|
user = &pb.CacheUser{}
|
|
if err := this.GetListObj(comm.RDS_SESSION, uid, user); err != nil {
|
|
this.module.Errorln(err)
|
|
return nil
|
|
}
|
|
return user
|
|
}
|
|
|
|
//设置用户session
|
|
func (this *ModelSession) setUserSession(uid string, session comm.IUserSession) (err error) {
|
|
if err = this.AddList(comm.RDS_SESSION, uid, map[string]interface{}{
|
|
"uid": uid,
|
|
"sessionId": session.GetSessionId(),
|
|
"serviceTag": session.GetServiecTag(),
|
|
"gatewayServiceId": session.GetGatewayServiceId(),
|
|
"ip": session.GetIP(),
|
|
}, db.SetDBMgoLog(false)); err != nil {
|
|
log.Debugf("setUserSession err:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 启动时清理session
|
|
func (this *ModelSession) clean(key string) {
|
|
keys, err := this.Redis.Keys(fmt.Sprintf("session:%s-%s_*", comm.RDS_SESSION, key))
|
|
if err != nil {
|
|
log.Errorf("redis keys err:%v", err)
|
|
return
|
|
}
|
|
|
|
for _, k := range keys {
|
|
if err := this.Redis.Delete(k); err != nil {
|
|
log.Errorf("redis delete key err:%v", err)
|
|
}
|
|
}
|
|
}
|