update sessionRelease
1. mysql fix last access time not update 2. mysql & redid Release when data is empty 3. add maxlifetime distinct Gclifetime
Showing
4 changed files
with
19 additions
and
22 deletions
| ... | @@ -34,10 +34,3 @@ More info [beego.me](http://beego.me) | ... | @@ -34,10 +34,3 @@ More info [beego.me](http://beego.me) |
| 34 | 34 | ||
| 35 | beego is licensed under the Apache Licence, Version 2.0 | 35 | beego is licensed under the Apache Licence, Version 2.0 |
| 36 | (http://www.apache.org/licenses/LICENSE-2.0.html). | 36 | (http://www.apache.org/licenses/LICENSE-2.0.html). |
| 37 | |||
| 38 | |||
| 39 | ## Use case | ||
| 40 | |||
| 41 | - Displaying API documentation: [gowalker](https://github.com/Unknwon/gowalker) | ||
| 42 | - seocms: [seocms](https://github.com/chinakr/seocms) | ||
| 43 | - CMS: [toropress](https://github.com/insionng/toropress) | ... | ... |
| ... | @@ -63,13 +63,13 @@ func (st *MysqlSessionStore) SessionID() string { | ... | @@ -63,13 +63,13 @@ func (st *MysqlSessionStore) SessionID() string { |
| 63 | 63 | ||
| 64 | func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) { | 64 | func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) { |
| 65 | defer st.c.Close() | 65 | defer st.c.Close() |
| 66 | if len(st.values) > 0 { | 66 | b, err := encodeGob(st.values) |
| 67 | b, err := encodeGob(st.values) | 67 | if err != nil { |
| 68 | if err != nil { | 68 | return |
| 69 | return | ||
| 70 | } | ||
| 71 | st.c.Exec("UPDATE session set `session_data`= ? where session_key=?", b, st.sid) | ||
| 72 | } | 69 | } |
| 70 | st.c.Exec("UPDATE session set `session_data`=?, `session_expiry`=? where session_key=?", | ||
| 71 | b, time.Now().Unix(), st.sid) | ||
| 72 | |||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | type MysqlProvider struct { | 75 | type MysqlProvider struct { |
| ... | @@ -97,7 +97,8 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) { | ... | @@ -97,7 +97,8 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) { |
| 97 | var sessiondata []byte | 97 | var sessiondata []byte |
| 98 | err := row.Scan(&sessiondata) | 98 | err := row.Scan(&sessiondata) |
| 99 | if err == sql.ErrNoRows { | 99 | if err == sql.ErrNoRows { |
| 100 | c.Exec("insert into session(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", sid, "", time.Now().Unix()) | 100 | c.Exec("insert into session(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", |
| 101 | sid, "", time.Now().Unix()) | ||
| 101 | } | 102 | } |
| 102 | var kv map[interface{}]interface{} | 103 | var kv map[interface{}]interface{} |
| 103 | if len(sessiondata) == 0 { | 104 | if len(sessiondata) == 0 { | ... | ... |
| ... | @@ -61,14 +61,12 @@ func (rs *RedisSessionStore) SessionID() string { | ... | @@ -61,14 +61,12 @@ func (rs *RedisSessionStore) SessionID() string { |
| 61 | 61 | ||
| 62 | func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) { | 62 | func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) { |
| 63 | defer rs.c.Close() | 63 | defer rs.c.Close() |
| 64 | if len(rs.values) > 0 { | 64 | b, err := encodeGob(rs.values) |
| 65 | b, err := encodeGob(rs.values) | 65 | if err != nil { |
| 66 | if err != nil { | 66 | return |
| 67 | return | ||
| 68 | } | ||
| 69 | rs.c.Do("SET", rs.sid, string(b)) | ||
| 70 | rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime) | ||
| 71 | } | 67 | } |
| 68 | rs.c.Do("SET", rs.sid, string(b)) | ||
| 69 | rs.c.Do("EXPIRE", rs.sid, rs.maxlifetime) | ||
| 72 | } | 70 | } |
| 73 | 71 | ||
| 74 | type RedisProvider struct { | 72 | type RedisProvider struct { | ... | ... |
| ... | @@ -52,6 +52,7 @@ type managerConfig struct { | ... | @@ -52,6 +52,7 @@ type managerConfig struct { |
| 52 | CookieName string `json:"cookieName"` | 52 | CookieName string `json:"cookieName"` |
| 53 | EnableSetCookie bool `json:"enableSetCookie,omitempty"` | 53 | EnableSetCookie bool `json:"enableSetCookie,omitempty"` |
| 54 | Gclifetime int64 `json:"gclifetime"` | 54 | Gclifetime int64 `json:"gclifetime"` |
| 55 | Maxlifetime int64 `json:"maxLifetime"` | ||
| 55 | Maxage int `json:"maxage"` | 56 | Maxage int `json:"maxage"` |
| 56 | Secure bool `json:"secure"` | 57 | Secure bool `json:"secure"` |
| 57 | SessionIDHashFunc string `json:"sessionIDHashFunc"` | 58 | SessionIDHashFunc string `json:"sessionIDHashFunc"` |
| ... | @@ -81,7 +82,11 @@ func NewManager(provideName, config string) (*Manager, error) { | ... | @@ -81,7 +82,11 @@ func NewManager(provideName, config string) (*Manager, error) { |
| 81 | if err != nil { | 82 | if err != nil { |
| 82 | return nil, err | 83 | return nil, err |
| 83 | } | 84 | } |
| 84 | provider.SessionInit(cf.Gclifetime, cf.ProviderConfig) | 85 | if cf.Maxlifetime == 0 { |
| 86 | cf.Maxlifetime = cf.Gclifetime | ||
| 87 | } | ||
| 88 | |||
| 89 | provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig) | ||
| 85 | 90 | ||
| 86 | if cf.SessionIDHashFunc == "" { | 91 | if cf.SessionIDHashFunc == "" { |
| 87 | cf.SessionIDHashFunc = "sha1" | 92 | cf.SessionIDHashFunc = "sha1" | ... | ... |
-
Please register or sign in to post a comment