1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-24 23:50:55 +00:00
Beego/session/ledis/ledis_session.go

174 lines
3.7 KiB
Go
Raw Normal View History

2015-09-12 14:53:55 +00:00
// Package ledis provide session Provider
package ledis
2014-10-27 05:56:00 +00:00
import (
2015-03-01 04:59:34 +00:00
"net/http"
"strconv"
"strings"
"sync"
2020-06-06 10:59:12 +00:00
"github.com/ledisdb/ledisdb/config"
"github.com/ledisdb/ledisdb/ledis"
2015-03-01 04:59:34 +00:00
"github.com/astaxie/beego/session"
2014-10-27 05:56:00 +00:00
)
var (
ledispder = &Provider{}
c *ledis.DB
)
2014-10-27 05:56:00 +00:00
2015-09-12 14:53:55 +00:00
// SessionStore ledis session store
type SessionStore struct {
2015-03-01 04:59:34 +00:00
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxlifetime int64
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// Set value in ledis session
func (ls *SessionStore) Set(key, value interface{}) error {
2015-03-01 04:59:34 +00:00
ls.lock.Lock()
defer ls.lock.Unlock()
ls.values[key] = value
return nil
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// Get value in ledis session
func (ls *SessionStore) Get(key interface{}) interface{} {
2015-03-01 04:59:34 +00:00
ls.lock.RLock()
defer ls.lock.RUnlock()
if v, ok := ls.values[key]; ok {
return v
}
2015-09-12 14:53:55 +00:00
return nil
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// Delete value in ledis session
func (ls *SessionStore) Delete(key interface{}) error {
2015-03-01 04:59:34 +00:00
ls.lock.Lock()
defer ls.lock.Unlock()
delete(ls.values, key)
return nil
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// Flush clear all values in ledis session
func (ls *SessionStore) Flush() error {
2015-03-01 04:59:34 +00:00
ls.lock.Lock()
defer ls.lock.Unlock()
ls.values = make(map[interface{}]interface{})
return nil
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionID get ledis session id
func (ls *SessionStore) SessionID() string {
2015-03-01 04:59:34 +00:00
return ls.sid
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionRelease save session values to ledis
func (ls *SessionStore) SessionRelease(w http.ResponseWriter) {
2015-03-01 04:59:34 +00:00
b, err := session.EncodeGob(ls.values)
if err != nil {
return
}
c.Set([]byte(ls.sid), b)
c.Expire([]byte(ls.sid), ls.maxlifetime)
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// Provider ledis session provider
type Provider struct {
2015-03-01 04:59:34 +00:00
maxlifetime int64
savePath string
db int
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionInit init ledis session
2014-10-27 05:56:00 +00:00
// savepath like ledis server saveDataPath,pool size
// e.g. 127.0.0.1:6379,100,astaxie
2015-09-12 14:53:55 +00:00
func (lp *Provider) SessionInit(maxlifetime int64, savePath string) error {
2015-03-01 04:59:34 +00:00
var err error
lp.maxlifetime = maxlifetime
configs := strings.Split(savePath, ",")
if len(configs) == 1 {
lp.savePath = configs[0]
} else if len(configs) == 2 {
lp.savePath = configs[0]
lp.db, err = strconv.Atoi(configs[1])
if err != nil {
return err
}
}
cfg := new(config.Config)
cfg.DataDir = lp.savePath
var ledisInstance *ledis.Ledis
ledisInstance, err = ledis.Open(cfg)
2015-03-01 04:59:34 +00:00
if err != nil {
return err
2015-03-01 04:59:34 +00:00
}
c, err = ledisInstance.Select(lp.db)
return err
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionRead read ledis session by sid
func (lp *Provider) SessionRead(sid string) (session.Store, error) {
var (
kv map[interface{}]interface{}
err error
)
2017-04-30 15:38:48 +00:00
kvs, _ := c.Get([]byte(sid))
2015-03-01 04:59:34 +00:00
if len(kvs) == 0 {
kv = make(map[interface{}]interface{})
} else {
if kv, err = session.DecodeGob(kvs); err != nil {
2015-03-01 04:59:34 +00:00
return nil, err
}
}
2015-09-12 14:53:55 +00:00
ls := &SessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
2015-03-01 04:59:34 +00:00
return ls, nil
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionExist check ledis session exist by sid
func (lp *Provider) SessionExist(sid string) bool {
2015-03-01 04:59:34 +00:00
count, _ := c.Exists([]byte(sid))
return count != 0
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionRegenerate generate new sid for ledis session
func (lp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
2015-03-01 04:59:34 +00:00
count, _ := c.Exists([]byte(sid))
if count == 0 {
// oldsid doesn't exists, set the new sid directly
// ignore error here, since if it return error
// the existed value will be 0
c.Set([]byte(sid), []byte(""))
c.Expire([]byte(sid), lp.maxlifetime)
} else {
data, _ := c.Get([]byte(oldsid))
c.Set([]byte(sid), data)
c.Expire([]byte(sid), lp.maxlifetime)
}
return lp.SessionRead(sid)
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionDestroy delete ledis session by id
func (lp *Provider) SessionDestroy(sid string) error {
2015-03-01 04:59:34 +00:00
c.Del([]byte(sid))
return nil
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionGC Impelment method, no used.
func (lp *Provider) SessionGC() {
2014-10-27 05:56:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionAll return all active session
func (lp *Provider) SessionAll() int {
2015-03-01 04:59:34 +00:00
return 0
2014-10-27 05:56:00 +00:00
}
func init() {
2015-03-01 04:59:34 +00:00
session.Register("ledis", ledispder)
2014-10-27 05:56:00 +00:00
}