- update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

master v1.0.1
李光春 2 years ago
parent 8a3b3100f8
commit 261f847d33

3
.gitignore vendored

@ -6,4 +6,5 @@
*.log
goinit.sh
gomod.sh
/vendor/
/vendor/
goredis_test.go

@ -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
}

@ -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
}

@ -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 从列表左边插入数据

@ -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, // 过去时间

@ -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, // 过期时间

@ -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, // 过期时间

@ -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(),
}
}

@ -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, // 过期时间

@ -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(),
}
}

@ -1,3 +1,3 @@
package goredis
const Version = "1.0.0"
const Version = "1.0.1"

Loading…
Cancel
Save