1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

remove MaxAgeSeconds from redis sessiondb - now it derives from the sessions configuration automatically. Add option to use an existing BoltDB connection on boltdb sessiondb.

TODO: leveldb sessiondb

Former-commit-id: 8cb781bf089ab7b9a09dccc633454db9c6077610
This commit is contained in:
kataras
2017-08-08 12:31:42 +03:00
parent 48e352e1df
commit 92d0b146df
7 changed files with 70 additions and 43 deletions

View File

@@ -1,6 +1,8 @@
package redis
import (
"runtime"
"github.com/kataras/golog"
"github.com/kataras/iris/sessions"
"github.com/kataras/iris/sessions/sessiondb/redis/service"
@@ -14,10 +16,9 @@ type Database struct {
// New returns a new redis database.
func New(cfg ...service.Config) *Database {
return &Database{redis: service.New(cfg...)}
// 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.
db := &Database{redis: service.New(cfg...)}
runtime.SetFinalizer(db, closeDB)
return db
}
// Config returns the configuration for the redis server bridge, you can change them.
@@ -83,5 +84,14 @@ func (db *Database) sync(p sessions.SyncPayload) {
return
}
db.redis.Set(p.SessionID, storeB)
db.redis.Set(p.SessionID, storeB, p.Store.Lifetime.Second())
}
// Close shutdowns the redis connection.
func (db *Database) Close() error {
return closeDB(db)
}
func closeDB(db *Database) error {
return db.redis.CloseConnection()
}

View File

@@ -33,21 +33,18 @@ type Config struct {
IdleTimeout time.Duration
// Prefix "myprefix-for-this-website". Default ""
Prefix string
// MaxAgeSeconds how much long the redis should keep the session in seconds. Default 31556926.0 (1 year)
MaxAgeSeconds int
}
// DefaultConfig returns the default configuration for Redis service.
func DefaultConfig() Config {
return Config{
Network: DefaultRedisNetwork,
Addr: DefaultRedisAddr,
Password: "",
Database: "",
MaxIdle: 0,
MaxActive: 0,
IdleTimeout: DefaultRedisIdleTimeout,
Prefix: "",
MaxAgeSeconds: DefaultRedisMaxAgeSeconds,
Network: DefaultRedisNetwork,
Addr: DefaultRedisAddr,
Password: "",
Database: "",
MaxIdle: 0,
MaxActive: 0,
IdleTimeout: DefaultRedisIdleTimeout,
Prefix: "",
}
}

View File

@@ -44,15 +44,21 @@ func (r *Service) CloseConnection() error {
// Set sets a key-value to the redis store.
// The expiration is setted by the MaxAgeSeconds.
func (r *Service) Set(key string, value interface{}) error {
func (r *Service) Set(key string, value interface{}, secondsLifetime int) (err error) {
c := r.pool.Get()
defer c.Close()
if c.Err() != nil {
return c.Err()
}
_, err := c.Do("SETEX", r.Config.Prefix+key, r.Config.MaxAgeSeconds, value)
return err
// if has expiration, then use the "EX" to delete the key automatically.
if secondsLifetime > 0 {
_, err = c.Do("SETEX", r.Config.Prefix+key, secondsLifetime, value)
} else {
_, err = c.Do("SET", r.Config.Prefix+key, value)
}
return
}
// Get returns value, err by its key
@@ -164,10 +170,6 @@ func (r *Service) Connect() {
c.Addr = DefaultRedisAddr
}
if c.MaxAgeSeconds <= 0 {
c.MaxAgeSeconds = DefaultRedisMaxAgeSeconds
}
pool := &redis.Pool{IdleTimeout: DefaultRedisIdleTimeout, MaxIdle: c.MaxIdle, MaxActive: c.MaxActive}
pool.TestOnBorrow = func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")