Database
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.
  1. 1.
    import the gitbub.com/kataras/iris/sessions/sessiondb/redis or boltdb or badger,
  2. 2.
    initialize the database and
  3. 3.
    register it
For example, to register the redis session database:
1
import (
2
"github.com/kataras/iris/v12"
3
"github.com/kataras/iris/v12/sessions"
4
5
// 1. Import the session database.
6
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
7
)
8
9
// 2. Initialize the database.
10
// These are the default values,
11
// you can replace them based on your running redis' server settings:
12
db := redis.New(redis.Config{
13
Network: "tcp",
14
Addr: "127.0.0.1:6379",
15
Timeout: time.Duration(30) * time.Second,
16
MaxActive: 10,
17
Password: "",
18
Database: "",
19
Prefix: "",
20
Delim: "-",
21
Driver: redis.Redigo(), // redis.Radix() can be used instead.
22
})
23
24
// Optionally configure the underline driver:
25
// driver := redis.Redigo()
26
// driver.MaxIdle = ...
27
// driver.IdleTimeout = ...
28
// driver.Wait = ...
29
// redis.Config {Driver: driver}
30
31
// Close connection when control+C/cmd+C
32
iris.RegisterOnInterrupt(func() {
33
db.Close()
34
})
35
36
sess := sessions.New(sessions.Config{
37
Cookie: "sessionscookieid",
38
AllowReclaim: true,
39
})
40
41
// 3. Register it.
42
sess.UseDatabase(db)
43
44
// [...]
Copied!
boltdb
1
import "os"
2
import "github.com/kataras/iris/v12/sessions/sessiondb/boltdb"
3
4
db, err := boltdb.New("./sessions.db", os.FileMode(0750))
Copied!
badger
1
import "github.com/kataras/iris/v12/sessions/sessiondb/badger"
2
3
db, err := badger.New("./data")
Copied!
Last modified 1yr ago
Export as PDF
Copy link