update redis

master
李光春 2 years ago
parent e7f43e07c2
commit ad44d7efec

@ -0,0 +1,35 @@
package goredis
import (
"context"
"github.com/go-redis/redis/v8"
"time"
)
// App 实例
type App struct {
Rdb *redis.Client
Addr string // 地址
Password string // 密码
DB int // 数据库
PoolSize int // 连接池大小
}
// InitClient 初始化连接 普通连接
func (app *App) InitClient() (err error) {
if app.PoolSize == 0 {
app.PoolSize = 100
}
app.Rdb = 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.Rdb.Ping(ctx).Result()
return err
}

@ -1,65 +0,0 @@
package goredis
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var (
Rdb *redis.Client
RdbC *redis.ClusterClient
)
// InitRedis 初始化连接 普通连接
func InitRedis(host string, port int, password string, db int) (err error) {
dsn := fmt.Sprintf("%s:%v", host, port)
fmt.Printf("【redis.普通】数据库配置 %s \n", dsn)
Rdb = redis.NewClient(&redis.Options{
Addr: dsn,
Password: password, // no password set
DB: db, // use default DB
PoolSize: 100, // 连接池大小
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = Rdb.Ping(ctx).Result()
return err
}
// InitSentinelRedis 初始化连接 哨兵模式
func InitSentinelRedis(adds []string, masterName string, password string, db int) (err error) {
fmt.Printf("【redis.哨兵】数据库配置 %s \n", adds)
Rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: masterName,
SentinelAddrs: adds,
Password: password, // no password set
DB: db, // use default DB
PoolSize: 100, // 连接池大小
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = Rdb.Ping(ctx).Result()
return err
}
// InitClusterRedis 初始化连接 集群
func InitClusterRedis(adds []string, password string) (err error) {
fmt.Printf("【redis.集群】数据库配置 %v \n", adds)
RdbC = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: adds,
Password: password, // no password set
PoolSize: 100, // 连接池大小
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = RdbC.Ping(ctx).Result()
return err
}

@ -1,62 +0,0 @@
package goredis
import (
"testing"
)
func TestSet(t *testing.T) {
//// 连接
//err := InitRedis("127.0.0.1", 6379, "", 2)
//if err != nil {
// fmt.Printf("err%v\n", err)
//}
//// 设置
//NewStringOperation().Set("test", "test", WithExpire(time.Second*1))
}
func TestMGet(t *testing.T) {
//// 连接
//err := InitRedis("127.0.0.1", 6379, "", 2)
//if err != nil {
// fmt.Printf("err%v\n", err)
//}
//// 获取
//iter := NewStringOperation().MGet("test1", "test2").Iter()
//for iter.HasNext() {
// fmt.Println("MGet", iter.Next())
//}
}
func TestJson(t *testing.T) {
//// 连接
//err := InitRedis("127.0.0.1", 6379, "", 2)
//if err != nil {
// fmt.Printf("err%v\n", err)
//}
//newCache := NewSimpleCache(NewStringOperation(), time.Second*10, SerializerJson)
//newCache.JsonGetter = func() interface{} {
// fmt.Println("【没有命中】SerializerJson")
// type a []string
// b := a{
// "me", "she", "you",
// }
// return b
//}
//cacheJSon := newCache.GetCache("test123")
//fmt.Printf("【GetCache】cacheJSon%v\n", cacheJSon)
}
func TestDbString(t *testing.T) {
//// 连接
//err := InitRedis("127.0.0.1", 6379, "", 2)
//if err != nil {
// fmt.Printf("err%v\n", err)
//}
//newCache := NewSimpleCache(NewStringOperation(), time.Second*10, SerializerString)
//newCache.DBGetter = func() string {
// fmt.Println("【没有命中】SerializerString")
// return "data by id=123"
//}
//cacheString := newCache.GetCache("test456")
//fmt.Printf("【GetCache】cacheString%v\n", cacheString)
}

@ -5,8 +5,8 @@ import "time"
type empty struct{}
const (
AttrExpr = "expr" //过期时间
AttrNx = "nx" // setNx
AttrExpr = "expr" // 过期时间
AttrNx = "nx" // 设置Nx
)
type OperationAttr struct {

@ -23,8 +23,12 @@ type SimpleCache struct {
Serializer string // 序列化方式
}
func NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache {
return &SimpleCache{Operation: operation, Expire: expire, Serializer: serializer}
func (app *App) NewSimpleCache(operation *StringOperation, expire time.Duration, serializer string) *SimpleCache {
return &SimpleCache{
Operation: operation, // 操作类
Expire: expire, // 过去时间
Serializer: serializer, // 缓存不存在的操作 DB
}
}
// SetCache 设置缓存

@ -2,15 +2,20 @@ package goredis
import (
"context"
"github.com/go-redis/redis/v8"
"time"
)
type StringOperation struct {
db *redis.Client
ctx context.Context
}
func NewStringOperation() *StringOperation {
return &StringOperation{ctx: context.Background()}
func (app *App) NewStringOperation() *StringOperation {
return &StringOperation{
db: app.Rdb,
ctx: context.Background(),
}
}
// Set 设置
@ -19,15 +24,15 @@ func (o *StringOperation) Set(key string, value interface{}, attrs ...*Operation
if exp == nil {
exp = time.Second * 0
}
return NewStringResult(Rdb.Set(o.ctx, key, value, exp.(time.Duration)).Result())
return NewStringResult(o.db.Set(o.ctx, key, value, exp.(time.Duration)).Result())
}
// Get 获取单个
func (o *StringOperation) Get(key string) *StringResult {
return NewStringResult(Rdb.Get(o.ctx, key).Result())
return NewStringResult(o.db.Get(o.ctx, key).Result())
}
// MGet 获取多个
func (o *StringOperation) MGet(keys ...string) *SliceResult {
return NewSliceResult(Rdb.MGet(o.ctx, keys...).Result())
return NewSliceResult(o.db.MGet(o.ctx, keys...).Result())
}

Loading…
Cancel
Save