tank/code/rest/session_dao.go

76 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rest
import (
"github.com/eyebluecn/tank/code/core"
"github.com/nu7hatch/gouuid"
"time"
)
type SessionDao struct {
BaseDao
}
//按照Id查询session.
func (this *SessionDao) FindByUuid(uuid string) *Session {
// Read
var session = &Session{}
db := core.CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session)
if db.Error != nil {
return nil
}
return session
}
//按照Id查询session.
func (this *SessionDao) CheckByUuid(uuid string) *Session {
// Read
var session = &Session{}
db := core.CONTEXT.GetDB().Where(&Session{Base: Base{Uuid: uuid}}).First(session)
this.PanicError(db.Error)
return session
}
//创建一个session并且持久化到数据库中。
func (this *SessionDao) Create(session *Session) *Session {
timeUUID, _ := uuid.NewV4()
session.Uuid = string(timeUUID.String())
session.CreateTime = time.Now()
session.UpdateTime = time.Now()
session.Sort = time.Now().UnixNano() / 1e6
db := core.CONTEXT.GetDB().Create(session)
this.PanicError(db.Error)
return session
}
//修改一个session
func (this *SessionDao) Save(session *Session) *Session {
session.UpdateTime = time.Now()
db := core.CONTEXT.GetDB().Save(session)
this.PanicError(db.Error)
return session
}
func (this *SessionDao) Delete(uuid string) {
session := this.CheckByUuid(uuid)
session.ExpireTime = time.Now()
db := core.CONTEXT.GetDB().Delete(session)
this.PanicError(db.Error)
}
//执行清理操作
func (this *SessionDao) Cleanup() {
this.logger.Info("[SessionDao]执行清理清除数据库中所有Session记录。")
db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Session{})
this.PanicError(db.Error)
}