redisdb

package
v1.6.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MaxGoroutinePoolSize = 5

	MGetCostMax                 = 60
	MSetCostMax                 = 60
	MDelCostMax                 = 60
	RedisClusterCommonCostMax   = 20
	RedisClusterCmdNormal       = "redis_cluster_cmd_normal"
	RedisClusterCmd             = "redis_cluster_cmd_%v"
	RedisClusterCmdSlowCount    = "redis_cluster_%v_slow_count"
	RedisClusterNormalSlowCount = "redis_cluster_common_slow_count"
)
View Source
const (
	DefaultDatabases = 0
	DefaultTimeout   = 15 * time.Second
	DefaultMaxIdle   = 1
	DefaultMaxActive = 0

	RedisPoolCommonCostMax   = 20
	RedisPoolCmdNormal       = "redis_pool_cmd_normal"
	RedisPoolCmd             = "redis_pool_cmd_%v"
	RedisPoolCmdSlowCount    = "redis_pool_%v_slow_count"
	RedisPoolNormalSlowCount = "redis_pool_common_slow_count"
)

Variables

View Source
var ErrNil = errors.New("redis: nil returned")

Functions

This section is empty.

Types

type BatchReply

type BatchReply struct {
	Req   *BatchReq
	Reply interface{}
	Err   error
}

type BatchReq

type BatchReq struct {
	Key  string
	Args interface{}
}

type RedisCluster

type RedisCluster struct {
	// contains filtered or unexported fields
}

func NewRedisCluster

func NewRedisCluster(clusterConf *RedisClusterConf) (*RedisCluster, error)

func (*RedisCluster) Close

func (r *RedisCluster) Close() error

func (*RedisCluster) Del

func (r *RedisCluster) Del(key string) (int64, error)

* 1. 若正常, 返回 (num,nil), num为删除的key个数 2. 若异常, 返回 (0,error)

func (*RedisCluster) Eval

func (r *RedisCluster) Eval(script string, keys []string, args []interface{}) (interface{}, error)

func (*RedisCluster) EvalSha

func (r *RedisCluster) EvalSha(scriptSha string, keys []string, args []interface{}) (interface{}, error)

func (*RedisCluster) Exist

func (r *RedisCluster) Exist(key string) (bool, error)

func (*RedisCluster) Expire

func (r *RedisCluster) Expire(key string, expiration time.Duration) (bool, error)

*

  1. 若正常, 返回 (true,nil)
  2. 若异常, 返回 (false,error) key不存在返回(false,nil)

func (*RedisCluster) Get

func (r *RedisCluster) Get(key string) (string, error)

* 1. 若key存在且成功, 返回(string,nil) 2. 若key不存在且成功, 返回("",redisCluster.ErrNil) 3. 若异常, 返回("",error)

func (*RedisCluster) HGet

func (r *RedisCluster) HGet(key string, field string) (string, error)

* 1. 异常, 返回 ("",error) 2. 正常, 但key不存在或者field不存在, 返回 ("",redisCluster.ErrNil) 3. 正常, 且key存在, filed存在, 返回 (string, nil)

func (*RedisCluster) HGetAll

func (r *RedisCluster) HGetAll(key string) (map[string]string, error)

* 1. 若异常, 返回 (nil, error) 2. 若正常, 返回 (map[string]string, nil)

func (*RedisCluster) HIncrBy

func (r *RedisCluster) HIncrBy(key string, field string, value int64) (int64, error)

* 1. 若设置成功, 返回(int64,nil) 2. 若异常, 返回(-1,error)

func (*RedisCluster) HLen

func (r *RedisCluster) HLen(key string) (int64, error)

func (*RedisCluster) HMDel

func (r *RedisCluster) HMDel(key string, fields []string) (int64, error)

func (*RedisCluster) HMGet

func (r *RedisCluster) HMGet(key string, fields []string) ([]interface{}, error)

