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:

  1. web.BConfig.WebConfig.Session.SessionOn = true

Or it can be activated in the configuration file:

  1. SessionOn = true

After being switched on, session can used be used like this:

  1. func (this *MainController) Get() {
  2. v := this.GetSession("asta")
  3. if v == nil {
  4. this.SetSession("asta", int(1))
  5. this.Data["num"] = 0
  6. } else {
  7. this.SetSession("asta", v.(int)+1)
  8. this.Data["num"] = v.(int)
  9. }
  10. this.TplName = "index.tpl"
  11. }

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:

  1. sess := this.StartSession()
  2. 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: SessionOn

  • SessionProvider Sets Session provider. Set to memory by default. File, mysql and redis are 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

  1. # Couchbase
  2. go get -u github.com/beego/beego/v2/server/web/session/couchbase
  3. # Ledis
  4. go get -u github.com/beego/beego/v2/server/web/session/ledis
  5. # Memcache
  6. go get -u github.com/beego/beego/v2/server/web/session/memcache
  7. # MySQL
  8. go get -u github.com/beego/beego/v2/server/web/session/mysql
  9. # Postgres
  10. go get -u github.com/beego/beego/v2/server/web/session/postgres
  11. # Redis
  12. go get -u github.com/beego/beego/v2/server/web/session/redis
  13. # Redis (cluster mode)
  14. go get -u github.com/beego/beego/v2/server/web/session/redis_cluster
  15. # Redis (sentinel)
  16. go get -u github.com/beego/beego/v2/server/web/session/redis_sentinel
  17. # SSDB
  18. go get -u github.com/beego/beego/v2/server/web/session/ssdb

Example Usage

Couchbase

SessionProviderConfig is connection address using couchbase.

  1. // main.go
  2. package main
  3. import (
  4. "github.com/beego/beego/v2/server/web"
  5. _ "github.com/beego/beego/v2/server/web/session/couchbase"
  6. )
  7. func init() {
  8. web.BConfig.WebConfig.Session.SessionOn = true
  9. web.BConfig.WebConfig.Session.SessionProvider = "couchbase"
  10. web.BConfig.WebConfig.Session.SessionProviderConfig = "http://bucketname:bucketpass@myserver:8091/"
  11. }

File

  1. // main.go
  2. package main
  3. import (
  4. "github.com/beego/beego/v2/server/web"
  5. )
  6. func init() {
  7. web.BConfig.WebConfig.Session.SessionOn = true
  8. web.BConfig.WebConfig.Session.SessionProvider = "file"
  9. web.BConfig.WebConfig.Session.SessionProviderConfig = "/tmp"
  10. }

Memcache

SessionProviderConfig is the connection address using memcache.

  1. // main.go
  2. package main
  3. import (
  4. "github.com/beego/beego/v2/server/web"
  5. _ "github.com/beego/beego/v2/server/web/session/memcache"
  6. )
  7. func init() {
  8. web.BConfig.WebConfig.Session.SessionOn = true
  9. web.BConfig.WebConfig.Session.SessionProvider = "memcache"
  10. web.BConfig.WebConfig.Session.SessionProviderConfig = "127.0.0.1:7080"
  11. }

MySQL

SessionProviderConfig is the connection address using go-sql-driver.

  1. // main.go
  2. package main
  3. import (
  4. "github.com/beego/beego/v2/server/web"
  5. _ "github.com/beego/beego/v2/server/web/session/mysql"
  6. )
  7. func init() {
  8. web.BConfig.WebConfig.Session.SessionOn = true
  9. web.BConfig.WebConfig.Session.SessionProvider = "mysql"
  10. web.BConfig.WebConfig.Session.SessionProviderConfig = "username:password@protocol(address)/dbname?param=value"
  11. }

Postgres

SessionProviderConfig is the connection address using postgres.

  1. // main.go
  2. package main
  3. import (
  4. "github.com/beego/beego/v2/server/web"
  5. _ "github.com/beego/beego/v2/server/web/session/postgres"
  6. )
  7. func init() {
  8. web.BConfig.WebConfig.Session.SessionOn = true
  9. web.BConfig.WebConfig.Session.SessionProvider = "postgresql"
  10. web.BConfig.WebConfig.Session.SessionProviderConfig = "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full"
  11. }

Redis

SessionProviderConfig is the connection address using redigo.

  1. // main.go
  2. package main
  3. import (
  4. "github.com/beego/beego/v2/server/web"
  5. _ "github.com/beego/beego/v2/server/web/session/redis"
  6. )
  7. func init() {
  8. web.BConfig.WebConfig.Session.SessionOn = true
  9. web.BConfig.WebConfig.Session.SessionProvider = "redis"
  10. web.BConfig.WebConfig.Session.SessionProviderConfig = "127.0.0.1:6379"
  11. }

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.