Sometimes you need a backend storage, i.e redis, which will keep your session data on server restarts and scale horizontally.
Registering a session database can be done through a single call of sessions.UseDatabase(database).
Iris implements three (3) builtin session databases for redis, badger and boltdb. These session databases are implemented via the iris/sessions/sessiondb subpackage which you'll have to import.
import the gitbub.com/kataras/iris/sessions/sessiondb/redis or boltdb or badger,
initialize the database and
register it
For example, to register the redis session database:
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/sessions"
// 1. Import the session database.
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
)
// 2. Initialize the database.
// These are the default values,
// you can replace them based on your running redis' server settings:
db := redis.New(redis.Config{
Network: "tcp",
Addr: "127.0.0.1:6379",
Timeout: time.Duration(30) * time.Second,
MaxActive: 10,
Password: "",
Database: "",
Prefix: "",
Delim: "-",
Driver: redis.Redigo(), // redis.Radix() can be used instead.
})
// Optionally configure the underline driver:
// driver := redis.Redigo()
// driver.MaxIdle = ...
// driver.IdleTimeout = ...
// driver.Wait = ...
// redis.Config {Driver: driver}
// Close connection when control+C/cmd+C
iris.RegisterOnInterrupt(func() {
db.Close()
})
sess := sessions.New(sessions.Config{
Cookie: "sessionscookieid",
AllowReclaim: true,
})
// 3. Register it.
sess.UseDatabase(db)
// [...]