*

  1. 异常, 返回 (nil, error)
  2. 正常, 返回 ([]interface{}, nil), 其中slice里的值顺序与fields一一对应, 对于不存在的field, 对应值为nil 例如: HMGet("key", []string{"field1","field2","field3"}) 返回值: []interface{}{ "value1", //field1的值 nil, //field2不存在 "value3" //field3的值 },nil 备注: 若key不存在, slice里的所有值都为nil

func (*RedisCluster) HMSet

func (r *RedisCluster) HMSet(key string, fields map[string]string) (bool, error)

* 1. 若异常, 返回 (false, error) 2. 若正常, 返回 (true, nil)

func (*RedisCluster) HScan

func (r *RedisCluster) HScan(key string, count int64) (map[string]string, error)

* 1. 若异常, 返回 (nil, error) 2. 若正常, 返回 (map[string]string, nil)

Notice: 这个函数并不能严格的限制返回count个,简单测试看起来主要和redis用的存储结构有关。

Hmap redis在存储的时候如果数据比较少(看文章是512)会使用ziplist,测试了下,在ziplist存储的状态下,会都返回,count不生效
如果数据超过512之后会使用hmap来存储,这时基本就是准确的了。所以这个函数只能保证返回 >= count

func (*RedisCluster) HSet

func (r *RedisCluster) HSet(key string, field string, value string) (bool, error)

* 1. 若异常, 返回 (false, error) 2. 若正常, 返回 (bool, nil), 其中true: field在hash中不存在且新增成功; false: field已在hash中存在且更新成功.

func (*RedisCluster) HSetNx

func (r *RedisCluster) HSetNx(key string, field string, value string) (bool, error)

* 1. 若异常, 返回 (false, error) 2. 若正常, 返回 (bool, nil), 其中true: field在hash中不存在且新增成功; false: field已在hash中存在, 不做更新.

func (*RedisCluster) IncrBy

func (r *RedisCluster) IncrBy(key string, value int64) (int64, error)

func (*RedisCluster) LIndex

func (r *RedisCluster) LIndex(key string, index int64) (string, error)

func (*RedisCluster) LPop

func (r *RedisCluster) LPop(key string) (string, error)

func (*RedisCluster) LPush

func (r *RedisCluster) LPush(key string, value string) (int64, error)

func (*RedisCluster) MDel

func (r *RedisCluster) MDel(keys []string) (map[string]bool, error)

func (*RedisCluster) MDel2

func (r *RedisCluster) MDel2(keys []string) (map[string]bool, error)

* 1. err!=nil, 在err!=nil的情况下,若ret为空,则表示都失败了 2. ret标识每个key对应的成功与否,true成功,false失败 有如下情况: ret==nil, err!=nil: 全部失败 ret!=nil, err!=nil: 部分失败, ret会包含所有key的操作结果 ret!=nil, err==nil: 全部成功, ret会包含所有key的操作结果

func (*RedisCluster) MGet

func (r *RedisCluster) MGet(keys []string) ([]interface{}, error)

*

  1. 若有异常, 返回 (nil,error)
  2. 若正常, 返回 ([]interface{}{...},nil), 其中结果里的value顺序与keys的顺序一一对应, 若某个key不存在, 所对应value为nil 例如 MGet([]string{"key1","key2","key3"}), 假设key2不存在, 返回数据如下: []interface{}{ "value1", //key1的值 nil, //不存在 "value3", //key3的值 }, nil

func (*RedisCluster) MGet2

func (r *RedisCluster) MGet2(keys []string) ([]interface{}, error)

*

  1. 若有异常, 返回 (nil,error)
  2. 若正常, 返回 ([]interface{}{...},nil), 其中结果里的value顺序与keys的顺序一一对应, 若某个key不存在, 所对应value为nil 例如 MGet([]string{"key1","key2","key3"}), 假设key2不存在, 返回数据如下: []interface{}{ "value1", //key1的值 nil, //不存在 "value3", //key3的值 }, nil

func (*RedisCluster) MSet

func (r *RedisCluster) MSet(kvs map[string]string, expire time.Duration) (map[string]bool, error)

func (*RedisCluster) MSet2

func (r *RedisCluster) MSet2(kvs map[string]string, expire time.Duration) (map[string]bool, error)

* 1. err!=nil, 在err!=nil的情况下,若ret为空,则表示都失败了 2. ret标识每个key对应的成功与否,true成功,false失败 有如下情况: ret==nil, err!=nil: 全部失败 ret!=nil, err!=nil: 部分失败, ret里会包含所有key的操作结果 ret!=nil, err==nil: 全部成功, ret里会包含所有key的操作结果

备注: expire为0表示key不过期

func (*RedisCluster) PExpire

func (r *RedisCluster) PExpire(key string, expiration time.Duration) (bool, error)

*

  1. 若正常, 返回 (true,nil)
  2. 若异常, 返回 (false,error) key不存在返回(false,nil)

func (*RedisCluster) PTtl

func (r *RedisCluster) PTtl(key string) (time.Duration, error)

*

  1. 若正常, 返回 (true,nil)
  2. 若异常, 返回 (false,error) key不存在返回(false,nil)

func (*RedisCluster) RPush

func (r *RedisCluster) RPush(key string, value string) (int64, error)

func (*RedisCluster) SAdd

func (r *RedisCluster) SAdd(key string, members []interface{}) (int64, error)

func (*RedisCluster) SPop

func (r *RedisCluster) SPop(key string) (string, error)

func (*RedisCluster) Set

func (r *RedisCluster) Set(key string, value string, expire time.Duration) (string, error)

* 1. 若expire为0, 表示不设置过期 2. 若设置成功, 返回("ok",nil) 3. 若异常, 返回("",error)

func (*RedisCluster) SetBit

func (r *RedisCluster) SetBit(key string, offset int64, value int) (int64, error)

func (*RedisCluster) SetNX

func (r *RedisCluster) SetNX(key string, value string, expire time.Duration) (bool, error)

* 1. 若expire为0, 表示不设置过期 2. 如果 err 不为空, 则发生异常; 3. 在 err 为空的情况下, bool=false 表示key已存在set无效, bool=true表示key不存在set成功 https://redis.io/commands/setnx

func (*RedisCluster) ZAdd

func (r *RedisCluster) ZAdd(key string, members []redis.Z) (int64, error)

func (*RedisCluster) ZRange

func (r *RedisCluster) ZRange(key string, start, stop int64) ([]string, error)

func (*RedisCluster) ZRangeByScoreWithScores

func (r *RedisCluster) ZRangeByScoreWithScores(key string, min, max string, offset, limit int64) ([]*ZSetResult, error)

func (*RedisCluster) ZRem

func (r *RedisCluster) ZRem(key string, members ...interface{}) (int64, error)

func (*RedisCluster) ZRemRangeByRank

func (r *RedisCluster) ZRemRangeByRank(key string, start, stop int64) (int64, error)

func (*RedisCluster) ZRemRangeByScore

func (r *RedisCluster) ZRemRangeByScore(key string, min, max string) (int64, error)

func (*RedisCluster) ZRevRange

func (r *RedisCluster) ZRevRange(key string, start, stop int64) ([]string, error)

func (*RedisCluster) ZRevRangeByScoreWithScores

func (r *RedisCluster) ZRevRangeByScoreWithScores(key string, min, max string, offset, limit int64) ([]*ZSetResult, error)

type RedisClusterConf

type RedisClusterConf struct {
	//cluster name
	ClusterName string

	//server address
	Addrs []string

	//conn/read/write timeout, 单位: 毫秒, 0表示默认值(1秒), -1表示不超时
	DialTimeout  int64
	ReadTimeout  int64
	WriteTimeout int64

	//pool config
	//每个redis节点会起一个连接池, 该poolsize表示每个redis节点的最大连接数
	PoolSize int
	//从连接池获取可用连接的超时, 单位: 毫秒, 0表示默认值(1秒), -1表示不超时
	PoolTimeout int64
	//最小空闲连接数
	MinIdleConns int
	//空闲连接可被回收的判断阈值, 单位: 秒
	IdleTimeout int64
	//空闲连接检查频率, 单位: 秒, 0表示默认值(30分钟), -1表示不做检查
	IdleCheckFrequency int64

	//当访问的key不在某节点或者某节点有异常, 会做move(redirect)操作, 该参数表示最大move操作次数, 0表示默认值(2次)
	MaxRedirects int
}

type RedisClusterCustomError

type RedisClusterCustomError string

func (RedisClusterCustomError) Error

func (e RedisClusterCustomError) Error() string

type RedisConfig

type RedisConfig struct {
	Addrs          []string
	MaxActive      int
	MaxIdle        int
	Retry          int
	IdleTimeoutSec int //空闲连接可被回收的判断阈值, 单位: 秒
	ConnTimeoutMs  int64
	ReadTimeoutMs  int64
	WriteTimeoutMs int64
	Password       string
}

func RedisConfigFromURLString

func RedisConfigFromURLString(rawUrl string) (*RedisConfig, error)

type RedisPool

type RedisPool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(servers []string, password string, maxActive, maxIdle, idleTimeout int) *RedisPool

func NewPoolRetry

func NewPoolRetry(servers []string, password string, maxActive, maxIdle, idleTimeout int, retry int) *RedisPool

func NewPoolRetryTimeout

func NewPoolRetryTimeout(servers []string, password string, maxActive, maxIdle, idleTimeout int, retry int, connTimeout, readTimeout, writeTimeout time.Duration) *RedisPool

func NewRedisPools

func NewRedisPools(cfg *RedisConfig) (*RedisPool, error)

func (*RedisPool) ActiveCount

func (p *RedisPool) ActiveCount() int

func (*RedisPool) BatchDo

func (p *RedisPool) BatchDo(commandName string, req []*BatchReq) (reply []*BatchReply)

func (*RedisPool) Close

func (p *RedisPool) Close()

func (RedisPool) Del

func (p RedisPool) Del(key string) (err error)

func (*RedisPool) Do

func (p *RedisPool) Do(commandName string, args ...interface{}) (reply interface{}, err error)

func (RedisPool) Exists

func (p RedisPool) Exists(key string) (bool, error)

func (RedisPool) Expire

func (p RedisPool) Expire(key string, time int) (int, error)

func (RedisPool) Get

func (p RedisPool) Get(key string) (ret string, errRet error)

* Redis get return string if exist

err = redis.ErrNil if not exist

func (RedisPool) HDel

func (p RedisPool) HDel(key, field string) (err error)

func (RedisPool) HGet

func (p RedisPool) HGet(key string, field string) (string, error)

func (RedisPool) HGetAll

func (p RedisPool) HGetAll(key string) (ret map[string]string, err error)

func (RedisPool) HIncrBy

func (p RedisPool) HIncrBy(key, field string, val int64) (int64, error)

func (RedisPool) HLen

func (p RedisPool) HLen(key string) (int64, error)

func (RedisPool) HMDel

func (p RedisPool) HMDel(key string, values []string) (int64, error)

func (RedisPool) HMGet

func (p RedisPool) HMGet(key string, values []string) ([]string, error)

func (RedisPool) HScan

func (p RedisPool) HScan(key string, count int64) (ret map[string]string, err error)

func (RedisPool) HSet

func (p RedisPool) HSet(key string, field string, value string) (err error)

func (RedisPool) Incr

func (p RedisPool) Incr(key string) (int, error)

func (RedisPool) IncrBy

func (p RedisPool) IncrBy(key string, val int64) (int64, error)

func (RedisPool) LIndex

func (p RedisPool) LIndex(key string, index int64) (string, error)

func (RedisPool) LPop

func (p RedisPool) LPop(key string) (string, error)

func (RedisPool) LPush

func (p RedisPool) LPush(key string, val string) (int64, error)

func (RedisPool) MGet

func (p RedisPool) MGet(keys []string) (ret []interface{}, errRet error)

func (RedisPool) RPush

func (p RedisPool) RPush(key, val string) (int64, error)

func (RedisPool) SAdd

func (p RedisPool) SAdd(key string, vals []string) error

func (RedisPool) SPop

func (p RedisPool) SPop(key string) (string, error)

func (RedisPool) Set

func (p RedisPool) Set(key, value string) (err error)

func (RedisPool) SetEx

func (p RedisPool) SetEx(key string, expire int64, value string) (err error)

func (RedisPool) SetNX

func (p RedisPool) SetNX(key, value string, expire int) (interface{}, error)

func (RedisPool) ZAdd

func (p RedisPool) ZAdd(key string, score int64, val string) error

func (RedisPool) ZRange

func (p RedisPool) ZRange(key string, start int64, end int64) ([]string, error)

func (RedisPool) ZRemByScore

func (p RedisPool) ZRemByScore(key string, start string, end string) error

type ZSetResult

type ZSetResult struct {
	Member string
	Score  float64
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL