1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

Fix up for sessions

- Restore expiration state of the sessions
- Add APIs to modify expire date
- Free the timer for the session destroy task

Former-commit-id: 4d796a9efba2c37cd3750275ac62c068ceb82be8
This commit is contained in:
corebreaker
2017-07-31 21:49:30 +03:00
parent 4f2985cb4e
commit 0f1a265e5a
5 changed files with 226 additions and 91 deletions

View File

@@ -1,6 +1,7 @@
package redis
import (
"time"
"bytes"
"encoding/gob"
@@ -23,7 +24,7 @@ func (d *Database) Config() *service.Config {
}
// Load loads the values to the underline.
func (d *Database) Load(sid string) map[string]interface{} {
func (d *Database) Load(sid string) (datas map[string]interface{}, expireDate *time.Time) {
values := make(map[string]interface{})
if !d.redis.Connected { //yes, check every first time's session for valid redis connection
@@ -38,6 +39,7 @@ func (d *Database) Load(sid string) map[string]interface{} {
}
}
}
//fetch the values from this session id and copy-> store them
val, err := d.redis.GetBytes(sid)
if err == nil {
@@ -45,8 +47,19 @@ func (d *Database) Load(sid string) map[string]interface{} {
DeserializeBytes(val, &values)
}
return values
datas, _ = values["session-data"].(map[string]interface{})
dbExpireDateValue, exists := values["expire-date"]
if !exists {
return
}
expireDateValue, ok := dbExpireDateValue.(time.Time)
if !ok {
return
}
return datas, &expireDateValue
}
// serialize the values to be stored as strings inside the Redis, we panic at any serialization error here
@@ -60,11 +73,17 @@ func serialize(values map[string]interface{}) []byte {
}
// Update updates the real redis store
func (d *Database) Update(sid string, newValues map[string]interface{}) {
func (d *Database) Update(sid string, newValues map[string]interface{}, expireDate *time.Time) {
if len(newValues) == 0 {
go d.redis.Delete(sid)
} else {
go d.redis.Set(sid, serialize(newValues)) //set/update all the values
datas := map[string]interface{}{"session-data": newValues}
if expireDate != nil {
datas["expire-date"] = *expireDate
}
//set/update all the values
go d.redis.Set(sid, serialize(datas))
}
}