Sessions database
Gerasimos (Makis) Maropoulos edited this page Oct 26, 2019
·
3 revisions
Pages 72
- What is Iris
-
📌 Getting Started - Host
- Configuration
- Routing
- HTTP Method Override
- API Versioning
- Content Negotiation
- Response Recorder
- HTTP Referrer
- Request Authentication
- URL Query Parameters
- Forms
- Model Validation
- Cache
- View
- Cookies
- Sessions
- Websockets
- Dependency Injection
- MVC
- gRPC
- Sitemap
- Localization
- Testing
-
🤓 Resources
Clone this wiki locally
Sessions 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.
- 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)
// [...]boltdb
import "os"
import "github.com/kataras/iris/v12/sessions/sessiondb/boltdb"
db, err := boltdb.New("./sessions.db", os.FileMode(0750))badger
import "github.com/kataras/iris/v12/sessions/sessiondb/badger"
db, err := badger.New("./data")Home | Project | Quick Start | Technical Docs | Copyright © 2019-2020 Gerasimos Maropoulos. Documentation terms of use.