package redis_lock_test
import (
"context"
"errors"
"strings"
"time"
"github.com/gomodule/redigo/redis"
)
type LockClient interface {
SetNEX(ctx context.Context, key, value string, expireSeconds int64) (int64, error)
Eval(ctx context.Context, src string, keyCount int, keyAndArgs []interface{}) (interface{}, error)
}
// Client Redis 客户端
type Client struct {
pool *redis.Pool
ClientOptions
}
// 创建Redis客户端
func NewClient(netWork, address, password string, clientOption ...ClientOption) *Client {
c := Client{
ClientOptions: ClientOptions{
netWork: netWork,
address: address,
password: password,
},
}
for _, opt := range clientOption {
opt(&c.ClientOptions)
}
RepairClient(&c.ClientOptions)
pool := c.getRedisPool()
return &Client{
pool: pool,
}
}
// 创建redis连接池
func (c *Client) getRedisPool() *redis.Pool {
return &redis.Pool{
// 最大空闲连接
MaxIdle: c.maxIdle,
// 空闲等待超时时间
IdleTimeout: time.Duration(c.idleTimeoutSeconds) * time.Second,
// 最大连接鼠粮
MaxActive: c.maxActive,
// 阻塞模式下是否等待连接
Wait: c.wait,
// 创建redis连接的函数
Dial: func() (redis.Conn, error) {
conn, err := c.getRedisConn()
if err != nil {
return nil, err
}
return conn, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
// 与redis建立连接
func (c *Client) getRedisConn() (redis.Conn, error) {
if c.address == "" {
panic("Cannot get redis address from config")
}
// 加载redis密码进入连接配置
var dialOpts []redis.DialOption
if len(c.password) > 0 {
dialOpts = append(dialOpts, redis.DialPassword(c.password))
}
conn, err := redis.DialContext(context.Background(), c.netWork, c.address, dialOpts...)
if err != nil {
return nil, err
}
return conn, nil
}
// 从redis的连接池获取连接
func (c *Client) GetConn(ctx context.Context) (redis.Conn, error) {
return c.pool.GetContext(ctx)
}
// 封装redis的get方法
func (c *Client) Get(ctx context.Context, key string) (string, error) {
if key == "" {
return "", errors.New("redis GET key can't be empty")
}
conn, err := c.pool.GetContext(ctx)
if err != nil {
return "", err
}
defer conn.Close()
return redis.String(conn.Do("GET", key))
}
// 封装redis的set方法
func (c *Client) Set(ctx context.Context, key, value string) (int64, error) {
if key == "" || value == "" {
return -1, errors.New("redis SET key or value can't be empty")
}
conn, err := c.pool.GetContext(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
return redis.Int64(conn.Do("SET", key, value))
}
// 封装redis的setnx方法
func (c *Client) SetNX(ctx context.Context, key, value string) (int64, error) {
if key == "" || value == "" {
return -1, errors.New("redis SET key or value can't be empty")
}
conn, err := c.pool.GetContext(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
return redis.Int64(conn.Do("SET", key, value, "NX"))
}
// 封装redis的setnex方法
func (c *Client) SetNEX(ctx context.Context, key, value string, expireSeconds int64) (int64, error) {
if key == "" || value == "" {
return -1, errors.New("redis SET key or value can't be empty")
}
conn, err := c.pool.GetContext(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
reply, err := conn.Do("SET", key, value, "EX", expireSeconds, "NX")
if err != nil {
return -1, nil
}
if respStr, ok := reply.(string); ok && strings.ToLower(respStr) == "ok" {
return 1, nil
}
return redis.Int64(reply, err)
// return redis.Int64(conn.Do("SET", key, value, "EX", expireSeconds, "NX"))
}
// 封装redis的Del方法
func (c *Client) Del(ctx context.Context, key string) (int64, error) {
if key == "" {
return -1, errors.New("redis DEL key can't be empty")
}
conn, err := c.pool.GetContext(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
return redis.Int64(conn.Do("DEL", key))
}
// 封装redis的incr方法
func (c *Client) Incr(ctx context.Context, key string) (int64, error) {
if key == "" {
return -1, errors.New("redis INCR key can't be empty")
}
conn, err := c.pool.GetContext(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
return redis.Int64(conn.Do("INCR", key))
}
// Eval 支持使用lua脚本
func (c *Client) Eval(ctx context.Context, src string, keyCount int, keyAndArgs []interface{}) (interface{}, error) {
args := make([]interface{}, 2+len(keyAndArgs))
// lua语句变量名称
args[0] = src
// 有多少个键
args[1] = keyCount
copy(args[2:], keyAndArgs)
conn, err := c.pool.GetContext(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
return conn.Do("EVAL", args...)
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
redis-lock-test.rar (10个子文件)
redis-lock-test
go.mod 172B
lock.go 5KB
go.sum 1KB
lock_test.go 2KB
redLock.go 2KB
utils
os.go 574B
redis.go 5KB
expireLock.go 2KB
option.go 4KB
lua.go 701B
共 10 条
- 1
资源评论
lunar@qq.com
- 粉丝: 7
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功