From 8de9252ae5e963ee1fab384b856cc65992e90b20 Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Tue, 31 Oct 2023 14:03:34 +0800 Subject: [PATCH] =?UTF-8?q?redis=E9=85=8D=E7=BD=AE=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis/redis.go | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/redis/redis.go b/redis/redis.go index 890d292..ae5f619 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -9,7 +9,7 @@ import ( var pool *redigo.Pool -//Config 配置 +// Config 配置 type Config struct { Host string `yml:"host" json:"host"` Password string `yml:"password" json:"password"` @@ -19,17 +19,23 @@ type Config struct { IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` //second } -//Init init +// Init init func Init(opts *Config) error { if opts == nil { - opts = &Config{ - Host: "127.0.0.1:6379", - Password: "", - Database: 0, - MaxIdle: 10, - MaxActive: 100, - IdleTimeout: 600, - } + opts = &Config{} + } + + if opts.Host == "" { + opts.Host = "127.0.0.1:6379" + } + if opts.MaxIdle == 0 { + opts.MaxIdle = 1 + } + if opts.MaxActive == 0 { + opts.MaxActive = 10 + } + if opts.IdleTimeout == 0 { + opts.IdleTimeout = 600 } pool = &redigo.Pool{ @@ -53,7 +59,7 @@ func Init(opts *Config) error { return nil } -//GetBytes 获取一个字节数组值 +// GetBytes 获取一个字节数组值 func GetBytes(key *string) (*[]byte, error) { conn := pool.Get() defer conn.Close() @@ -62,7 +68,7 @@ func GetBytes(key *string) (*[]byte, error) { return &data, err } -//Get 获取一个值 +// Get 获取一个值 func Get(key string) interface{} { conn := pool.Get() defer conn.Close() @@ -80,7 +86,7 @@ func Get(key string) interface{} { return reply } -//集合Set增加元素 +// 集合Set增加元素 func SetAdd(key string, data ...interface{}) error { conn := pool.Get() defer conn.Close() @@ -95,7 +101,7 @@ func SetAdd(key string, data ...interface{}) error { return err } -//集合Set删除元素 +// 集合Set删除元素 func SetRem(key string, data ...interface{}) error { conn := pool.Get() defer conn.Close() @@ -112,7 +118,7 @@ func SetRem(key string, data ...interface{}) error { return err } -//集合Set判断是否存在成员member,结果.(int64)==1表示存在 +// 集合Set判断是否存在成员member,结果.(int64)==1表示存在 func SetIsMember(key string, member interface{}) (interface{}, error) { conn := pool.Get() defer conn.Close() @@ -120,7 +126,7 @@ func SetIsMember(key string, member interface{}) (interface{}, error) { return conn.Do("SISMEMBER", key, member) } -//设置一个值 +// 设置一个值 func Set(key string, val interface{}, timeout time.Duration) error { data, err := json.Marshal(val) if err != nil { @@ -137,7 +143,7 @@ func SetBytes(key *string, data *[]byte, timeout time.Duration) error { return err } -//IsExist 判断key是否存在 +// IsExist 判断key是否存在 func IsExist(key string) bool { conn := pool.Get() defer conn.Close() @@ -147,7 +153,7 @@ func IsExist(key string) bool { return i > 0 } -//Delete 删除 +// Delete 删除 func Delete(key string) error { conn := pool.Get() defer conn.Close() @@ -159,7 +165,7 @@ func Delete(key string) error { return nil } -//Expire 失效时间配置 +// Expire 失效时间配置 func Expire(key string, t int64) error { conn := pool.Get() defer conn.Close()