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

193 lines
3.8 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"
)
2016-05-15 04:15:40 +00:00
var ssdbProvider = &SsdbProvider{}
2016-05-10 11:56:45 +00:00
type SsdbProvider 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
}
2016-05-15 04:15:40 +00:00
func (p *SsdbProvider) connectInit() error {
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)
if err != nil {
return err
}
return nil
2016-05-10 11:56:45 +00:00
}
2016-05-15 04:15:40 +00:00
func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error {
2016-05-19 13:00:04 +00:00
var e error = nil
2016-05-15 04:15:40 +00:00
p.maxLifetime = maxLifetime
address := strings.Split(savePath, ":")
p.host = address[0]
2016-05-19 13:00:04 +00:00
p.port, e = strconv.Atoi(address[1])
2016-05-15 04:15:40 +00:00
if e != nil {
return e
}
2016-05-19 13:00:04 +00:00
err := p.connectInit()
2016-05-15 04:15:40 +00:00
if err != nil {
return err
}
return nil
2016-05-10 11:56:45 +00:00
}
2016-05-15 04:15:40 +00:00
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
}
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
func (p *SsdbProvider) SessionExist(sid string) bool {
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
}
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)
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
}
2016-05-15 04:15:40 +00:00
func (p *SsdbProvider) SessionDestroy(sid string) error {
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)
2016-05-15 04:15:40 +00:00
if err != nil {
return err
}
return nil
2016-05-10 11:56:45 +00:00
}
2016-05-15 04:15:40 +00:00
func (p *SsdbProvider) SessionGC() {
2016-05-10 11:56:45 +00:00
return
}
2016-05-15 04:15:40 +00:00
func (p *SsdbProvider) SessionAll() int {
2016-05-10 11:56:45 +00:00
return 0
}
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
}
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
}
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
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
}
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
}
func (s *SessionStore) SessionID() string {
2016-05-15 04:15:40 +00:00
return s.sid
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-15 04:15:40 +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
}