1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 12:10:55 +00:00
This commit is contained in:
astaxie 2013-11-05 23:41:01 +08:00
parent c8f86652a3
commit bca6a33325

View File

@ -58,21 +58,12 @@ func (rs *RedisSessionStore) SessionID() string {
func (rs *RedisSessionStore) SessionRelease() { func (rs *RedisSessionStore) SessionRelease() {
defer rs.c.Close() defer rs.c.Close()
keys, err := redis.Values(rs.c.Do("HKEYS", rs.sid))
if err == nil {
for _, key := range keys {
if val, ok := rs.values[key]; ok {
rs.c.Do("HSET", rs.sid, key, val)
rs.Delete(key)
} else {
rs.c.Do("HDEL", rs.sid, key)
}
}
}
if len(rs.values) > 0 { if len(rs.values) > 0 {
for k, v := range rs.values { b, err := encodeGob(rs.values)
rs.c.Do("HSET", rs.sid, k, v) if err != nil {
return
} }
rs.c.Do("SET", rs.sid, string(b))
} }
} }
@ -123,31 +114,27 @@ 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 str, err := redis.String(c.Do("GET", sid)); err != nil || str == "" { if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
if str, err := redis.String(c.Do("HGET", sid, sid)); err != nil || str == "" { c.Do("SET", sid)
//c.Do("SET", sid, sid, rp.maxlifetime)
c.Do("HSET", sid, sid, rp.maxlifetime)
} }
c.Do("EXPIRE", sid, rp.maxlifetime) c.Do("EXPIRE", sid, rp.maxlifetime)
kvs, err := redis.Values(c.Do("HGETALL", sid)) kvs, err := redis.String(c.Do("GET", sid))
vals := make(map[interface{}]interface{}) var kv map[interface{}]interface{}
var key interface{} if len(kvs) == 0 {
if err == nil { kv = make(map[interface{}]interface{})
for k, v := range kvs { } else {
if k%2 == 0 { kv, err = decodeGob([]byte(kvs))
key = v if err != nil {
} else { return nil, err
vals[key] = v
}
} }
} }
rs := &RedisSessionStore{c: c, sid: sid, values: vals} rs := &RedisSessionStore{c: c, sid: sid, values: kv}
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()
if str, err := redis.String(c.Do("HGET", sid, sid)); err != nil || str == "" { if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
return false return false
} else { } else {
return true return true
@ -156,24 +143,22 @@ 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 str, err := redis.String(c.Do("HGET", oldsid, oldsid)); err != nil || str == "" { if existed, err := redis.Int(c.Do("EXISTS", oldsid)); err != nil || existed == 0 {
c.Do("HSET", oldsid, oldsid, rp.maxlifetime) 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.Values(c.Do("HGETALL", sid)) kvs, err := redis.String(c.Do("GET", sid))
vals := make(map[interface{}]interface{}) var kv map[interface{}]interface{}
var key interface{} if len(kvs) == 0 {
if err == nil { kv = make(map[interface{}]interface{})
for k, v := range kvs { } else {
if k%2 == 0 { kv, err = decodeGob([]byte(kvs))
key = v if err != nil {
} else { return nil, err
vals[key] = v
}
} }
} }
rs := &RedisSessionStore{c: c, sid: sid, values: vals} rs := &RedisSessionStore{c: c, sid: sid, values: kv}
return rs, nil return rs, nil
} }