From aa2fef0d36151297fe0f70480353d8fd45b9700f Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 8 Jan 2014 20:54:20 +0800 Subject: [PATCH] update sessionRelease 1. mysql fix last access time not update 2. mysql & redid Release when data is empty 3. add maxlifetime distinct Gclifetime --- README.md | 7 ------- session/sess_mysql.go | 15 ++++++++------- session/sess_redis.go | 12 +++++------- session/session.go | 7 ++++++- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8d010eb9..00ad468d 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,3 @@ More info [beego.me](http://beego.me) beego is licensed under the Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html). - - -## Use case - -- Displaying API documentation: [gowalker](https://github.com/Unknwon/gowalker) -- seocms: [seocms](https://github.com/chinakr/seocms) -- CMS: [toropress](https://github.com/insionng/toropress) diff --git a/session/sess_mysql.go b/session/sess_mysql.go index 1101e437..3b0c6f3f 100644 --- a/session/sess_mysql.go +++ b/session/sess_mysql.go @@ -63,13 +63,13 @@ func (st *MysqlSessionStore) SessionID() string { func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) { defer st.c.Close() - if len(st.values) > 0 { - b, err := encodeGob(st.values) - if err != nil { - return - } - st.c.Exec("UPDATE session set `session_data`= ? where session_key=?", b, st.sid) + b, err := encodeGob(st.values) + if err != nil { + return } + st.c.Exec("UPDATE session set `session_data`=?, `session_expiry`=? where session_key=?", + b, time.Now().Unix(), st.sid) + } type MysqlProvider struct { @@ -97,7 +97,8 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) { var sessiondata []byte err := row.Scan(&sessiondata) if err == sql.ErrNoRows { - c.Exec("insert into session(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", sid, "", time.Now().Unix()) + c.Exec("insert into session(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", + sid, "", time.Now().Unix()) } var kv map[interface{}]interface{} if len(sessiondata) == 0 { diff --git a/session/sess_redis.go b/session/sess_redis.go index 0f8c0308..51685844 100644 --- a/session/sess_redis.go +++ b/session/sess_redis.go @@ -61,14 +61,12 @@ func (rs *RedisSessionStore) SessionID() string { func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) { defer rs.c.Close() - if len(rs.values) > 0 { - b, err := encodeGob(rs.values) - if err != nil { - return - } - rs.c.Do("SET", rs.sid, string(b)) - rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime) + b, err := encodeGob(rs.values) + if err != nil { + return } + rs.c.Do("SET", rs.sid, string(b)) + rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime) } type RedisProvider struct { diff --git a/session/session.go b/session/session.go index df348fab..0447dac7 100644 --- a/session/session.go +++ b/session/session.go @@ -52,6 +52,7 @@ type managerConfig struct { CookieName string `json:"cookieName"` EnableSetCookie bool `json:"enableSetCookie,omitempty"` Gclifetime int64 `json:"gclifetime"` + Maxlifetime int64 `json:"maxLifetime"` Maxage int `json:"maxage"` Secure bool `json:"secure"` SessionIDHashFunc string `json:"sessionIDHashFunc"` @@ -81,7 +82,11 @@ func NewManager(provideName, config string) (*Manager, error) { if err != nil { return nil, err } - provider.SessionInit(cf.Gclifetime, cf.ProviderConfig) + if cf.Maxlifetime == 0 { + cf.Maxlifetime = cf.Gclifetime + } + + provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig) if cf.SessionIDHashFunc == "" { cf.SessionIDHashFunc = "sha1"