hotime/session.go

72 lines
1.3 KiB
Go
Raw Normal View History

2017-08-04 08:20:59 +00:00
package hotime
2017-08-10 10:14:56 +00:00
//session对象
2017-08-04 08:20:59 +00:00
type SessionIns struct {
ShortCache CacheIns
2017-08-10 10:14:56 +00:00
LongCache CacheIns
SessionId string
2017-08-04 08:20:59 +00:00
Map
2017-08-23 07:35:49 +00:00
contextBase
2017-08-04 08:20:59 +00:00
}
2017-08-10 10:14:56 +00:00
func (this *SessionIns) set() {
2017-08-04 08:20:59 +00:00
2017-08-10 10:14:56 +00:00
if this.ShortCache != nil {
this.ShortCache.Cache(HEAD_SESSION_ADD+this.SessionId, this.Map)
2017-08-10 10:14:56 +00:00
}
if this.LongCache != nil {
this.LongCache.Cache(HEAD_SESSION_ADD+this.SessionId, this.Map)
2017-08-10 10:14:56 +00:00
}
2017-08-04 08:20:59 +00:00
}
2017-08-10 10:14:56 +00:00
func (this *SessionIns) Session(key string, data ...interface{}) *Obj {
2017-08-04 08:20:59 +00:00
2017-08-10 10:14:56 +00:00
if this.Map == nil {
2017-08-04 08:20:59 +00:00
this.get()
}
2017-08-10 10:14:56 +00:00
if len(data) != 0 {
if data[0] == nil {
delete(this.Map, key)
2017-08-04 08:20:59 +00:00
this.set()
2017-08-10 10:14:56 +00:00
} else {
this.Map[key] = data[0]
2017-08-04 08:20:59 +00:00
this.set()
}
2017-08-10 10:14:56 +00:00
return &Obj{Data: nil}
2017-08-04 08:20:59 +00:00
}
2017-08-10 10:14:56 +00:00
return &Obj{Data: this.Map.Get(key)}
2017-08-04 08:20:59 +00:00
}
2017-08-10 10:14:56 +00:00
func (this *SessionIns) get() {
2017-08-04 08:20:59 +00:00
2017-08-10 10:14:56 +00:00
if this.ShortCache != nil {
2018-04-03 17:54:27 +00:00
this.Map = this.ShortCache.Cache(HEAD_SESSION_ADD + this.SessionId).ToMap()
2017-08-10 10:14:56 +00:00
if this.Map != nil {
2017-08-04 08:20:59 +00:00
return
}
}
2017-08-10 10:14:56 +00:00
if this.LongCache != nil {
2018-04-03 17:54:27 +00:00
this.Map = this.LongCache.Cache(HEAD_SESSION_ADD + this.SessionId).ToMap()
2017-08-10 10:14:56 +00:00
if this.Map != nil {
if this.ShortCache != nil {
2018-04-03 17:54:27 +00:00
this.ShortCache.Cache(HEAD_SESSION_ADD+this.SessionId, this.Map)
2017-08-04 08:20:59 +00:00
}
return
}
}
2017-08-10 10:14:56 +00:00
this.Map = Map{}
2018-04-03 17:54:27 +00:00
this.ShortCache.Cache(HEAD_SESSION_ADD+this.SessionId, this.Map)
2017-08-04 08:20:59 +00:00
return
}
2017-08-10 10:14:56 +00:00
func (this *SessionIns) Init(short CacheIns, long CacheIns) {
this.ShortCache = short
this.LongCache = long
}