redis配置默认值

This commit is contained in:
suguo.yao 2023-10-31 14:03:34 +08:00
parent 9b7f627b0f
commit 8de9252ae5
1 changed files with 25 additions and 19 deletions

View File

@ -9,7 +9,7 @@ import (
var pool *redigo.Pool var pool *redigo.Pool
//Config 配置 // Config 配置
type Config struct { type Config struct {
Host string `yml:"host" json:"host"` Host string `yml:"host" json:"host"`
Password string `yml:"password" json:"password"` Password string `yml:"password" json:"password"`
@ -19,17 +19,23 @@ type Config struct {
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` //second IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` //second
} }
//Init init // Init init
func Init(opts *Config) error { func Init(opts *Config) error {
if opts == nil { if opts == nil {
opts = &Config{ opts = &Config{}
Host: "127.0.0.1:6379", }
Password: "",
Database: 0, if opts.Host == "" {
MaxIdle: 10, opts.Host = "127.0.0.1:6379"
MaxActive: 100, }
IdleTimeout: 600, if opts.MaxIdle == 0 {
} opts.MaxIdle = 1
}
if opts.MaxActive == 0 {
opts.MaxActive = 10
}
if opts.IdleTimeout == 0 {
opts.IdleTimeout = 600
} }
pool = &redigo.Pool{ pool = &redigo.Pool{
@ -53,7 +59,7 @@ func Init(opts *Config) error {
return nil return nil
} }
//GetBytes 获取一个字节数组值 // GetBytes 获取一个字节数组值
func GetBytes(key *string) (*[]byte, error) { func GetBytes(key *string) (*[]byte, error) {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -62,7 +68,7 @@ func GetBytes(key *string) (*[]byte, error) {
return &data, err return &data, err
} }
//Get 获取一个值 // Get 获取一个值
func Get(key string) interface{} { func Get(key string) interface{} {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -80,7 +86,7 @@ func Get(key string) interface{} {
return reply return reply
} }
//集合Set增加元素 // 集合Set增加元素
func SetAdd(key string, data ...interface{}) error { func SetAdd(key string, data ...interface{}) error {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -95,7 +101,7 @@ func SetAdd(key string, data ...interface{}) error {
return err return err
} }
//集合Set删除元素 // 集合Set删除元素
func SetRem(key string, data ...interface{}) error { func SetRem(key string, data ...interface{}) error {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -112,7 +118,7 @@ func SetRem(key string, data ...interface{}) error {
return err return err
} }
//集合Set判断是否存在成员member结果.(int64)==1表示存在 // 集合Set判断是否存在成员member结果.(int64)==1表示存在
func SetIsMember(key string, member interface{}) (interface{}, error) { func SetIsMember(key string, member interface{}) (interface{}, error) {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -120,7 +126,7 @@ func SetIsMember(key string, member interface{}) (interface{}, error) {
return conn.Do("SISMEMBER", key, member) return conn.Do("SISMEMBER", key, member)
} }
//设置一个值 // 设置一个值
func Set(key string, val interface{}, timeout time.Duration) error { func Set(key string, val interface{}, timeout time.Duration) error {
data, err := json.Marshal(val) data, err := json.Marshal(val)
if err != nil { if err != nil {
@ -137,7 +143,7 @@ func SetBytes(key *string, data *[]byte, timeout time.Duration) error {
return err return err
} }
//IsExist 判断key是否存在 // IsExist 判断key是否存在
func IsExist(key string) bool { func IsExist(key string) bool {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -147,7 +153,7 @@ func IsExist(key string) bool {
return i > 0 return i > 0
} }
//Delete 删除 // Delete 删除
func Delete(key string) error { func Delete(key string) error {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
@ -159,7 +165,7 @@ func Delete(key string) error {
return nil return nil
} }
//Expire 失效时间配置 // Expire 失效时间配置
func Expire(key string, t int64) error { func Expire(key string, t int64) error {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()