Chapter VI, when we saw how to use the Go language session, also achieved a sessionManger, beego sessionManager based framework to achieve a convenient session handling functions.
beego mainly in the following global variables to control the session handling:
// related to session
SessionOn bool // whether to open the session module, the default is not open
SessionProvider string // session backend processing module provided, the default is sessionManager supported memory
SessionName string // client name saved in cookies
SessionGCMaxLifetime int64 // cookies validity
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:
if ar, err := AppConfig.Bool("sessionon"); err != nil {
SessionOn = false
} else {
SessionOn = ar
}
if ar := AppConfig.String("sessionprovider"); ar == "" {
SessionProvider = "memory"
} else {
SessionProvider = ar
}
if ar := AppConfig.String("sessionname"); ar == "" {
SessionName = "beegosessionID"
} else {
SessionName = ar
}
if ar, err := AppConfig.Int("sessiongcmaxlifetime"); err != nil && ar != 0 {
int64val, _ := strconv.ParseInt(strconv.Itoa(ar), 10, 64)
SessionGCMaxLifetime = int64val
} else {
SessionGCMaxLifetime = 3600
}
In beego.Run function to add the following code:
if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
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.
In order to facilitate our custom Controller quickly using session, the author beego.Controller
provides the following methods:
func (c *Controller) StartSession() (sess session.Session) {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
return
}
Through the above code we can see, beego framework simply inherit the session function, then how to use it in your project ?
First, we need to apply the main entrance open session:
beego.SessionOn = true
We can then corresponding method in the controller to use the session as follows: the
func (this *MainController) Get() {
var intcount int
sess := this.StartSession()
count := sess.Get("count")
if count == nil {
intcount = 0
} else {
intcount = count.(int)
}
intcount = intcount + 1
sess.Set("count", intcount)
this.Data["Username"] = "astaxie"
this.Data["Email"] = "[email protected]"
this.Data["Count"] = intcount
this.TplNames = "index.tpl"
}
The above code shows how to use the control logic session, mainly divided into two steps:
-
Get session object
// Get the object, similar in PHP session_start() sess:= this.StartSession()
-
to use the session for general session value operation
// Get the session values , similar in PHP $ _SESSION ["count"] sess.Get("count") // Set the session value sess.Set("count", intcount)
As can be seen from the above code beego framework based applications developed using the session quite easy, basically, and PHP to call session_start()
similar.
- Directory
- Previous section: Static files
- Next section: Form