hotime/session.go

77 lines
1.3 KiB
Go
Raw Normal View History

2017-08-04 08:20:59 +00:00
package hotime
2021-05-23 23:27:41 +00:00
import (
. "./cache"
. "./common"
)
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
2021-05-23 23:27:41 +00:00
ContextBase
2017-08-04 08:20:59 +00:00
}
func (that *SessionIns) set() {
2017-08-04 08:20:59 +00:00
if that.ShortCache != nil {
that.ShortCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
2017-08-10 10:14:56 +00:00
}
if that.LongCache != nil {
that.LongCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
2017-08-10 10:14:56 +00:00
}
2017-08-04 08:20:59 +00:00
}
func (that *SessionIns) Session(key string, data ...interface{}) *Obj {
2017-08-04 08:20:59 +00:00
if that.Map == nil {
that.get()
2017-08-04 08:20:59 +00:00
}
2017-08-10 10:14:56 +00:00
if len(data) != 0 {
if data[0] == nil {
delete(that.Map, key)
that.set()
2017-08-10 10:14:56 +00:00
} else {
that.Map[key] = data[0]
that.set()
2017-08-04 08:20:59 +00:00
}
2017-08-10 10:14:56 +00:00
return &Obj{Data: nil}
2017-08-04 08:20:59 +00:00
}
return &Obj{Data: that.Map.Get(key)}
2017-08-04 08:20:59 +00:00
}
func (that *SessionIns) get() {
2017-08-04 08:20:59 +00:00
if that.ShortCache != nil {
that.Map = that.ShortCache.Cache(HEAD_SESSION_ADD + that.SessionId).ToMap()
if that.Map != nil {
2017-08-04 08:20:59 +00:00
return
}
}
if that.LongCache != nil {
that.Map = that.LongCache.Cache(HEAD_SESSION_ADD + that.SessionId).ToMap()
if that.Map != nil {
if that.ShortCache != nil {
that.ShortCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
2017-08-04 08:20:59 +00:00
}
return
}
}
that.Map = Map{}
that.ShortCache.Cache(HEAD_SESSION_ADD+that.SessionId, that.Map)
2017-08-04 08:20:59 +00:00
return
}
func (that *SessionIns) Init(short CacheIns, long CacheIns) {
that.ShortCache = short
that.LongCache = long
2017-08-10 10:14:56 +00:00
}