Improve this page on GitHub

Session control

Beego has a built-in session module. It supports memory, file, mysql, redis, couchbase, memcache and postgres as the save provider. You can also implement your own provider according to the interface.

It is very easy to use session in Beego, just switch session on in the main function:

beego.SessionOn = true

Or you can switch it on in the configuration file:

SessionOn = true

After switching on the session, you can use it 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.TplNames = "index.tpl"
}

There are some useful methods to handle session:

  • SetSession(name string, value interface{})
  • GetSession(name string) interface{}
  • DelSession(name string)
  • SessionRegenerateID()
  • DestroySession()

The most common used methods are SetSession, GetSession, and DelSession.

You can also use your own logic:

sess := this.StartSession()
defer sess.SessionRelease()

sess object has following methods:

  • Set
  • Get
  • Delete
  • SessionID
  • SessionRelease
  • Flush

But I recommend that you use SetSession, GetSession and DelSession to prevent forgetting to release resource by yourself.

Here are some parameters used in the Session module:

  • SessionOn

Enable Session or not, false by default. Parameter name in configuration file: SessionOn

  • SessionProvider
    Set Session provider, which is set to memory by default. It also supports file, mysql and redis. Parameter name in configuration file: sessionprovider

  • SessionName
    Set the cookie name. Session is stored in browser’s cookies by default. The default name is beegosessionID. Parameter name in configuration file: sessionname.

  • SessionGCMaxLifetime
    Set the Session expire time, 3600s by default. Parameter name in configuration file: sessiongcmaxlifetime.

  • SessionSavePath
    This is the save path or connection string for file, mysql or redis, empty by default. Parameter name in configuration file: sessionsavepath.

  • SessionHashFunc
    Default value is sha1 which means sessionid is generated by sha1.

  • SessionHashKey
    Default key is beegoserversessionkey. We recommend you change this value.

  • SessionCookieLifeTime
    The cookie expire time. The cookie is used to store data in client.

from beego version 1.1.3 onwards, Beego removed all the dependencies, if you use these storages: mysql, redis, couchbase, memcache, postgres. You should install them first:

go get -u github.com/astaxie/beego/session/mysql

then you can import in main.go, the same as the database/sql:

import _ "github.com/astaxie/beego/session/mysql"

When SessionProvider is file, SessionSavePath is the save path of session files. For example:

beego.SessionProvider = "file"
beego.SessionSavePath = "./tmp"

When SessionProvider is mysql, SessionSavePath is connection address which is using go-sql-driver. For example:

beego.SessionProvider = "mysql"
beego.SessionSavePath = "username:password@protocol(address)/dbname?param=value"

When SessionProvider is redis, SessionSavePath is connection address for redis which is using redigo. For example:

beego.SessionProvider = "redis"
beego.SessionSavePath = "127.0.0.1:6379"

When SessionProvider is memcache,SessionSavePath is connection address for memcache which is using memcache. For example

beego.SessionProvider = "memcache"
beego.SessionSavePath = "127.0.0.1:7080"

When SessionProvider is postgres,SessionSavePath is connection address for postgres which is using postgres. For example:

beego.SessionProvider = "postgresql"
beego.SessionSavePath = "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full"

When SessionProvider is couchbase,SessionSavePath is connection address for couchbase which is using couchbase. For example:

beego.SessionProvider = "couchbase"
beego.SessionSavePath = "http://bucketname:bucketpass@myserver:8091/"

Note:

Session uses gob to register objects. If you are using a session engine other than memory, please register the objects you use in session before using them. You can use gob.Register() to register them in init() function.

comments powered by Disqus