Chore: correct the `DB` property of `RedisConfig` to int type

Converting Int to String is safer than converting String to Int.
pull/1475/head
AH-dark 3 years ago
parent cc303e74dd
commit 50c3100afb
No known key found for this signature in database
GPG Key ID: 73D3D212EB75AB80

@ -8,6 +8,7 @@ import (
"github.com/gin-contrib/sessions/memstore" "github.com/gin-contrib/sessions/memstore"
"github.com/gin-contrib/sessions/redis" "github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
) )
// Store session存储 // Store session存储
@ -18,7 +19,7 @@ func Session(secret string) gin.HandlerFunc {
// Redis设置不为空且非测试模式时使用Redis // Redis设置不为空且非测试模式时使用Redis
if conf.RedisConfig.Server != "" && gin.Mode() != gin.TestMode { if conf.RedisConfig.Server != "" && gin.Mode() != gin.TestMode {
var err error var err error
Store, err = redis.NewStoreWithDB(10, conf.RedisConfig.Network, conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, []byte(secret)) Store, err = redis.NewStoreWithDB(10, conf.RedisConfig.Network, conf.RedisConfig.Server, conf.RedisConfig.Password, strconv.Itoa(conf.RedisConfig.DB), []byte(secret))
if err != nil { if err != nil {
util.Log().Panic("无法连接到 Redis%s", err) util.Log().Panic("无法连接到 Redis%s", err)
} }

10
pkg/cache/redis.go vendored

@ -3,7 +3,6 @@ package cache
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"strconv"
"time" "time"
"github.com/cloudreve/Cloudreve/v3/pkg/util" "github.com/cloudreve/Cloudreve/v3/pkg/util"
@ -44,7 +43,7 @@ func deserializer(value []byte) (interface{}, error) {
} }
// NewRedisStore 创建新的redis存储 // NewRedisStore 创建新的redis存储
func NewRedisStore(size int, network, address, password, database string) *RedisStore { func NewRedisStore(size int, network, address, password string, database int) *RedisStore {
return &RedisStore{ return &RedisStore{
pool: &redis.Pool{ pool: &redis.Pool{
MaxIdle: size, MaxIdle: size,
@ -54,15 +53,10 @@ func NewRedisStore(size int, network, address, password, database string) *Redis
return err return err
}, },
Dial: func() (redis.Conn, error) { Dial: func() (redis.Conn, error) {
db, err := strconv.Atoi(database)
if err != nil {
return nil, err
}
c, err := redis.Dial( c, err := redis.Dial(
network, network,
address, address,
redis.DialDatabase(db), redis.DialDatabase(database),
redis.DialPassword(password), redis.DialPassword(password),
) )
if err != nil { if err != nil {

@ -52,7 +52,7 @@ type redis struct {
Network string Network string
Server string Server string
Password string Password string
DB string DB int
} }
// 跨域配置 // 跨域配置

@ -7,7 +7,7 @@ var RedisConfig = &redis{
Network: util.EnvStr("REDIS_NETWORK", "tcp"), Network: util.EnvStr("REDIS_NETWORK", "tcp"),
Server: util.EnvStr("REDIS_SERVER", ""), Server: util.EnvStr("REDIS_SERVER", ""),
Password: util.EnvStr("REDIS_PASSWORD", ""), Password: util.EnvStr("REDIS_PASSWORD", ""),
DB: util.EnvStr("REDIS_DB", "0"), DB: util.EnvInt("REDIS_DB", 0),
} }
// DatabaseConfig 数据库配置 // DatabaseConfig 数据库配置

Loading…
Cancel
Save