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
This commit is contained in:
kataras 2017-08-02 21:18:37 +03:00
parent 6fb90cfdb4
commit d08c8aa1d6
2 changed files with 29 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/kataras/golog" "github.com/kataras/golog"
) )
@ -30,9 +31,7 @@ func (d *Database) sessPath(sid string) string {
} }
// Load loads the values to the underline // Load loads the values to the underline
func (d *Database) Load(sid string) map[string]interface{} { func (d *Database) Load(sid string) (values map[string]interface{}, expireDate *time.Time) {
values := make(map[string]interface{})
val, err := ioutil.ReadFile(d.sessPath(sid)) val, err := ioutil.ReadFile(d.sessPath(sid))
if err == nil { if err == nil {
@ -43,7 +42,8 @@ func (d *Database) Load(sid string) map[string]interface{} {
golog.Errorf("load error: %v", err) golog.Errorf("load error: %v", err)
} }
return values // no expiration
return
} }
// serialize the values to be stored as strings inside the session file-storage. // 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 return val
} }
func (d *Database) expireSess(sid string) {
go os.Remove(d.sessPath(sid))
}
// Update updates the session file-storage. // 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) sessPath := d.sessPath(sid)
if len(newValues) == 0 { if len(newValues) == 0 { // means delete by call
go os.Remove(sessPath) d.expireSess(sid)
return 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) ioutil.WriteFile(sessPath, serialize(newValues), 0666)
} }

View File

@ -5,6 +5,7 @@ import (
"encoding/gob" "encoding/gob"
"time" "time"
"github.com/kataras/golog"
"github.com/kataras/iris/sessions/sessiondb/redis/service" "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() d.redis.Connect()
_, err := d.redis.PingPong() _, err := d.redis.PingPong()
if err != nil { if err != nil {
if err != nil { golog.Errorf("redis database error on connect: %v", err)
// don't use to get the logger, just prin these to the console... atm return
///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!")
}
} }
} }