From cfef97175ef4e7db99f3fe69dcf6f12321be5448 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Sat, 12 Mar 2016 12:34:54 +0800 Subject: [PATCH 1/7] change import sort --- cache/ssdb/ssdb_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cache/ssdb/ssdb_test.go b/cache/ssdb/ssdb_test.go index e03ba343..dd474960 100644 --- a/cache/ssdb/ssdb_test.go +++ b/cache/ssdb/ssdb_test.go @@ -1,10 +1,11 @@ package ssdb import ( - "github.com/astaxie/beego/cache" "strconv" "testing" "time" + + "github.com/astaxie/beego/cache" ) func TestSsdbcacheCache(t *testing.T) { From 27be1e7ca3ce2e52efc38b4757623be3041817c2 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Tue, 10 May 2016 19:56:45 +0800 Subject: [PATCH 2/7] add ssdb-session interface --- session/ssdb/sess_ssdb.go | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 session/ssdb/sess_ssdb.go diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go new file mode 100644 index 00000000..109f3416 --- /dev/null +++ b/session/ssdb/sess_ssdb.go @@ -0,0 +1,55 @@ +package ssdb + +import ( + "net/http" + + "github.com/astaxie/beego/session" + "github.com/ssdb/gossdb/ssdb" +) + +var ssdbSsdbProvider = &SsdbSsdbProvider{} + +type SsdbProvider struct { +} + +func (r *SsdbProvider) SessionInit(maxlifetime int64, savePath string) error { +} + +func (r *SsdbProvider) SessionRead(sid string) (session.Store, error) { +} + +func (r *SsdbProvider) SessionExist(sid string) bool { +} +func (r *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { +} + +func (r *SsdbProvider) SessionDestroy(sid string) error { +} + +func (r *SsdbProvider) SessionGC() { + return +} + +func (r *SsdbProvider) SessionAll() int { + return 0 +} + +type SessionStore struct { +} + +func (s *SessionStore) Set(key, value interface{}) error { +} +func (s *SessionStore) Get(key interface{}) interface{} { +} +func (s *SessionStore) Delete(key interface{}) error { +} +func (s *SessionStore) Flush() error { +} +func (s *SessionStore) SessionID() string { +} + +func (s *SessionStore) SessionRelease(w http.ResponseWriter) { +} +func init() { + session.Register("redis", redispder) +} From 0e4b9563ae42173f49182168b2d4bc77aed3add9 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Tue, 10 May 2016 20:08:15 +0800 Subject: [PATCH 3/7] add test file --- session/ssdb/sess_ssdb_test.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 session/ssdb/sess_ssdb_test.go diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go new file mode 100644 index 00000000..e69de29b From e50f4f5631957a741589398f0d14a9e6df241d53 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Sun, 15 May 2016 12:15:40 +0800 Subject: [PATCH 4/7] complete sess_ssdb.go --- session/ssdb/sess_ssdb.go | 148 +++++++++++++++++++++++++++++++++++--- 1 file changed, 138 insertions(+), 10 deletions(-) diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index 109f3416..26dd3e8a 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -2,54 +2,182 @@ package ssdb import ( "net/http" + "strconv" + "strings" + "sync" "github.com/astaxie/beego/session" "github.com/ssdb/gossdb/ssdb" ) -var ssdbSsdbProvider = &SsdbSsdbProvider{} +var ssdbProvider = &SsdbProvider{} type SsdbProvider struct { + client *ssdb.Client + host string + port int32 + maxLifetime int64 } -func (r *SsdbProvider) SessionInit(maxlifetime int64, savePath string) error { +func (p *SsdbProvider) connectInit() error { + var err error + p.client, err = ssdb.Connect(p.host, p.port) + if err != nil { + return err + } + return nil } -func (r *SsdbProvider) SessionRead(sid string) (session.Store, error) { +func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { + p.maxLifetime = maxLifetime + address := strings.Split(savePath, ":") + p.host = address[0] + port, e := strconv.Atoi(address[1]) + if e != nil { + return e + } + p.port = address[1] + err := p.connectinit() + if err != nil { + return err + } + return nil } -func (r *SsdbProvider) SessionExist(sid string) bool { -} -func (r *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { +func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + var kv map[interface{}]interface{} + value, err := p.client.Get(sid) + if err != nil { + return nil, err + } + if value == nil || len(value) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob([]byte(value)) + if err != nil { + return nil, err + } + } + rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client} + return rs, nil } -func (r *SsdbProvider) SessionDestroy(sid string) error { +func (p *SsdbProvider) SessionExist(sid string) bool { + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + if value == nil || len(value) == 0 { + return false + } + return true + +} +func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { + //conn.Do("setx", key, v, ttl) + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + value, err := p.client.Get(oldsid) + if err != nil || len(value) == 0 { + value = "" + } 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{} + if value == nil || len(value) == 0 { + kv = make(map[interface{}]interface{}) + } else { + var err error + kv, err = session.DecodeGob(value) + if err != nil { + return nil, err + } + } + rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client} + return rs, nil } -func (r *SsdbProvider) SessionGC() { +func (p *SsdbProvider) SessionDestroy(sid string) error { + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + flag, err := p.client.Del(sid) + if err != nil { + return err + } + return nil +} + +func (p *SsdbProvider) SessionGC() { return } -func (r *SsdbProvider) SessionAll() int { +func (p *SsdbProvider) SessionAll() int { return 0 } type SessionStore struct { + sid string + lock sync.RWMutex + values map[interface{}]interface{} + maxLifetime int64 + client *ssdb.Client } func (s *SessionStore) Set(key, value interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + s.values[key] = value + return nil } func (s *SessionStore) Get(key interface{}) interface{} { + s.lock.Lock() + defer s.lock.Unlock() + if value, ok := rs.values[key]; ok { + return value + } + return nil } + func (s *SessionStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + delete(s.values, key) + return nil } func (s *SessionStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + s.values = make(map[interface{}]interface{}) + return nil } func (s *SessionStore) SessionID() string { + return s.sid } func (s *SessionStore) SessionRelease(w http.ResponseWriter) { + b, err := session.EncodeGob(s.values) + if err != nil { + return + } + s.client.Do("setx", s.sid, s.values, s.maxLifetime) + } func init() { - session.Register("redis", redispder) + session.Register("ssdb", ssdbProvider) } From 6234b50111b867a34ad8fa9b1bde4450bc6eed80 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Thu, 19 May 2016 21:00:04 +0800 Subject: [PATCH 5/7] test done --- session/ssdb/sess_ssdb.go | 50 +++++++++++++++------------- session/ssdb/sess_ssdb_test.go | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 22 deletions(-) diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index 26dd3e8a..cbb94840 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -15,7 +15,7 @@ var ssdbProvider = &SsdbProvider{} type SsdbProvider struct { client *ssdb.Client host string - port int32 + port int maxLifetime int64 } @@ -29,15 +29,15 @@ func (p *SsdbProvider) connectInit() error { } func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { + var e error = nil p.maxLifetime = maxLifetime address := strings.Split(savePath, ":") p.host = address[0] - port, e := strconv.Atoi(address[1]) + p.port, e = strconv.Atoi(address[1]) if e != nil { return e } - p.port = address[1] - err := p.connectinit() + err := p.connectInit() if err != nil { return err } @@ -55,10 +55,10 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { if err != nil { return nil, err } - if value == nil || len(value) == 0 { + if value == nil || len(value.(string)) == 0 { kv = make(map[interface{}]interface{}) } else { - kv, err = session.DecodeGob([]byte(value)) + kv, err = session.DecodeGob([]byte(value.(string))) if err != nil { return nil, err } @@ -70,10 +70,14 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { func (p *SsdbProvider) SessionExist(sid string) bool { if p.client == 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 true @@ -87,24 +91,26 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err } } value, err := p.client.Get(oldsid) - if err != nil || len(value) == 0 { - value = "" - } else { - err = p.client.Del(sid) - } - _, e := p.client.Do("setx", sid, value, p.maxLifetime) - if e != nil { - return nil, e + if err != nil { + return nil, err } var kv map[interface{}]interface{} - if value == nil || len(value) == 0 { + if value == nil || len(value.(string)) == 0 { kv = make(map[interface{}]interface{}) } else { var err error - kv, err = session.DecodeGob(value) + kv, err = session.DecodeGob([]byte(value.(string))) if err != nil { 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} return rs, nil @@ -113,10 +119,10 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err func (p *SsdbProvider) SessionDestroy(sid string) error { if p.client == 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 { return err } @@ -148,7 +154,7 @@ func (s *SessionStore) Set(key, value interface{}) error { func (s *SessionStore) Get(key interface{}) interface{} { s.lock.Lock() defer s.lock.Unlock() - if value, ok := rs.values[key]; ok { + if value, ok := s.values[key]; ok { return value } return nil @@ -175,7 +181,7 @@ func (s *SessionStore) SessionRelease(w http.ResponseWriter) { if err != nil { return } - s.client.Do("setx", s.sid, s.values, s.maxLifetime) + s.client.Do("setx", s.sid, string(b), s.maxLifetime) } func init() { diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go index e69de29b..dfeba4a3 100644 --- a/session/ssdb/sess_ssdb_test.go +++ b/session/ssdb/sess_ssdb_test.go @@ -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) + +} From 4890dd708c9b9245ed969339e12d2c70a5c4303d Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Fri, 20 May 2016 10:27:18 +0800 Subject: [PATCH 6/7] clear code --- session/ssdb/sess_ssdb.go | 7 +++++-- session/ssdb/sess_ssdb_test.go | 3 --- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index cbb94840..4dcf160a 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -1,6 +1,7 @@ package ssdb import ( + "errors" "net/http" "strconv" "strings" @@ -21,6 +22,9 @@ type SsdbProvider struct { func (p *SsdbProvider) connectInit() error { var err error + if p.host == "" || p.port == 0 { + return errors.New("SessionInit First") + } p.client, err = ssdb.Connect(p.host, p.port) if err != nil { return err @@ -98,7 +102,6 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err if value == nil || len(value.(string)) == 0 { kv = make(map[interface{}]interface{}) } else { - var err error kv, err = session.DecodeGob([]byte(value.(string))) if err != nil { return nil, err @@ -108,7 +111,7 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err return nil, err } } - _, e := p.client.Do("setx", sid, value.(string), p.maxLifetime) + _, e := p.client.Do("setx", sid, value, p.maxLifetime) if e != nil { return nil, e } diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go index dfeba4a3..4a5933f2 100644 --- a/session/ssdb/sess_ssdb_test.go +++ b/session/ssdb/sess_ssdb_test.go @@ -1,7 +1,6 @@ package ssdb import ( - "fmt" "net/http" "testing" ) @@ -55,6 +54,4 @@ func Test(t *testing.T) { if p.SessionExist("1") { t.Error("SessionExist err") } - fmt.Println(newS) - } From e77a591a6c28d90cc0c9daf23f2cb5030878362e Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Fri, 20 May 2016 17:25:22 +0800 Subject: [PATCH 7/7] delete test file --- session/ssdb/sess_ssdb_test.go | 57 ---------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 session/ssdb/sess_ssdb_test.go diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go deleted file mode 100644 index 4a5933f2..00000000 --- a/session/ssdb/sess_ssdb_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package ssdb - -import ( - "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") - } -}