1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 15:10:54 +00:00
Beego/session/ssdb/sess_ssdb.go

200 lines
4.2 KiB
Go
Raw Normal View History

2016-05-10 11:56:45 +00:00
package ssdb
import (
2016-05-20 02:27:18 +00:00
"errors"
2016-05-10 11:56:45 +00:00
"net/http"
2016-05-15 04:15:40 +00:00
"strconv"
"strings"
"sync"
2016-05-10 11:56:45 +00:00
"github.com/astaxie/beego/session"
"github.com/ssdb/gossdb/ssdb"
)
2017-04-30 14:41:23 +00:00
var ssdbProvider = &Provider{}
2016-05-10 11:56:45 +00:00
2017-04-30 14:41:23 +00:00
// Provider holds ssdb client and configs
type Provider struct {
2016-05-15 04:15:40 +00:00
client *ssdb.Client
host string
2016-05-19 13:00:04 +00:00
port int
2016-05-15 04:15:40 +00:00
maxLifetime int64
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
func (p *Provider) connectInit() error {
2016-05-15 04:15:40 +00:00
var err error
2016-05-20 02:27:18 +00:00
if p.host == "" || p.port == 0 {
return errors.New("SessionInit First")
}
2016-05-15 04:15:40 +00:00
p.client, err = ssdb.Connect(p.host, p.port)
2017-03-17 17:24:45 +00:00
return err
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionInit init the ssdb with the config
func (p *Provider) SessionInit(maxLifetime int64, savePath string) error {
2016-05-15 04:15:40 +00:00
p.maxLifetime = maxLifetime
address := strings.Split(savePath, ":")
p.host = address[0]
var err error
if p.port, err = strconv.Atoi(address[1]); err != nil {
return err
2016-05-15 04:15:40 +00:00
}
2017-03-17 17:24:45 +00:00
return p.connectInit()
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionRead return a ssdb client session Store
func (p *Provider) SessionRead(sid string) (session.Store, error) {
2016-05-15 04:15:40 +00:00
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
}
2016-05-19 13:00:04 +00:00
if value == nil || len(value.(string)) == 0 {
2016-05-15 04:15:40 +00:00
kv = make(map[interface{}]interface{})
} else {
2016-05-19 13:00:04 +00:00
kv, err = session.DecodeGob([]byte(value.(string)))
2016-05-15 04:15:40 +00:00
if err != nil {
return nil, err
}
}
rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
return rs, nil
2016-05-10 11:56:45 +00:00
}
2016-05-15 04:15:40 +00:00
2017-04-30 14:41:23 +00:00
// SessionExist judged whether sid is exist in session
func (p *Provider) SessionExist(sid string) bool {
2016-05-15 04:15:40 +00:00
if p.client == nil {
if err := p.connectInit(); err != nil {
2016-05-19 13:00:04 +00:00
panic(err)
2016-05-15 04:15:40 +00:00
}
}
2016-05-19 13:00:04 +00:00
value, err := p.client.Get(sid)
if err != nil {
panic(err)
}
if value == nil || len(value.(string)) == 0 {
2016-05-15 04:15:40 +00:00
return false
}
return true
}
2017-04-30 14:41:23 +00:00
// SessionRegenerate regenerate session with new sid and delete oldsid
func (p *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
2016-05-15 04:15:40 +00:00
//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)
2016-05-19 13:00:04 +00:00
if err != nil {
return nil, err
2016-05-15 04:15:40 +00:00
}
var kv map[interface{}]interface{}
2016-05-19 13:00:04 +00:00
if value == nil || len(value.(string)) == 0 {
2016-05-15 04:15:40 +00:00
kv = make(map[interface{}]interface{})
} else {
2016-05-19 13:00:04 +00:00
kv, err = session.DecodeGob([]byte(value.(string)))
if err != nil {
return nil, err
}
_, err = p.client.Del(oldsid)
2016-05-15 04:15:40 +00:00
if err != nil {
return nil, err
}
}
2016-05-20 02:27:18 +00:00
_, e := p.client.Do("setx", sid, value, p.maxLifetime)
2016-05-19 13:00:04 +00:00
if e != nil {
return nil, e
}
2016-05-15 04:15:40 +00:00
rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client}
return rs, nil
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionDestroy destroy the sid
func (p *Provider) SessionDestroy(sid string) error {
2016-05-15 04:15:40 +00:00
if p.client == nil {
if err := p.connectInit(); err != nil {
2016-05-19 13:00:04 +00:00
return err
2016-05-15 04:15:40 +00:00
}
}
2016-05-19 13:00:04 +00:00
_, err := p.client.Del(sid)
2017-03-17 17:24:45 +00:00
return err
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionGC not implemented
func (p *Provider) SessionGC() {
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionAll not implemented
func (p *Provider) SessionAll() int {
2016-05-10 11:56:45 +00:00
return 0
}
2017-04-30 14:41:23 +00:00
// SessionStore holds the session information which stored in ssdb
2016-05-10 11:56:45 +00:00
type SessionStore struct {
2016-05-15 04:15:40 +00:00
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxLifetime int64
client *ssdb.Client
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// Set the key and value
2016-05-10 11:56:45 +00:00
func (s *SessionStore) Set(key, value interface{}) error {
2016-05-15 04:15:40 +00:00
s.lock.Lock()
defer s.lock.Unlock()
s.values[key] = value
return nil
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// Get return the value by the key
2016-05-10 11:56:45 +00:00
func (s *SessionStore) Get(key interface{}) interface{} {
2016-05-15 04:15:40 +00:00
s.lock.Lock()
defer s.lock.Unlock()
2016-05-19 13:00:04 +00:00
if value, ok := s.values[key]; ok {
2016-05-15 04:15:40 +00:00
return value
}
return nil
2016-05-10 11:56:45 +00:00
}
2016-05-15 04:15:40 +00:00
2017-04-30 14:41:23 +00:00
// Delete the key in session store
2016-05-10 11:56:45 +00:00
func (s *SessionStore) Delete(key interface{}) error {
2016-05-15 04:15:40 +00:00
s.lock.Lock()
defer s.lock.Unlock()
delete(s.values, key)
return nil
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// Flush delete all keys and values
2016-05-10 11:56:45 +00:00
func (s *SessionStore) Flush() error {
2016-05-15 04:15:40 +00:00
s.lock.Lock()
defer s.lock.Unlock()
s.values = make(map[interface{}]interface{})
return nil
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionID return the sessionID
2016-05-10 11:56:45 +00:00
func (s *SessionStore) SessionID() string {
2016-05-15 04:15:40 +00:00
return s.sid
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
// SessionRelease Store the keyvalues into ssdb
2016-05-10 11:56:45 +00:00
func (s *SessionStore) SessionRelease(w http.ResponseWriter) {
2016-05-15 04:15:40 +00:00
b, err := session.EncodeGob(s.values)
if err != nil {
return
}
2016-05-19 13:00:04 +00:00
s.client.Do("setx", s.sid, string(b), s.maxLifetime)
2016-05-10 11:56:45 +00:00
}
2017-04-30 14:41:23 +00:00
2016-05-10 11:56:45 +00:00
func init() {
2016-05-15 04:15:40 +00:00
session.Register("ssdb", ssdbProvider)
2016-05-10 11:56:45 +00:00
}