1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 18:50:55 +00:00

Reform the ledis_session.go

This commit is contained in:
Meaglith Ma 2015-03-01 12:59:34 +08:00
parent 7ec2a077d9
commit 20cc5b261e

View File

@ -1,14 +1,14 @@
package session package session
import ( import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
) )
var ledispder = &LedisProvider{} var ledispder = &LedisProvider{}
@ -16,165 +16,165 @@ var c *ledis.DB
// ledis session store // ledis session store
type LedisSessionStore struct { type LedisSessionStore struct {
sid string sid string
lock sync.RWMutex lock sync.RWMutex
values map[interface{}]interface{} values map[interface{}]interface{}
maxlifetime int64 maxlifetime int64
} }
// set value in ledis session // set value in ledis session
func (ls *LedisSessionStore) Set(key, value interface{}) error { func (ls *LedisSessionStore) Set(key, value interface{}) error {
ls.lock.Lock() ls.lock.Lock()
defer ls.lock.Unlock() defer ls.lock.Unlock()
ls.values[key] = value ls.values[key] = value
return nil return nil
} }
// get value in ledis session // get value in ledis session
func (ls *LedisSessionStore) Get(key interface{}) interface{} { func (ls *LedisSessionStore) Get(key interface{}) interface{} {
ls.lock.RLock() ls.lock.RLock()
defer ls.lock.RUnlock() defer ls.lock.RUnlock()
if v, ok := ls.values[key]; ok { if v, ok := ls.values[key]; ok {
return v return v
} else { } else {
return nil return nil
} }
} }
// delete value in ledis session // delete value in ledis session
func (ls *LedisSessionStore) Delete(key interface{}) error { func (ls *LedisSessionStore) Delete(key interface{}) error {
ls.lock.Lock() ls.lock.Lock()
defer ls.lock.Unlock() defer ls.lock.Unlock()
delete(ls.values, key) delete(ls.values, key)
return nil return nil
} }
// clear all values in ledis session // clear all values in ledis session
func (ls *LedisSessionStore) Flush() error { func (ls *LedisSessionStore) Flush() error {
ls.lock.Lock() ls.lock.Lock()
defer ls.lock.Unlock() defer ls.lock.Unlock()
ls.values = make(map[interface{}]interface{}) ls.values = make(map[interface{}]interface{})
return nil return nil
} }
// get ledis session id // get ledis session id
func (ls *LedisSessionStore) SessionID() string { func (ls *LedisSessionStore) SessionID() string {
return ls.sid return ls.sid
} }
// save session values to ledis // save session values to ledis
func (ls *LedisSessionStore) SessionRelease(w http.ResponseWriter) { func (ls *LedisSessionStore) SessionRelease(w http.ResponseWriter) {
b, err := session.EncodeGob(ls.values) b, err := session.EncodeGob(ls.values)
if err != nil { if err != nil {
return return
} }
c.Set([]byte(ls.sid), b) c.Set([]byte(ls.sid), b)
c.Expire([]byte(ls.sid), ls.maxlifetime) c.Expire([]byte(ls.sid), ls.maxlifetime)
} }
// ledis session provider // ledis session provider
type LedisProvider struct { type LedisProvider struct {
maxlifetime int64 maxlifetime int64
savePath string savePath string
db int db int
} }
// init ledis session // init ledis session
// savepath like ledis server saveDataPath,pool size // savepath like ledis server saveDataPath,pool size
// e.g. 127.0.0.1:6379,100,astaxie // e.g. 127.0.0.1:6379,100,astaxie
func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error { func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error {
var err error var err error
lp.maxlifetime = maxlifetime lp.maxlifetime = maxlifetime
configs := strings.Split(savePath, ",") configs := strings.Split(savePath, ",")
if len(configs) == 1 { if len(configs) == 1 {
lp.savePath = configs[0] lp.savePath = configs[0]
} else if len(configs) == 2 { } else if len(configs) == 2 {
lp.savePath = configs[0] lp.savePath = configs[0]
lp.db, err = strconv.Atoi(configs[1]) lp.db, err = strconv.Atoi(configs[1])
if err != nil { if err != nil {
return err return err
} }
} }
cfg := new(config.Config) cfg := new(config.Config)
cfg.DataDir = lp.savePath cfg.DataDir = lp.savePath
nowLedis, err := ledis.Open(cfg) nowLedis, err := ledis.Open(cfg)
c, err = nowLedis.Select(lp.db) c, err = nowLedis.Select(lp.db)
if err != nil { if err != nil {
println(err) println(err)
return nil return nil
} }
return nil return nil
} }
// read ledis session by sid // read ledis session by sid
func (lp *LedisProvider) SessionRead(sid string) (session.SessionStore, error) { func (lp *LedisProvider) SessionRead(sid string) (session.SessionStore, error) {
kvs, err := c.Get([]byte(sid)) kvs, err := c.Get([]byte(sid))
var kv map[interface{}]interface{} var kv map[interface{}]interface{}
if len(kvs) == 0 { if len(kvs) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = session.DecodeGob(kvs) kv, err = session.DecodeGob(kvs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
ls := &LedisSessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime} ls := &LedisSessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
return ls, nil return ls, nil
} }
// check ledis session exist by sid // check ledis session exist by sid
func (lp *LedisProvider) SessionExist(sid string) bool { func (lp *LedisProvider) SessionExist(sid string) bool {
count, _ := c.Exists([]byte(sid)) count, _ := c.Exists([]byte(sid))
if count == 0 { if count == 0 {
return false return false
} else { } else {
return true return true
} }
} }
// generate new sid for ledis session // generate new sid for ledis session
func (lp *LedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) { func (lp *LedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
count, _ := c.Exists([]byte(sid)) count, _ := c.Exists([]byte(sid))
if count == 0 { if count == 0 {
// oldsid doesn't exists, set the new sid directly // oldsid doesn't exists, set the new sid directly
// ignore error here, since if it return error // ignore error here, since if it return error
// the existed value will be 0 // the existed value will be 0
c.Set([]byte(sid), []byte("")) c.Set([]byte(sid), []byte(""))
c.Expire([]byte(sid), lp.maxlifetime) c.Expire([]byte(sid), lp.maxlifetime)
} else { } else {
data, _ := c.Get([]byte(oldsid)) data, _ := c.Get([]byte(oldsid))
c.Set([]byte(sid), data) c.Set([]byte(sid), data)
c.Expire([]byte(sid), lp.maxlifetime) c.Expire([]byte(sid), lp.maxlifetime)
} }
kvs, err := c.Get([]byte(sid)) kvs, err := c.Get([]byte(sid))
var kv map[interface{}]interface{} var kv map[interface{}]interface{}
if len(kvs) == 0 { if len(kvs) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = session.DecodeGob([]byte(kvs)) kv, err = session.DecodeGob([]byte(kvs))
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
ls := &LedisSessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime} ls := &LedisSessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
return ls, nil return ls, nil
} }
// delete ledis session by id // delete ledis session by id
func (lp *LedisProvider) SessionDestroy(sid string) error { func (lp *LedisProvider) SessionDestroy(sid string) error {
c.Del([]byte(sid)) c.Del([]byte(sid))
return nil return nil
} }
// Impelment method, no used. // Impelment method, no used.
func (lp *LedisProvider) SessionGC() { func (lp *LedisProvider) SessionGC() {
return return
} }
// @todo // @todo
func (lp *LedisProvider) SessionAll() int { func (lp *LedisProvider) SessionAll() int {
return 0 return 0
} }
func init() { func init() {
session.Register("ledis", ledispder) session.Register("ledis", ledispder)
} }