mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 16:50:54 +00:00
fix bug, redis session doesnt work
This commit is contained in:
parent
38f93a7ba9
commit
1509a6b681
@ -16,7 +16,7 @@ var MAX_POOL_SIZE = 100
|
|||||||
var redisPool chan redis.Conn
|
var redisPool chan redis.Conn
|
||||||
|
|
||||||
type RedisSessionStore struct {
|
type RedisSessionStore struct {
|
||||||
c redis.Conn
|
p *redis.Pool
|
||||||
sid string
|
sid string
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
values map[interface{}]interface{}
|
values map[interface{}]interface{}
|
||||||
@ -60,13 +60,15 @@ func (rs *RedisSessionStore) SessionID() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
|
func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||||
defer rs.c.Close()
|
c := rs.p.Get()
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
b, err := encodeGob(rs.values)
|
b, err := encodeGob(rs.values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rs.c.Do("SET", rs.sid, string(b))
|
|
||||||
rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime)
|
c.Do("SET", rs.sid, string(b), "EX", rs.maxlifetime)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisProvider struct {
|
type RedisProvider struct {
|
||||||
@ -116,10 +118,8 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
|||||||
|
|
||||||
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
|
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist.Get()
|
||||||
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
|
defer c.Close()
|
||||||
c.Do("SET", sid)
|
|
||||||
}
|
|
||||||
c.Do("EXPIRE", sid, rp.maxlifetime)
|
|
||||||
kvs, err := redis.String(c.Do("GET", sid))
|
kvs, err := redis.String(c.Do("GET", sid))
|
||||||
var kv map[interface{}]interface{}
|
var kv map[interface{}]interface{}
|
||||||
if len(kvs) == 0 {
|
if len(kvs) == 0 {
|
||||||
@ -130,13 +130,15 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rs := &RedisSessionStore{c: c, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
|
||||||
|
rs := &RedisSessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||||
return rs, nil
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *RedisProvider) SessionExist(sid string) bool {
|
func (rp *RedisProvider) SessionExist(sid string) bool {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist.Get()
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
|
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
@ -146,11 +148,11 @@ func (rp *RedisProvider) SessionExist(sid string) bool {
|
|||||||
|
|
||||||
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist.Get()
|
||||||
if existed, err := redis.Int(c.Do("EXISTS", oldsid)); err != nil || existed == 0 {
|
defer c.Close()
|
||||||
c.Do("SET", oldsid)
|
|
||||||
}
|
|
||||||
c.Do("RENAME", oldsid, sid)
|
c.Do("RENAME", oldsid, sid)
|
||||||
c.Do("EXPIRE", sid, rp.maxlifetime)
|
c.Do("EXPIRE", sid, rp.maxlifetime)
|
||||||
|
|
||||||
kvs, err := redis.String(c.Do("GET", sid))
|
kvs, err := redis.String(c.Do("GET", sid))
|
||||||
var kv map[interface{}]interface{}
|
var kv map[interface{}]interface{}
|
||||||
if len(kvs) == 0 {
|
if len(kvs) == 0 {
|
||||||
@ -161,13 +163,15 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rs := &RedisSessionStore{c: c, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
|
||||||
|
rs := &RedisSessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||||
return rs, nil
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *RedisProvider) SessionDestroy(sid string) error {
|
func (rp *RedisProvider) SessionDestroy(sid string) error {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist.Get()
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
c.Do("DEL", sid)
|
c.Do("DEL", sid)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -178,7 +182,6 @@ func (rp *RedisProvider) SessionGC() {
|
|||||||
|
|
||||||
//@todo
|
//@todo
|
||||||
func (rp *RedisProvider) SessionAll() int {
|
func (rp *RedisProvider) SessionAll() int {
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user