mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
example: sessions: scale out using redis
Former-commit-id: a811648e0bdf83c289656426f8ba67d785b7d5cc
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
@@ -18,7 +21,7 @@ func main() {
|
||||
// you can replace them based on your running redis' server settings:
|
||||
db := redis.New(redis.Config{
|
||||
Network: "tcp",
|
||||
Addr: "127.0.0.1:6379",
|
||||
Addr: getenv("REDIS_ADDR", "127.0.0.1:6379"),
|
||||
Timeout: time.Duration(30) * time.Second,
|
||||
MaxActive: 10,
|
||||
Password: "",
|
||||
@@ -55,5 +58,20 @@ func main() {
|
||||
sess.UseDatabase(db)
|
||||
|
||||
app := example.NewApp(sess)
|
||||
app.Listen(":8080")
|
||||
|
||||
// TIP scaling-out Iris sessions using redis:
|
||||
// $ docker-compose up
|
||||
// http://localhost:8080/set/$key/$value
|
||||
// The value will be available on all Iris servers as well.
|
||||
// E.g. http://localhost:9090/get/$key and vice versa.
|
||||
addr := fmt.Sprintf(":%s", getenv("PORT", "8080"))
|
||||
app.Listen(addr)
|
||||
}
|
||||
|
||||
func getenv(key string, def string) string {
|
||||
if v := os.Getenv(strings.ToUpper(key)); v != "" {
|
||||
return v
|
||||
}
|
||||
|
||||
return def
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user