2017-02-15 19:06:19 +01:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2017-08-01 21:25:08 +02:00
|
|
|
"time"
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-08-02 20:18:37 +02:00
|
|
|
"github.com/kataras/golog"
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
"github.com/kataras/iris/sessions/sessiondb/redis/service"
|
2017-02-15 19:06:19 +01:00
|
|
|
)
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Database the redis back-end session database for the sessions.
|
2017-02-15 19:06:19 +01:00
|
|
|
type Database struct {
|
|
|
|
redis *service.Service
|
|
|
|
}
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// New returns a new redis database.
|
2017-02-15 19:06:19 +01:00
|
|
|
func New(cfg ...service.Config) *Database {
|
|
|
|
return &Database{redis: service.New(cfg...)}
|
|
|
|
}
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Config returns the configuration for the redis server bridge, you can change them.
|
2017-02-15 19:06:19 +01:00
|
|
|
func (d *Database) Config() *service.Config {
|
|
|
|
return d.redis.Config
|
|
|
|
}
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Load loads the values to the underline.
|
2017-07-31 20:49:30 +02:00
|
|
|
func (d *Database) Load(sid string) (datas map[string]interface{}, expireDate *time.Time) {
|
2017-02-15 19:06:19 +01:00
|
|
|
values := make(map[string]interface{})
|
|
|
|
|
|
|
|
if !d.redis.Connected { //yes, check every first time's session for valid redis connection
|
|
|
|
d.redis.Connect()
|
|
|
|
_, err := d.redis.PingPong()
|
|
|
|
if err != nil {
|
2017-08-02 20:18:37 +02:00
|
|
|
golog.Errorf("redis database error on connect: %v", err)
|
|
|
|
return
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-31 20:49:30 +02:00
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
//fetch the values from this session id and copy-> store them
|
|
|
|
val, err := d.redis.GetBytes(sid)
|
|
|
|
if err == nil {
|
2017-02-17 09:45:47 +01:00
|
|
|
// err removed because of previous TODO
|
|
|
|
DeserializeBytes(val, &values)
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
datas, _ = values["session-data"].(map[string]interface{})
|
|
|
|
|
|
|
|
dbExpireDateValue, exists := values["expire-date"]
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
expireDateValue, ok := dbExpireDateValue.(time.Time)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return datas, &expireDateValue
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// serialize the values to be stored as strings inside the Redis, we panic at any serialization error here
|
|
|
|
func serialize(values map[string]interface{}) []byte {
|
|
|
|
val, err := SerializeBytes(values)
|
|
|
|
if err != nil {
|
2017-06-12 17:23:35 +02:00
|
|
|
return nil
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the real redis store
|
2017-07-31 20:49:30 +02:00
|
|
|
func (d *Database) Update(sid string, newValues map[string]interface{}, expireDate *time.Time) {
|
2017-02-15 19:06:19 +01:00
|
|
|
if len(newValues) == 0 {
|
|
|
|
go d.redis.Delete(sid)
|
|
|
|
} else {
|
2017-07-31 20:49:30 +02:00
|
|
|
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))
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:53:17 +02:00
|
|
|
// SerializeBytes serialize the "m" into bytes using the gob encoder and returns the result.
|
2017-02-15 19:06:19 +01:00
|
|
|
func SerializeBytes(m interface{}) ([]byte, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := gob.NewEncoder(buf)
|
|
|
|
err := enc.Encode(m)
|
|
|
|
if err == nil {
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:53:17 +02:00
|
|
|
// DeserializeBytes converts the bytes to a go value and puts that to "m" using the gob decoder.
|
2017-02-15 19:06:19 +01:00
|
|
|
func DeserializeBytes(b []byte, m interface{}) error {
|
|
|
|
dec := gob.NewDecoder(bytes.NewBuffer(b))
|
|
|
|
return dec.Decode(m) //no reference here otherwise doesn't work because of go remote object
|
|
|
|
}
|