mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Sessions are now in full sync with the registered database, on acquire(init), set, get, delete, clear, visit, len, release(destroy) as requested by almost everyone. https://github.com/kataras/iris/issues/969
Former-commit-id: 49fcdb93106a78f0a24ad3fb4d8725e35e98451a
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"github.com/kataras/iris/sessions/sessiondb/redis/service"
|
||||
)
|
||||
|
||||
// tested with redis version 3.0.503.
|
||||
// for windows see: https://github.com/ServiceStack/redis-windows
|
||||
func main() {
|
||||
// replace with your running redis' server settings:
|
||||
db := redis.New(service.Config{
|
||||
@@ -27,7 +29,12 @@ func main() {
|
||||
db.Close()
|
||||
})
|
||||
|
||||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid", Expires: 45 * time.Minute})
|
||||
defer db.Close() // close the database connection if application errored.
|
||||
|
||||
sess := sessions.New(sessions.Config{
|
||||
Cookie: "sessionscookieid",
|
||||
Expires: 45 * time.Minute}, // <=0 means unlimited life. Defaults to 0.
|
||||
)
|
||||
|
||||
//
|
||||
// IMPORTANT:
|
||||
@@ -46,13 +53,30 @@ func main() {
|
||||
s.Set("name", "iris")
|
||||
|
||||
//test if setted here
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
ctx.Writef("All ok session value of the 'name' is: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
s := sess.Start(ctx)
|
||||
// set session values
|
||||
s.Set(key, value)
|
||||
|
||||
// test if setted here
|
||||
ctx.Writef("All ok session value of the '%s' is: %s", key, s.GetString(key))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The 'name' on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/get/{key}", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
@@ -76,5 +100,5 @@ func main() {
|
||||
sess.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user