From d08c8aa1d6214418ec036f1971609f68ae9bd914 Mon Sep 17 00:00:00 2001 From: kataras Date: Wed, 2 Aug 2017 21:18:37 +0300 Subject: [PATCH] Session database for file-storage https://github.com/kataras/iris/issues/702 using the latest API (Not tested on every OS, yet) Former-commit-id: fb98f86f2a602d543ca1abb5281a30af96462c11 --- sessions/sessiondb/file/database.go | 33 ++++++++++++++++++++++------ sessions/sessiondb/redis/database.go | 9 +++----- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/sessions/sessiondb/file/database.go b/sessions/sessiondb/file/database.go index 0b83b35f..bce3054b 100644 --- a/sessions/sessiondb/file/database.go +++ b/sessions/sessiondb/file/database.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path/filepath" + "time" "github.com/kataras/golog" ) @@ -30,9 +31,7 @@ func (d *Database) sessPath(sid string) string { } // Load loads the values to the underline -func (d *Database) Load(sid string) map[string]interface{} { - values := make(map[string]interface{}) - +func (d *Database) Load(sid string) (values map[string]interface{}, expireDate *time.Time) { val, err := ioutil.ReadFile(d.sessPath(sid)) if err == nil { @@ -43,7 +42,8 @@ func (d *Database) Load(sid string) map[string]interface{} { golog.Errorf("load error: %v", err) } - return values + // no expiration + return } // serialize the values to be stored as strings inside the session file-storage. @@ -56,14 +56,33 @@ func serialize(values map[string]interface{}) []byte { return val } +func (d *Database) expireSess(sid string) { + go os.Remove(d.sessPath(sid)) +} + // Update updates the session file-storage. -func (d *Database) Update(sid string, newValues map[string]interface{}) { +func (d *Database) Update(sid string, newValues map[string]interface{}, expireDate *time.Time) { + now := time.Now() sessPath := d.sessPath(sid) - if len(newValues) == 0 { - go os.Remove(sessPath) + if len(newValues) == 0 { // means delete by call + d.expireSess(sid) return } + // delete the file on expiration + if expireDate != nil && !expireDate.IsZero() { + if expireDate.Before(now) { + // already expirated, delete it now and return. + d.expireSess(sid) + return + } + // otherwise set a timer to delete the file automatically + afterDur := expireDate.Sub(now) + time.AfterFunc(afterDur, func() { + go os.Remove(sessPath) + }) + } + ioutil.WriteFile(sessPath, serialize(newValues), 0666) } diff --git a/sessions/sessiondb/redis/database.go b/sessions/sessiondb/redis/database.go index 00b95c6a..df190f8d 100644 --- a/sessions/sessiondb/redis/database.go +++ b/sessions/sessiondb/redis/database.go @@ -5,6 +5,7 @@ import ( "encoding/gob" "time" + "github.com/kataras/golog" "github.com/kataras/iris/sessions/sessiondb/redis/service" ) @@ -31,12 +32,8 @@ func (d *Database) Load(sid string) (datas map[string]interface{}, expireDate *t d.redis.Connect() _, err := d.redis.PingPong() if err != nil { - if err != nil { - // don't use to get the logger, just prin these to the console... atm - ///TODO: Find a way to use the iris' defined logger via an optional interface to Database. - // println("Redis Connection error on Connect: " + err.Error()) - // println("But don't panic, auto-switching to memory store right now!") - } + golog.Errorf("redis database error on connect: %v", err) + return } }