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

#369 set maxlifetime, close conn

This commit is contained in:
slene 2013-12-14 23:29:17 +08:00
parent 495033b977
commit d48b7e0101

View File

@ -15,10 +15,11 @@ var MAX_POOL_SIZE = 100
var redisPool chan redis.Conn var redisPool chan redis.Conn
type RedisSessionStore struct { type RedisSessionStore struct {
c redis.Conn c redis.Conn
sid string sid string
lock sync.RWMutex lock sync.RWMutex
values map[interface{}]interface{} values map[interface{}]interface{}
maxlifetime int64
} }
func (rs *RedisSessionStore) Set(key, value interface{}) error { func (rs *RedisSessionStore) Set(key, value interface{}) error {
@ -65,6 +66,7 @@ func (rs *RedisSessionStore) SessionRelease() {
return return
} }
rs.c.Do("SET", rs.sid, string(b)) rs.c.Do("SET", rs.sid, string(b))
rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime)
} }
} }
@ -129,12 +131,13 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
return nil, err return nil, err
} }
} }
rs := &RedisSessionStore{c: c, sid: sid, values: kv} rs := &RedisSessionStore{c: c, 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()
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 {
@ -159,12 +162,13 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
return nil, err return nil, err
} }
} }
rs := &RedisSessionStore{c: c, sid: sid, values: kv} rs := &RedisSessionStore{c: c, 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()
c.Do("DEL", sid) c.Do("DEL", sid)
return nil return nil
} }