name: Session control
sort: 5
Session control
Beego has a built-in session module that supports memory, file, mysql, redis, couchbase, memcache and postgres as the save provider. Other providers can be implemented according to the interface.
To use session in Beego switch it on in the main function:
web.BConfig.WebConfig.Session.SessionOn = true
Or it can be activated in the configuration file:
SessionOn = true
After being switched on, session can used be used like this:
func (this *MainController) Get() {v := this.GetSession("asta")if v == nil {this.SetSession("asta", int(1))this.Data["num"] = 0} else {this.SetSession("asta", v.(int)+1)this.Data["num"] = v.(int)}this.TplName = "index.tpl"}
There are several useful methods to handle session:
- SetSession(name string, value interface{})
- GetSession(name string) interface{}
- DelSession(name string)
- SessionRegenerateID()
- DestroySession()
The most commonly used methods are SetSession, GetSession, and DelSession.
Custom logic can also be used:
sess := this.StartSession()defer sess.SessionRelease()
sess object has following methods:
- Set
- Get
- Delete
- SessionID
- SessionRelease
- Flush
SetSession, GetSession and DelSession methods are recommended for session operation as it will release resource automatically.
Here are some parameters used in the Session module:
SessionOn
Enables Session. Default value is
false. Parameter name in configuration file:SessionOnSessionProvider Sets Session provider. Set to
memoryby default.File,mysqlandredisare also supported. Parameter name in configuration file:sessionprovider.SessionName Sets the cookie name. Session is stored in browser’s cookies by default. The default name is beegosessionID. Parameter name in configuration file:
sessionname.SessionGCMaxLifetime Sets the Session expire time. Default value is
3600s. Parameter name in configuration file:sessiongcmaxlifetime.SessionProviderConfig Sets the save path or connection string for file, mysql or redis. Default value is empty. Parameter name in configuration file:
sessionproviderconfig.SessionHashFunc Sets the function used to generate sessionid. The default value is
sha1.SessionCookieLifeTime Sets the cookie expire time. The cookie is used to store data in client.
Package Installation
If you are not using Go modules, manual installation may be required.
Note: Beego >= 1.1.3 removed all dependencies
# Couchbasego get -u github.com/beego/beego/v2/server/web/session/couchbase# Ledisgo get -u github.com/beego/beego/v2/server/web/session/ledis# Memcachego get -u github.com/beego/beego/v2/server/web/session/memcache# MySQLgo get -u github.com/beego/beego/v2/server/web/session/mysql# Postgresgo get -u github.com/beego/beego/v2/server/web/session/postgres# Redisgo get -u github.com/beego/beego/v2/server/web/session/redis# Redis (cluster mode)go get -u github.com/beego/beego/v2/server/web/session/redis_cluster# Redis (sentinel)go get -u github.com/beego/beego/v2/server/web/session/redis_sentinel# SSDBgo get -u github.com/beego/beego/v2/server/web/session/ssdb
Example Usage
Couchbase
SessionProviderConfig is connection address using couchbase.
// main.gopackage mainimport ("github.com/beego/beego/v2/server/web"_ "github.com/beego/beego/v2/server/web/session/couchbase")func init() {web.BConfig.WebConfig.Session.SessionOn = trueweb.BConfig.WebConfig.Session.SessionProvider = "couchbase"web.BConfig.WebConfig.Session.SessionProviderConfig = "http://bucketname:bucketpass@myserver:8091/"}
File
// main.gopackage mainimport ("github.com/beego/beego/v2/server/web")func init() {web.BConfig.WebConfig.Session.SessionOn = trueweb.BConfig.WebConfig.Session.SessionProvider = "file"web.BConfig.WebConfig.Session.SessionProviderConfig = "/tmp"}
Memcache
SessionProviderConfig is the connection address using memcache.
// main.gopackage mainimport ("github.com/beego/beego/v2/server/web"_ "github.com/beego/beego/v2/server/web/session/memcache")func init() {web.BConfig.WebConfig.Session.SessionOn = trueweb.BConfig.WebConfig.Session.SessionProvider = "memcache"web.BConfig.WebConfig.Session.SessionProviderConfig = "127.0.0.1:7080"}
MySQL
SessionProviderConfig is the connection address using go-sql-driver.
// main.gopackage mainimport ("github.com/beego/beego/v2/server/web"_ "github.com/beego/beego/v2/server/web/session/mysql")func init() {web.BConfig.WebConfig.Session.SessionOn = trueweb.BConfig.WebConfig.Session.SessionProvider = "mysql"web.BConfig.WebConfig.Session.SessionProviderConfig = "username:password@protocol(address)/dbname?param=value"}
Postgres
SessionProviderConfig is the connection address using postgres.
// main.gopackage mainimport ("github.com/beego/beego/v2/server/web"_ "github.com/beego/beego/v2/server/web/session/postgres")func init() {web.BConfig.WebConfig.Session.SessionOn = trueweb.BConfig.WebConfig.Session.SessionProvider = "postgresql"web.BConfig.WebConfig.Session.SessionProviderConfig = "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full"}
Redis
SessionProviderConfig is the connection address using redigo.
// main.gopackage mainimport ("github.com/beego/beego/v2/server/web"_ "github.com/beego/beego/v2/server/web/session/redis")func init() {web.BConfig.WebConfig.Session.SessionOn = trueweb.BConfig.WebConfig.Session.SessionProvider = "redis"web.BConfig.WebConfig.Session.SessionProviderConfig = "127.0.0.1:6379"}
Note:
Session uses gob to register objects. When using a session engine other than memory, objects must be registered in session before they can be used. Use gob.Register() to register them in init() function.
