1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:20:54 +00:00

test done

This commit is contained in:
lcbluestorm 2016-05-19 21:00:04 +08:00
parent e50f4f5631
commit 6234b50111
2 changed files with 88 additions and 22 deletions

View File

@ -15,7 +15,7 @@ var ssdbProvider = &SsdbProvider{}
type SsdbProvider struct { type SsdbProvider struct {
client *ssdb.Client client *ssdb.Client
host string host string
port int32 port int
maxLifetime int64 maxLifetime int64
} }
@ -29,15 +29,15 @@ func (p *SsdbProvider) connectInit() error {
} }
func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error {
var e error = nil
p.maxLifetime = maxLifetime p.maxLifetime = maxLifetime
address := strings.Split(savePath, ":") address := strings.Split(savePath, ":")
p.host = address[0] p.host = address[0]
port, e := strconv.Atoi(address[1]) p.port, e = strconv.Atoi(address[1])
if e != nil { if e != nil {
return e return e
} }
p.port = address[1] err := p.connectInit()
err := p.connectinit()
if err != nil { if err != nil {
return err return err
} }
@ -55,10 +55,10 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if value == nil || len(value) == 0 { if value == nil || len(value.(string)) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = session.DecodeGob([]byte(value)) kv, err = session.DecodeGob([]byte(value.(string)))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -70,10 +70,14 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) {
func (p *SsdbProvider) SessionExist(sid string) bool { func (p *SsdbProvider) SessionExist(sid string) bool {
if p.client == nil { if p.client == nil {
if err := p.connectInit(); err != nil { if err := p.connectInit(); err != nil {
return nil, err panic(err)
} }
} }
if value == nil || len(value) == 0 { value, err := p.client.Get(sid)
if err != nil {
panic(err)
}
if value == nil || len(value.(string)) == 0 {
return false return false
} }
return true return true
@ -87,24 +91,26 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err
} }
} }
value, err := p.client.Get(oldsid) value, err := p.client.Get(oldsid)
if err != nil || len(value) == 0 { if err != nil {
value = "" return nil, err
} else {
err = p.client.Del(sid)
}
_, e := p.client.Do("setx", sid, value, p.maxLifetime)
if e != nil {
return nil, e
} }
var kv map[interface{}]interface{} var kv map[interface{}]interface{}
if value == nil || len(value) == 0 { if value == nil || len(value.(string)) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
var err error var err error
kv, err = session.DecodeGob(value) kv, err = session.DecodeGob([]byte(value.(string)))
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = p.client.Del(oldsid)
if err != nil {
return nil, err
}
}
_, e := p.client.Do("setx", sid, value.(string), p.maxLifetime)
if e != nil {
return nil, e
} }
rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client} rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
return rs, nil return rs, nil
@ -113,10 +119,10 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err
func (p *SsdbProvider) SessionDestroy(sid string) error { func (p *SsdbProvider) SessionDestroy(sid string) error {
if p.client == nil { if p.client == nil {
if err := p.connectInit(); err != nil { if err := p.connectInit(); err != nil {
return nil, err return err
} }
} }
flag, err := p.client.Del(sid) _, err := p.client.Del(sid)
if err != nil { if err != nil {
return err return err
} }
@ -148,7 +154,7 @@ func (s *SessionStore) Set(key, value interface{}) error {
func (s *SessionStore) Get(key interface{}) interface{} { func (s *SessionStore) Get(key interface{}) interface{} {
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
if value, ok := rs.values[key]; ok { if value, ok := s.values[key]; ok {
return value return value
} }
return nil return nil
@ -175,7 +181,7 @@ func (s *SessionStore) SessionRelease(w http.ResponseWriter) {
if err != nil { if err != nil {
return return
} }
s.client.Do("setx", s.sid, s.values, s.maxLifetime) s.client.Do("setx", s.sid, string(b), s.maxLifetime)
} }
func init() { func init() {

View File

@ -0,0 +1,60 @@
package ssdb
import (
"fmt"
"net/http"
"testing"
)
func Test(t *testing.T) {
p := &SsdbProvider{}
p.SessionInit(300, "127.0.0.1:8888")
if p.host != "127.0.0.1" || p.port != 8888 {
t.Error("host:port err")
}
if p.client == nil {
t.Error("client err")
}
ss, err := p.SessionRead("1")
if err != nil {
t.Error(err)
}
err = ss.Set("key", "value")
if err != nil {
t.Error(err)
}
if ss.Get("key") != "value" {
t.Error("Get err")
}
err = ss.Delete("key")
//err = ss.Flush()
if err != nil {
t.Error(err)
}
if ss.Get("key") == "value" {
t.Error("Delete/Flush err")
}
if ss.SessionID() != "1" {
t.Error("id err")
}
ss.Set("key1", "value1")
var w http.ResponseWriter
ss.SessionRelease(w)
new, e := p.SessionRead("1")
if new == nil || e != nil {
t.Error(e)
}
if !p.SessionExist("1") {
t.Error("SessionExist err")
}
newS, er := p.SessionRegenerate("1", "3")
if er != nil || newS == nil {
t.Error("SessionRegenerate err")
}
if p.SessionExist("1") {
t.Error("SessionExist err")
}
fmt.Println(newS)
}