mirror of
https://github.com/astaxie/beego.git
synced 2025-06-14 19:10:40 +00:00
support using json string to init session
This commit is contained in:
@ -34,6 +34,7 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -110,48 +111,89 @@ func (rs *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWrite
|
||||
|
||||
// Provider redis session provider
|
||||
type Provider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
poolsize int
|
||||
password string
|
||||
dbNum int
|
||||
idleTimeout time.Duration
|
||||
idleCheckFrequency time.Duration
|
||||
maxRetries int
|
||||
poollist *redis.Client
|
||||
maxlifetime int64
|
||||
SavePath string `json:"save_path"`
|
||||
Poolsize int `json:"poolsize"`
|
||||
Password string `json:"password"`
|
||||
DbNum int `json:"db_num"`
|
||||
|
||||
idleTimeout time.Duration
|
||||
IdleTimeoutStr string `json:"idle_timeout"`
|
||||
|
||||
idleCheckFrequency time.Duration
|
||||
IdleCheckFrequencyStr string `json:"idle_check_frequency"`
|
||||
MaxRetries int `json:"max_retries"`
|
||||
poollist *redis.Client
|
||||
}
|
||||
|
||||
// SessionInit init redis session
|
||||
// savepath like redis server addr,pool size,password,dbnum,IdleTimeout second
|
||||
// e.g. 127.0.0.1:6379,100,astaxie,0,30
|
||||
func (rp *Provider) SessionInit(ctx context.Context, maxlifetime int64, savePath string) error {
|
||||
// v1.x e.g. 127.0.0.1:6379,100,astaxie,0,30
|
||||
// v2.0 you should pass json string
|
||||
func (rp *Provider) SessionInit(ctx context.Context, maxlifetime int64, cfgStr string) error {
|
||||
rp.maxlifetime = maxlifetime
|
||||
|
||||
cfgStr = strings.TrimSpace(cfgStr)
|
||||
// we think cfgStr is v2.0, using json to init the session
|
||||
if strings.HasPrefix(cfgStr, "{") {
|
||||
err := json.Unmarshal([]byte(cfgStr), rp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rp.idleTimeout, err = time.ParseDuration(rp.IdleTimeoutStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rp.idleCheckFrequency, err = time.ParseDuration(rp.IdleCheckFrequencyStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
rp.initOldStyle(cfgStr)
|
||||
}
|
||||
|
||||
rp.poollist = redis.NewClient(&redis.Options{
|
||||
Addr: rp.SavePath,
|
||||
Password: rp.Password,
|
||||
PoolSize: rp.Poolsize,
|
||||
DB: rp.DbNum,
|
||||
IdleTimeout: rp.idleTimeout,
|
||||
IdleCheckFrequency: rp.idleCheckFrequency,
|
||||
MaxRetries: rp.MaxRetries,
|
||||
})
|
||||
|
||||
return rp.poollist.Ping().Err()
|
||||
}
|
||||
|
||||
func (rp *Provider) initOldStyle(savePath string) {
|
||||
configs := strings.Split(savePath, ",")
|
||||
if len(configs) > 0 {
|
||||
rp.savePath = configs[0]
|
||||
rp.SavePath = configs[0]
|
||||
}
|
||||
if len(configs) > 1 {
|
||||
poolsize, err := strconv.Atoi(configs[1])
|
||||
if err != nil || poolsize < 0 {
|
||||
rp.poolsize = MaxPoolSize
|
||||
rp.Poolsize = MaxPoolSize
|
||||
} else {
|
||||
rp.poolsize = poolsize
|
||||
rp.Poolsize = poolsize
|
||||
}
|
||||
} else {
|
||||
rp.poolsize = MaxPoolSize
|
||||
rp.Poolsize = MaxPoolSize
|
||||
}
|
||||
if len(configs) > 2 {
|
||||
rp.password = configs[2]
|
||||
rp.Password = configs[2]
|
||||
}
|
||||
if len(configs) > 3 {
|
||||
dbnum, err := strconv.Atoi(configs[3])
|
||||
if err != nil || dbnum < 0 {
|
||||
rp.dbNum = 0
|
||||
rp.DbNum = 0
|
||||
} else {
|
||||
rp.dbNum = dbnum
|
||||
rp.DbNum = dbnum
|
||||
}
|
||||
} else {
|
||||
rp.dbNum = 0
|
||||
rp.DbNum = 0
|
||||
}
|
||||
if len(configs) > 4 {
|
||||
timeout, err := strconv.Atoi(configs[4])
|
||||
@ -168,21 +210,9 @@ func (rp *Provider) SessionInit(ctx context.Context, maxlifetime int64, savePath
|
||||
if len(configs) > 6 {
|
||||
retries, err := strconv.Atoi(configs[6])
|
||||
if err == nil && retries > 0 {
|
||||
rp.maxRetries = retries
|
||||
rp.MaxRetries = retries
|
||||
}
|
||||
}
|
||||
|
||||
rp.poollist = redis.NewClient(&redis.Options{
|
||||
Addr: rp.savePath,
|
||||
Password: rp.password,
|
||||
PoolSize: rp.poolsize,
|
||||
DB: rp.dbNum,
|
||||
IdleTimeout: rp.idleTimeout,
|
||||
IdleCheckFrequency: rp.idleCheckFrequency,
|
||||
MaxRetries: rp.maxRetries,
|
||||
})
|
||||
|
||||
return rp.poollist.Ping().Err()
|
||||
}
|
||||
|
||||
// SessionRead read redis session by sid
|
||||
|
@ -1,11 +1,15 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/astaxie/beego/server/web/session"
|
||||
)
|
||||
@ -94,3 +98,15 @@ func TestRedis(t *testing.T) {
|
||||
|
||||
sess.SessionRelease(nil, w)
|
||||
}
|
||||
|
||||
func TestProvider_SessionInit(t *testing.T) {
|
||||
|
||||
savePath := `
|
||||
{ "save_path": "my save path", "idle_timeout": "3s"}
|
||||
`
|
||||
cp := &Provider{}
|
||||
cp.SessionInit(context.Background(), 12, savePath)
|
||||
assert.Equal(t, "my save path", cp.SavePath)
|
||||
assert.Equal(t, 3*time.Second, cp.idleTimeout)
|
||||
assert.Equal(t, int64(12), cp.maxlifetime)
|
||||
}
|
||||
|
Reference in New Issue
Block a user