Skip to content

Commit

Permalink
fix the session memcache bug
Browse files Browse the repository at this point in the history
  • Loading branch information
astaxie committed May 28, 2015
1 parent 3abd017 commit db06e95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions session/memcache/sess_memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
}
}
item, err := client.Get(sid)
if err != nil {
return nil, err
if err != nil && err == memcache.ErrCacheMiss {
rs := &MemcacheSessionStore{sid: sid, values: make(map[interface{}]interface{}), maxlifetime: rp.maxlifetime}
return rs, nil
}
var kv map[interface{}]interface{}
if len(item.Value) == 0 {
Expand All @@ -141,7 +142,6 @@ func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
return nil, err
}
}

rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
return rs, nil
}
Expand Down
12 changes: 8 additions & 4 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
return nil, errs
}
session, err = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.config.CookieName,
cookie = &http.Cookie{
Name: manager.config.CookieName,
Value: url.QueryEscape(sid),
Path: "/",
HttpOnly: true,
Secure: manager.isSecure(r),
Domain: manager.config.Domain}
Domain: manager.config.Domain,
}
if manager.config.CookieLifeTime > 0 {
cookie.MaxAge = manager.config.CookieLifeTime
cookie.Expires = time.Now().Add(time.Duration(manager.config.CookieLifeTime) * time.Second)
Expand All @@ -170,12 +172,14 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
return nil, err
}
session, err = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.config.CookieName,
cookie = &http.Cookie{
Name: manager.config.CookieName,
Value: url.QueryEscape(sid),
Path: "/",
HttpOnly: true,
Secure: manager.isSecure(r),
Domain: manager.config.Domain}
Domain: manager.config.Domain,
}
if manager.config.CookieLifeTime > 0 {
cookie.MaxAge = manager.config.CookieLifeTime
cookie.Expires = time.Now().Add(time.Duration(manager.config.CookieLifeTime) * time.Second)
Expand Down

0 comments on commit db06e95

Please sign in to comment.