2017-02-15 19:06:19 +01:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2017-08-02 20:18:37 +02:00
|
|
|
"github.com/kataras/golog"
|
2017-08-07 05:04:35 +02:00
|
|
|
"github.com/kataras/iris/sessions"
|
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-08-07 05:04:35 +02:00
|
|
|
async bool
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
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-08-07 05:04:35 +02:00
|
|
|
// Note: no need to clean up here, the redis should handle these automatically because of the "SETEX"
|
|
|
|
// but that expiration doesn't depend on the session, instead it depends on the `MaxAgeSeconds`
|
|
|
|
// of the redis database configuration.
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Config returns the configuration for the redis server bridge, you can change them.
|
2017-08-07 05:04:35 +02:00
|
|
|
func (db *Database) Config() *service.Config {
|
|
|
|
return db.redis.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async if true passed then it will use different
|
|
|
|
// go routines to update the redis storage.
|
|
|
|
func (db *Database) Async(useGoRoutines bool) *Database {
|
|
|
|
db.async = useGoRoutines
|
|
|
|
return db
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Load loads the values to the underline.
|
2017-08-07 05:04:35 +02:00
|
|
|
func (db *Database) Load(sid string) (storeDB sessions.RemoteStore) {
|
|
|
|
// values := make(map[string]interface{})
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-08-07 05:04:35 +02:00
|
|
|
if !db.redis.Connected { //yes, check every first time's session for valid redis connection
|
|
|
|
db.redis.Connect()
|
|
|
|
_, err := db.redis.PingPong()
|
2017-02-15 19:06:19 +01:00
|
|
|
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-08-07 05:04:35 +02:00
|
|
|
// fetch the values from this session id and copy-> store them
|
|
|
|
storeMaybe, err := db.redis.Get(sid)
|
|
|
|
if err != nil {
|
|
|
|
golog.Errorf("error while trying to load session values(%s) from redis: %v", sid, err)
|
2017-07-31 20:49:30 +02:00
|
|
|
return
|
|
|
|
}
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-08-07 05:04:35 +02:00
|
|
|
storeDB, ok := storeMaybe.(sessions.RemoteStore)
|
2017-07-31 20:49:30 +02:00
|
|
|
if !ok {
|
2017-08-07 05:04:35 +02:00
|
|
|
golog.Errorf(`error while trying to load session values(%s) from redis:
|
|
|
|
the retrieved value is not a sessions.RemoteStore type, please report that as bug, it should never occur`,
|
|
|
|
sid)
|
2017-07-31 20:49:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-07 05:04:35 +02:00
|
|
|
return
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-08-07 05:04:35 +02:00
|
|
|
// Sync syncs the database.
|
|
|
|
func (db *Database) Sync(p sessions.SyncPayload) {
|
|
|
|
if db.async {
|
|
|
|
go db.sync(p)
|
2017-02-15 19:06:19 +01:00
|
|
|
} else {
|
2017-08-07 05:04:35 +02:00
|
|
|
db.sync(p)
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 05:04:35 +02:00
|
|
|
func (db *Database) sync(p sessions.SyncPayload) {
|
|
|
|
if p.Action == sessions.ActionDestroy {
|
|
|
|
db.redis.Delete(p.SessionID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
storeB, err := p.Store.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
golog.Error("error while encoding the remote session store")
|
|
|
|
return
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-08-07 05:04:35 +02:00
|
|
|
db.redis.Set(p.SessionID, storeB)
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|