Merging other languages

This commit is contained in:
James Miranda
2016-09-23 18:01:10 -03:00
parent 380a8ee74c
commit de3c5bdaa4
490 changed files with 24539 additions and 24588 deletions

View File

@@ -1,103 +1,105 @@
# 14.2 Session支持
第六章的时候我们介绍过如何在Go语言中使用session也实现了一个sessionMangerbeego框架基于sessionManager实现了方便的session处理功能。
## session集成
beego中主要有以下的全局变量来控制session处理
//related to session
SessionOn bool // 是否开启session模块默认不开启
SessionProvider string // session后端提供处理模块默认是sessionManager支持的memory
SessionName string // 客户端保存的cookies的名称
SessionGCMaxLifetime int64 // cookies有效期
GlobalSessions *session.Manager //全局session控制器
当然上面这些变量需要初始化值,也可以按照下面的代码来配合配置文件以设置这些值:
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
}
在beego.Run函数中增加如下代码
if SessionOn {
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
go GlobalSessions.GC()
}
这样只要SessionOn设置为true那么就会默认开启session功能独立开一个goroutine来处理session。
为了方便我们在自定义Controller中快速使用session作者在`beego.Controller`中提供了如下方法:
func (c *Controller) StartSession() (sess session.Session) {
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
return
}
## session使用
通过上面的代码我们可以看到beego框架简单地继承了session功能那么在项目中如何使用呢
首先我们需要在应用的main入口处开启session
beego.SessionOn = true
然后我们就可以在控制器的相应方法中如下所示的使用session了
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"] = "astaxie@gmail.com"
this.Data["Count"] = intcount
this.TplNames = "index.tpl"
}
上面的代码展示了如何在控制逻辑中使用session主要分两个步骤
1. 获取session对象
//获取对象,类似PHP中的session_start()
sess := this.StartSession()
2. 使用session进行一般的session值操作
//获取session值类似PHP中的$_SESSION["count"]
sess.Get("count")
//设置session值
sess.Set("count", intcount)
从上面代码可以看出基于beego框架开发的应用中使用session相当方便基本上和PHP中调用`session_start()`类似。
## links
* [目录](<preface.md>)
* 上一节: [静态文件支持](<14.1.md>)
* 下一节: [表单及验证支持](<14.3.md>)
# 14.2 Session
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.
## Session integration
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
}
## Session using
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"] = "astaxie@gmail.com"
this.Data["Count"] = intcount
this.TplNames = "index.tpl"
}
The above code shows how to use the control logic session, mainly divided into two steps:
1. Get session object
// Get the object, similar in PHP session_start()
sess:= this.StartSession()
2. 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.
## Links
- [Directory](preface.md)
- Previous section: [Static files](14.1.md)
- Next section: [Form](14.3.md)