1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 19:41:00 +00:00

fix the session memcache bug

This commit is contained in:
astaxie 2015-05-28 12:04:19 +08:00
parent 3abd01799d
commit db06e954b5
2 changed files with 11 additions and 7 deletions

View File

@ -129,8 +129,9 @@ func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
} }
} }
item, err := client.Get(sid) item, err := client.Get(sid)
if err != nil { if err != nil && err == memcache.ErrCacheMiss {
return nil, err rs := &MemcacheSessionStore{sid: sid, values: make(map[interface{}]interface{}), maxlifetime: rp.maxlifetime}
return rs, nil
} }
var kv map[interface{}]interface{} var kv map[interface{}]interface{}
if len(item.Value) == 0 { if len(item.Value) == 0 {
@ -141,7 +142,6 @@ func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
return nil, err return nil, err
} }
} }
rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime} rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
return rs, nil return rs, nil
} }

View File

@ -143,12 +143,14 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
return nil, errs return nil, errs
} }
session, err = manager.provider.SessionRead(sid) session, err = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.config.CookieName, cookie = &http.Cookie{
Name: manager.config.CookieName,
Value: url.QueryEscape(sid), Value: url.QueryEscape(sid),
Path: "/", Path: "/",
HttpOnly: true, HttpOnly: true,
Secure: manager.isSecure(r), Secure: manager.isSecure(r),
Domain: manager.config.Domain} Domain: manager.config.Domain,
}
if manager.config.CookieLifeTime > 0 { if manager.config.CookieLifeTime > 0 {
cookie.MaxAge = manager.config.CookieLifeTime cookie.MaxAge = manager.config.CookieLifeTime
cookie.Expires = time.Now().Add(time.Duration(manager.config.CookieLifeTime) * time.Second) cookie.Expires = time.Now().Add(time.Duration(manager.config.CookieLifeTime) * time.Second)
@ -170,12 +172,14 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
return nil, err return nil, err
} }
session, err = manager.provider.SessionRead(sid) session, err = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.config.CookieName, cookie = &http.Cookie{
Name: manager.config.CookieName,
Value: url.QueryEscape(sid), Value: url.QueryEscape(sid),
Path: "/", Path: "/",
HttpOnly: true, HttpOnly: true,
Secure: manager.isSecure(r), Secure: manager.isSecure(r),
Domain: manager.config.Domain} Domain: manager.config.Domain,
}
if manager.config.CookieLifeTime > 0 { if manager.config.CookieLifeTime > 0 {
cookie.MaxAge = manager.config.CookieLifeTime cookie.MaxAge = manager.config.CookieLifeTime
cookie.Expires = time.Now().Add(time.Duration(manager.config.CookieLifeTime) * time.Second) cookie.Expires = time.Now().Add(time.Duration(manager.config.CookieLifeTime) * time.Second)