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

extract the Delim for redis sessiondb as requested at https://github.com/kataras/iris/issues/1256 and add a mvc/regexp example and some other trivial changes

Former-commit-id: f9e09320bfe07ae10ac74f54a78272cf21d21cc7
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-05-30 10:48:07 +03:00
parent 7df7f0fea2
commit adb6fd764a
20 changed files with 230 additions and 168 deletions

View File

@@ -61,10 +61,8 @@ func (db *Database) OnUpdateExpiration(sid string, newExpires time.Duration) err
return db.redis.UpdateTTLMany(sid, int64(newExpires.Seconds()))
}
const delim = "_"
func makeKey(sid, key string) string {
return sid + delim + key
func (db *Database) makeKey(sid, key string) string {
return sid + db.redis.Config.Delim + key
}
// Set sets a key value of a specific session.
@@ -76,14 +74,14 @@ func (db *Database) Set(sid string, lifetime sessions.LifeTime, key string, valu
return
}
if err = db.redis.Set(makeKey(sid, key), valueBytes, int64(lifetime.DurationUntilExpiration().Seconds())); err != nil {
if err = db.redis.Set(db.makeKey(sid, key), valueBytes, int64(lifetime.DurationUntilExpiration().Seconds())); err != nil {
golog.Debug(err)
}
}
// Get retrieves a session value based on the key.
func (db *Database) Get(sid string, key string) (value interface{}) {
db.get(makeKey(sid, key), &value)
db.get(db.makeKey(sid, key), &value)
return
}
@@ -100,7 +98,7 @@ func (db *Database) get(key string, outPtr interface{}) {
}
func (db *Database) keys(sid string) []string {
keys, err := db.redis.GetKeys(sid + delim)
keys, err := db.redis.GetKeys(sid + db.redis.Config.Delim)
if err != nil {
golog.Debugf("unable to get all redis keys of session '%s': %v", sid, err)
return nil
@@ -126,7 +124,7 @@ func (db *Database) Len(sid string) (n int) {
// Delete removes a session key value based on its key.
func (db *Database) Delete(sid string, key string) (deleted bool) {
err := db.redis.Delete(makeKey(sid, key))
err := db.redis.Delete(db.makeKey(sid, key))
if err != nil {
golog.Error(err)
}

View File

@@ -5,32 +5,36 @@ import (
)
const (
// DefaultRedisNetwork the redis network option, "tcp"
// DefaultRedisNetwork the redis network option, "tcp".
DefaultRedisNetwork = "tcp"
// DefaultRedisAddr the redis address option, "127.0.0.1:6379"
// DefaultRedisAddr the redis address option, "127.0.0.1:6379".
DefaultRedisAddr = "127.0.0.1:6379"
// DefaultRedisIdleTimeout the redis idle timeout option, time.Duration(5) * time.Minute
// DefaultRedisIdleTimeout the redis idle timeout option, time.Duration(5) * time.Minute.
DefaultRedisIdleTimeout = time.Duration(5) * time.Minute
// DefaultDelim ths redis delim option, "-".
DefaultDelim = "-"
)
// Config the redis configuration used inside sessions
type Config struct {
// Network "tcp"
// Network protocol. Defaults to "tcp".
Network string
// Addr "127.0.0.1:6379"
// Addr of the redis server. Defaults to "127.0.0.1:6379".
Addr string
// Password string .If no password then no 'AUTH'. Default ""
// Password string .If no password then no 'AUTH'. Defaults to "".
Password string
// If Database is empty "" then no 'SELECT'. Default ""
// If Database is empty "" then no 'SELECT'. Defaults to "".
Database string
// MaxIdle 0 no limit
// MaxIdle 0 no limit.
MaxIdle int
// MaxActive 0 no limit
// MaxActive 0 no limit.
MaxActive int
// IdleTimeout time.Duration(5) * time.Minute
// IdleTimeout time.Duration(5) * time.Minute.
IdleTimeout time.Duration
// Prefix "myprefix-for-this-website". Default ""
// Prefix "myprefix-for-this-website". Defaults to "".
Prefix string
// Delim the delimeter for the values. Defaults to "-".
Delim string
}
// DefaultConfig returns the default configuration for Redis service.
@@ -44,5 +48,6 @@ func DefaultConfig() Config {
MaxActive: 0,
IdleTimeout: DefaultRedisIdleTimeout,
Prefix: "",
Delim: DefaultDelim,
}
}