Fix section "integrating sessions" in 14.2.md

This commit is contained in:
Anchor
2015-02-11 22:39:25 -08:00
committed by James Miranda
parent cd1cd755ea
commit 8242f00f71

View File

@@ -2,18 +2,19 @@
In chapter 6, we introduced some basic concepts pertaining to sessions in Go, and we implemented a sessions manager. The Beego framework uses this session manager to implement some convenient session handling functionality. In chapter 6, we introduced some basic concepts pertaining to sessions in Go, and we implemented a sessions manager. The Beego framework uses this session manager to implement some convenient session handling functionality.
## Session integration ## Integrating sessions
beego mainly in the following global variables to control the session handling:
Beego handles sessions mainly according to the following global variables:
// related to session // related to session
SessionOn bool // whether to open the session module, the default is not open SessionOn bool // whether or not to open the session module. Defaults to false.
SessionProvider string // session backend processing module provided, the default is sessionManager supported memory SessionProvider string // the desired session backend processing module. Defaults to an in-memory sessionManager
SessionName string // client name saved in cookies SessionName string // the name of the client saved cookies
SessionGCMaxLifetime int64 // cookies validity SessionGCMaxLifetime int64 // cookie validity
GlobalSessions *session.Manager// global session controller GlobalSessions *session.Manager// global session controller
Of course, the above values of these variables need to be initialized, you can also follow the code to match the configuration file to set these values: Of course, the above values of these variables need to be initialized. You can also use the values from the following configuration file code to set these values:
if ar, err := AppConfig.Bool("sessionon"); err != nil { if ar, err := AppConfig.Bool("sessionon"); err != nil {
SessionOn = false SessionOn = false
@@ -37,17 +38,19 @@ Of course, the above values of these variables need to be initialized, you can a
SessionGCMaxLifetime = 3600 SessionGCMaxLifetime = 3600
} }
In beego.Run function to add the following code: Add the following code in the `beego.Run` function:
if SessionOn { if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime) GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
go GlobalSessions.GC() go GlobalSessions.GC()
} }
So long SessionOn set to true, then it will open the session by default function to open an independent goroutine to handle session. As long as `SessionOn` is set to true, it will open the session by default with an independent goroutine session handler
In order to facilitate our custom Controller quickly using session, the author `beego.Controller` provides the following methods: In order to facilitate our custom Controller quickly using session, the author `beego.Controller` provides the following methods:
To assist us in quickly using sessions in a custom Controller, `beego.Controller` provides the following method:
func (c *Controller) StartSession() (sess session.Session) { func (c *Controller) StartSession() (sess session.Session) {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request) sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
return return