mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Page:
Sessions database
Pages
API versioning
Automatic public address
Benchmarks
Cache
Configuration
Content negotiation
Cookies
Documentation terms and conditions
Forms
Grpc
HTTP method override
HTTP referrer
Home
Host
Installation
Localization
MVC
Model validation
Our users
Project and community
Publications
Quick start
Request authentication
Response recorder
Routing context methods
Routing error handlers
Routing middleware
Routing override context
Routing path parameter types
Routing reverse lookups
Routing subdomains
Routing wrap the router
Routing
Sessions database
Sessions flash messages
Sessions
Sitemap
Starter kits
Support
Testing
URL query parameters
View
Websockets
dependency injection
3
Sessions database
Gerasimos (Makis) Maropoulos edited this page 2019-10-26 18:53:48 +03:00
Table of Contents
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")
- 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
Home | Project | Quick Start | Technical Docs | Copyright © 2019-2020 Gerasimos Maropoulos. Documentation terms of use.