1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-14 18:50:40 +00:00

support using json string to init session

This commit is contained in:
Ming Deng
2020-10-21 22:12:25 +08:00
parent 03ba495b7f
commit 05f4e0c146
12 changed files with 464 additions and 136 deletions

View File

@ -34,14 +34,16 @@ package redis_cluster
import (
"context"
"encoding/json"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/astaxie/beego/server/web/session"
rediss "github.com/go-redis/redis/v7"
"github.com/astaxie/beego/server/web/session"
)
var redispder = &Provider{}
@ -109,48 +111,86 @@ func (rs *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWrite
// Provider redis_cluster session provider
type Provider struct {
maxlifetime int64
savePath string
poolsize int
password string
dbNum int
idleTimeout time.Duration
idleCheckFrequency time.Duration
maxRetries int
poollist *rediss.ClusterClient
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 *rediss.ClusterClient
}
// SessionInit init redis_cluster session
// savepath like redis server addr,pool size,password,dbnum
// cfgStr like redis server addr,pool size,password,dbnum
// e.g. 127.0.0.1:6379;127.0.0.1:6380,100,test,0
func (rp *Provider) SessionInit(ctx context.Context, maxlifetime int64, savePath string) error {
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 = rediss.NewClusterClient(&rediss.ClusterOptions{
Addrs: strings.Split(rp.SavePath, ";"),
Password: rp.Password,
PoolSize: rp.Poolsize,
IdleTimeout: rp.idleTimeout,
IdleCheckFrequency: rp.idleCheckFrequency,
MaxRetries: rp.MaxRetries,
})
return rp.poollist.Ping().Err()
}
// for v1.x
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])
@ -167,19 +207,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 = rediss.NewClusterClient(&rediss.ClusterOptions{
Addrs: strings.Split(rp.savePath, ";"),
Password: rp.password,
PoolSize: rp.poolsize,
IdleTimeout: rp.idleTimeout,
IdleCheckFrequency: rp.idleCheckFrequency,
MaxRetries: rp.maxRetries,
})
return rp.poollist.Ping().Err()
}
// SessionRead read redis_cluster session by sid

View File

@ -0,0 +1,35 @@
// Copyright 2020
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis_cluster
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
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)
}