123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package ssdb
- import (
- "errors"
- "net/http"
- "strconv"
- "strings"
- "sync"
- "github.com/astaxie/beego/session"
- "github.com/ssdb/gossdb/ssdb"
- )
- var ssdbProvider = &SsdbProvider{}
- type SsdbProvider struct {
- client *ssdb.Client
- host string
- port int
- maxLifetime int64
- }
- 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
- }
- return nil
- }
- func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error {
- var e error = nil
- p.maxLifetime = maxLifetime
- address := strings.Split(savePath, ":")
- p.host = address[0]
- p.port, e = strconv.Atoi(address[1])
- if e != nil {
- return e
- }
- err := p.connectInit()
- if err != nil {
- return err
- }
- return nil
- }
- 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.(string)) == 0 {
- kv = make(map[interface{}]interface{})
- } else {
- kv, err = session.DecodeGob([]byte(value.(string)))
- if err != nil {
- return nil, err
- }
- }
- rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
- return rs, nil
- }
- func (p *SsdbProvider) SessionExist(sid string) bool {
- if p.client == nil {
- if err := p.connectInit(); err != nil {
- panic(err)
- }
- }
- value, err := p.client.Get(sid)
- if err != nil {
- panic(err)
- }
- if value == nil || len(value.(string)) == 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 {
- return nil, err
- }
- var kv map[interface{}]interface{}
- if value == nil || len(value.(string)) == 0 {
- kv = make(map[interface{}]interface{})
- } else {
- 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, p.maxLifetime)
- if e != nil {
- return nil, e
- }
- rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
- return rs, nil
- }
- func (p *SsdbProvider) SessionDestroy(sid string) error {
- if p.client == nil {
- if err := p.connectInit(); err != nil {
- return err
- }
- }
- _, err := p.client.Del(sid)
- if err != nil {
- return err
- }
- return nil
- }
- func (p *SsdbProvider) SessionGC() {
- return
- }
- 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 := s.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, string(b), s.maxLifetime)
- }
- func init() {
- session.Register("ssdb", ssdbProvider)
- }
|