From 34eff4cc1fd084c100266ed83856c2aa9731857c Mon Sep 17 00:00:00 2001 From: Pengfei Xue Date: Sat, 25 Jan 2014 10:55:49 +0800 Subject: [PATCH] bugfix, delete the sid if it's values is empty * regenerate sid, if the old key doesn't exists, set the new one directly --- session/sess_redis.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/session/sess_redis.go b/session/sess_redis.go index 0326c6c2..b05e4831 100644 --- a/session/sess_redis.go +++ b/session/sess_redis.go @@ -60,14 +60,15 @@ func (rs *RedisSessionStore) SessionID() string { } func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) { - // if rs.values is empty, return directly - if len(rs.values) < 1 { - return - } - c := rs.p.Get() defer c.Close() + // if rs.values is empty, return directly + if len(rs.values) < 1 { + c.Do("DEL", rs.sid) + return + } + b, err := encodeGob(rs.values) if err != nil { return @@ -155,8 +156,15 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er c := rp.poollist.Get() defer c.Close() - c.Do("RENAME", oldsid, sid) - c.Do("EXPIRE", sid, rp.maxlifetime) + if existed, _ := redis.Int(c.Do("EXISTS", oldsid)); existed == 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.Do("SET", sid, "", "EX", rp.maxlifetime) + } else { + c.Do("RENAME", oldsid, sid) + c.Do("EXPIRE", sid, rp.maxlifetime) + } kvs, err := redis.String(c.Do("GET", sid)) var kv map[interface{}]interface{}