diff --git a/.gitignore b/.gitignore index 4794692..756d513 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ *.log goinit.sh gomod.sh -/vendor/ \ No newline at end of file +/vendor/ +goredis_test.go \ No newline at end of file diff --git a/app.go b/app.go deleted file mode 100644 index 3863016..0000000 --- a/app.go +++ /dev/null @@ -1,45 +0,0 @@ -package goredis - -import ( - "context" - "errors" - "fmt" - "github.com/go-redis/redis/v8" - "log" - "time" -) - -// App 实例 -type App struct { - Db *redis.Client - Addr string // 地址 - Password string // 密码 - DB int // 数据库 - PoolSize int // 连接池大小 -} - -// InitClient 初始化连接 -func (app *App) InitClient() { - - log.Printf("redis config:%+v\n", app) - - if app.PoolSize == 0 { - app.PoolSize = 100 - } - - app.Db = redis.NewClient(&redis.Options{ - Addr: app.Addr, // 地址 - Password: app.Password, // 密码 - DB: app.DB, // 数据库 - PoolSize: app.PoolSize, // 连接池大小 - }) - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - _, err := app.Db.Ping(ctx).Result() - if err != nil { - panic(errors.New(fmt.Sprintf("数据库【redis】连接失败:%v", err))) - } - return -} diff --git a/goredis.go b/goredis.go new file mode 100644 index 0000000..86181b4 --- /dev/null +++ b/goredis.go @@ -0,0 +1,48 @@ +package goredis + +import ( + "context" + "errors" + "fmt" + "github.com/go-redis/redis/v8" + "time" +) + +// ConfigClient 配置 +type ConfigClient struct { + Addr string // 地址 + Password string // 密码 + DB int // 数据库 + PoolSize int // 连接池大小 +} + +type Client struct { + Db *redis.Client + config *ConfigClient +} + +func NewClient(config *ConfigClient) *Client { + + client := &Client{config: config} + + if config.PoolSize == 0 { + config.PoolSize = 100 + } + + client.Db = redis.NewClient(&redis.Options{ + Addr: config.Addr, // 地址 + Password: config.Password, // 密码 + DB: config.DB, // 数据库 + PoolSize: config.PoolSize, // 连接池大小 + }) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + _, err := client.Db.Ping(ctx).Result() + if err != nil { + panic(errors.New(fmt.Sprintf("redis连接失败:%v", err))) + } + + return client +} diff --git a/list_operation.go b/list_operation.go index 5c2a412..ff200f6 100644 --- a/list_operation.go +++ b/list_operation.go @@ -11,8 +11,8 @@ type ListOperation struct { } // NewListOperation 列表(list)类型数据操作 https://www.tizi365.com/archives/299.html -func (app *App) NewListOperation() *ListOperation { - return &ListOperation{db: app.Db, ctx: context.Background()} +func (client *Client) NewListOperation() *ListOperation { + return &ListOperation{db: client.Db, ctx: context.Background()} } // LPush 从列表左边插入数据 diff --git a/simple_cache.go b/simple_cache.go index abcbd0a..503d080 100644 --- a/simple_cache.go +++ b/simple_cache.go @@ -24,7 +24,7 @@ type SimpleCache struct { } // NewSimpleCache 构造函数 -func (app *App) NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache { +func (client *Client) NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache { return &SimpleCache{ Operation: operation, // 操作类 Expire: expire, // 过去时间 diff --git a/simple_interface_cache.go b/simple_interface_cache.go index c80d246..8689193 100644 --- a/simple_interface_cache.go +++ b/simple_interface_cache.go @@ -15,7 +15,7 @@ type SimpleInterfaceCache struct { } // NewSimpleInterfaceCache 构造函数 -func (app *App) NewSimpleInterfaceCache(operation *SimpleOperation, expire time.Duration) *SimpleInterfaceCache { +func (client *Client) NewSimpleInterfaceCache(operation *SimpleOperation, expire time.Duration) *SimpleInterfaceCache { return &SimpleInterfaceCache{ Operation: operation, // 操作类 Expire: expire, // 过期时间 diff --git a/simple_json_cache.go b/simple_json_cache.go index a180925..03e1664 100644 --- a/simple_json_cache.go +++ b/simple_json_cache.go @@ -15,7 +15,7 @@ type SimpleJsonCache struct { } // NewSimpleJsonCache 构造函数 -func (app *App) NewSimpleJsonCache(operation *StringOperation, expire time.Duration) *SimpleJsonCache { +func (client *Client) NewSimpleJsonCache(operation *StringOperation, expire time.Duration) *SimpleJsonCache { return &SimpleJsonCache{ Operation: operation, // 操作类 Expire: expire, // 过期时间 diff --git a/simple_operation.go b/simple_operation.go index d479f9f..6d3ca62 100644 --- a/simple_operation.go +++ b/simple_operation.go @@ -11,9 +11,9 @@ type SimpleOperation struct { ctx context.Context } -func (app *App) NewSimpleOperation() *SimpleOperation { +func (client *Client) NewSimpleOperation() *SimpleOperation { return &SimpleOperation{ - db: app.Db, + db: client.Db, ctx: context.Background(), } } diff --git a/simple_sring_cache.go b/simple_sring_cache.go index 9d49c86..13d5c39 100644 --- a/simple_sring_cache.go +++ b/simple_sring_cache.go @@ -14,7 +14,7 @@ type SimpleStringCache struct { } // NewSimpleStringCache 构造函数 -func (app *App) NewSimpleStringCache(operation *StringOperation, expire time.Duration) *SimpleStringCache { +func (client *Client) NewSimpleStringCache(operation *StringOperation, expire time.Duration) *SimpleStringCache { return &SimpleStringCache{ Operation: operation, // 操作类 Expire: expire, // 过期时间 diff --git a/string_operation.go b/string_operation.go index e9183ae..c0e5277 100644 --- a/string_operation.go +++ b/string_operation.go @@ -11,9 +11,9 @@ type StringOperation struct { ctx context.Context } -func (app *App) NewStringOperation() *StringOperation { +func (client *Client) NewStringOperation() *StringOperation { return &StringOperation{ - db: app.Db, + db: client.Db, ctx: context.Background(), } } diff --git a/version.go b/version.go index ce28751..76bab73 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package goredis -const Version = "1.0.0" +const Version = "1.0.1"