mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
replace the redis library we used with another one, no performance differences but most options for connections pooling and read/write/connect timeout (two config fields are changed as well)
relative to: https://github.com/kataras/iris/issues/1285 Former-commit-id: c20530cd67144ab8d1c9325807fe5c13268fa428
This commit is contained in:
@@ -1,36 +1,39 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/redis/service"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
// Database the redis back-end session database for the sessions.
|
||||
type Database struct {
|
||||
redis *service.Service
|
||||
redis *Service
|
||||
}
|
||||
|
||||
var _ sessions.Database = (*Database)(nil)
|
||||
|
||||
// New returns a new redis database.
|
||||
func New(cfg ...service.Config) *Database {
|
||||
db := &Database{redis: service.New(cfg...)}
|
||||
db.redis.Connect()
|
||||
func New(cfg ...Config) *Database {
|
||||
service := newService(cfg...)
|
||||
if err := service.Connect(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
db := &Database{redis: service}
|
||||
_, err := db.redis.PingPong()
|
||||
if err != nil {
|
||||
golog.Debugf("error connecting to redis: %v", err)
|
||||
return nil
|
||||
}
|
||||
runtime.SetFinalizer(db, closeDB)
|
||||
// runtime.SetFinalizer(db, closeDB)
|
||||
return db
|
||||
}
|
||||
|
||||
// Config returns the configuration for the redis server bridge, you can change them.
|
||||
func (db *Database) Config() *service.Config {
|
||||
func (db *Database) Config() *Config {
|
||||
return db.redis.Config
|
||||
}
|
||||
|
||||
@@ -39,6 +42,7 @@ func (db *Database) Config() *service.Config {
|
||||
func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime {
|
||||
seconds, hasExpiration, found := db.redis.TTL(sid)
|
||||
if !found {
|
||||
// fmt.Printf("db.Acquire expires: %s. Seconds: %v\n", expires, expires.Seconds())
|
||||
// not found, create an entry with ttl and return an empty lifetime, session manager will do its job.
|
||||
if err := db.redis.Set(sid, sid, int64(expires.Seconds())); err != nil {
|
||||
golog.Debug(err)
|
||||
@@ -74,6 +78,8 @@ func (db *Database) Set(sid string, lifetime sessions.LifeTime, key string, valu
|
||||
return
|
||||
}
|
||||
|
||||
// fmt.Println("database.Set")
|
||||
// fmt.Printf("lifetime.DurationUntilExpiration(): %s. Seconds: %v\n", lifetime.DurationUntilExpiration(), lifetime.DurationUntilExpiration().Seconds())
|
||||
if err = db.redis.Set(db.makeKey(sid, key), valueBytes, int64(lifetime.DurationUntilExpiration().Seconds())); err != nil {
|
||||
golog.Debug(err)
|
||||
}
|
||||
@@ -98,7 +104,7 @@ func (db *Database) get(key string, outPtr interface{}) {
|
||||
}
|
||||
|
||||
func (db *Database) keys(sid string) []string {
|
||||
keys, err := db.redis.GetKeys(sid + db.redis.Config.Delim)
|
||||
keys, err := db.redis.GetKeys(sid)
|
||||
if err != nil {
|
||||
golog.Debugf("unable to get all redis keys of session '%s': %v", sid, err)
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user