Skip to content

Commit

Permalink
Fixed# add a clear method to sessionMgr
Browse files Browse the repository at this point in the history
  • Loading branch information
Q1mi committed Jul 8, 2019
1 parent 9bbc5d7 commit 28ee0d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func main(){

### memory_based:

warning: this is just for test!

```go

package main
Expand Down
7 changes: 7 additions & 0 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,10 @@ func (m *MemSessionMgr) CreateSession() (sd Session) {
m.session[sd.ID()] = sd
return
}

// Clear delete the request session in sessionMgr
func (m *MemSessionMgr)Clear(sessionID string){
m.rwLock.Lock()
defer m.rwLock.Unlock()
delete(m.session, sessionID)
}
7 changes: 7 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,10 @@ func (r *redisSessionMgr) CreateSession() (sd Session) {
r.session[sd.ID()] = sd
return
}

// Clear delete the request session in sessionMgr
func (r *redisSessionMgr) Clear(sessionID string){
r.rwLock.Lock()
defer r.rwLock.Unlock()
delete(r.session, sessionID)
}
4 changes: 3 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type SessionMgr interface {
Init(addr string, options ...string) error // init the session store
GetSession(string) (Session, error) // get the session by sessionID
CreateSession() (Session) // create a new session
Clear(string) // clear the request session data
}

// Options Cookie Options
Expand Down Expand Up @@ -65,7 +66,7 @@ func SessionMiddleware(sm SessionMgr, options Options) gin.HandlerFunc {
// so next handlerFunc can get the session by c.Get(SessionContextName)
var session Session
// try to get sessionID from cookie
sessionID, err := c.Cookie("session_id")
sessionID, err := c.Cookie(SessionCookieName)
if err != nil {
// can't get sessionID from Cookie, need to create a new session
log.Printf("get session_id from Cookie failed,err:%v\n", err)
Expand All @@ -85,6 +86,7 @@ func SessionMiddleware(sm SessionMgr, options Options) gin.HandlerFunc {
c.Set(SessionContextName, session)
// must write cookie before handlerFunc return
c.SetCookie(SessionCookieName, sessionID, options.MaxAge, options.Path, options.Domain, options.Secure, options.HttpOnly)
defer sm.Clear(sessionID)
c.Next()
}
}

0 comments on commit 28ee0d4

Please sign in to comment.