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

update sessionRelease

1. mysql fix last access time not update
2. mysql & redid Release when data is empty
3. add maxlifetime distinct Gclifetime
This commit is contained in:
astaxie 2014-01-08 20:54:20 +08:00
parent b766f65c26
commit aa2fef0d36
4 changed files with 19 additions and 22 deletions

View File

@ -34,10 +34,3 @@ More info [beego.me](http://beego.me)
beego is licensed under the Apache Licence, Version 2.0 beego is licensed under the Apache Licence, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0.html). (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)

View File

@ -63,13 +63,13 @@ func (st *MysqlSessionStore) SessionID() string {
func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) { func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
defer st.c.Close() defer st.c.Close()
if len(st.values) > 0 {
b, err := encodeGob(st.values) b, err := encodeGob(st.values)
if err != nil { if err != nil {
return return
} }
st.c.Exec("UPDATE session set `session_data`= ? where session_key=?", b, st.sid) st.c.Exec("UPDATE session set `session_data`=?, `session_expiry`=? where session_key=?",
} b, time.Now().Unix(), st.sid)
} }
type MysqlProvider struct { type MysqlProvider struct {
@ -97,7 +97,8 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) {
var sessiondata []byte var sessiondata []byte
err := row.Scan(&sessiondata) err := row.Scan(&sessiondata)
if err == sql.ErrNoRows { 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{} var kv map[interface{}]interface{}
if len(sessiondata) == 0 { if len(sessiondata) == 0 {

View File

@ -61,7 +61,6 @@ func (rs *RedisSessionStore) SessionID() string {
func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) { func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
defer rs.c.Close() defer rs.c.Close()
if len(rs.values) > 0 {
b, err := encodeGob(rs.values) b, err := encodeGob(rs.values)
if err != nil { if err != nil {
return return
@ -69,7 +68,6 @@ func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
rs.c.Do("SET", rs.sid, string(b)) rs.c.Do("SET", rs.sid, string(b))
rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime) rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime)
} }
}
type RedisProvider struct { type RedisProvider struct {
maxlifetime int64 maxlifetime int64

View File

@ -52,6 +52,7 @@ type managerConfig struct {
CookieName string `json:"cookieName"` CookieName string `json:"cookieName"`
EnableSetCookie bool `json:"enableSetCookie,omitempty"` EnableSetCookie bool `json:"enableSetCookie,omitempty"`
Gclifetime int64 `json:"gclifetime"` Gclifetime int64 `json:"gclifetime"`
Maxlifetime int64 `json:"maxLifetime"`
Maxage int `json:"maxage"` Maxage int `json:"maxage"`
Secure bool `json:"secure"` Secure bool `json:"secure"`
SessionIDHashFunc string `json:"sessionIDHashFunc"` SessionIDHashFunc string `json:"sessionIDHashFunc"`
@ -81,7 +82,11 @@ func NewManager(provideName, config string) (*Manager, error) {
if err != nil { if err != nil {
return nil, err 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 == "" { if cf.SessionIDHashFunc == "" {
cf.SessionIDHashFunc = "sha1" cf.SessionIDHashFunc = "sha1"