Documentation
¶
Index ¶
- Constants
- Variables
- func MustString(v interface{}, defaultValue string) string
- type BatchReply
- type BatchReq
- type FixedGoroutinePool
- type FixedGoroutinePoolTimeout
- type GoRoutinePoolWithConfig
- type RedisCluster
- func (r *RedisCluster) Close() error
- func (r *RedisCluster) Del(key string) (int64, error)
- func (r *RedisCluster) Eval(script string, keys []string, args []interface{}) (interface{}, error)
- func (r *RedisCluster) EvalSha(scriptsha string, keys []string, args []interface{}) (interface{}, error)
- func (r *RedisCluster) Exist(key string) (bool, error)
- func (r *RedisCluster) Expire(key string, expiration time.Duration) (bool, error)
- func (r *RedisCluster) Get(key string) (string, error)
- func (r *RedisCluster) HGet(key string, field string) (string, error)
- func (r *RedisCluster) HGetAll(key string) (map[string]string, error)
- func (r *RedisCluster) HIncrBy(key string, field string, value int64) (int64, error)
- func (r *RedisCluster) HLen(key string) (int64, error)
- func (r *RedisCluster) HMDel(key string, fields []string) (int64, error)
- func (r *RedisCluster) HMGet(key string, fields []string) ([]interface{}, error)
- func (r *RedisCluster) HMSet(key string, fields map[string]string) (bool, error)
- func (r *RedisCluster) HScan(key string, count int64) (map[string]string, error)
- func (r *RedisCluster) HSet(key string, field string, value string) (bool, error)
- func (r *RedisCluster) HSetNx(key string, field string, value string) (bool, error)
- func (r *RedisCluster) IncrBy(key string, value int64) (int64, error)
- func (r *RedisCluster) LIndex(key string, index int64) (string, error)
- func (r *RedisCluster) LPop(key string) (string, error)
- func (r *RedisCluster) LPush(key string, value string) (int64, error)
- func (r *RedisCluster) MDel(keys []string) (map[string]bool, error)
- func (r *RedisCluster) MDel2(keys []string) (map[string]bool, error)
- func (r *RedisCluster) MGet(keys []string) ([]interface{}, error)
- func (r *RedisCluster) MGet2(keys []string) ([]interface{}, error)
- func (r *RedisCluster) MSet(kvs map[string]string, expire time.Duration) (map[string]bool, error)
- func (r *RedisCluster) MSet2(kvs map[string]string, expire time.Duration) (map[string]bool, error)
- func (r *RedisCluster) PExpire(key string, expiration time.Duration) (bool, error)
- func (r *RedisCluster) PTtl(key string) (time.Duration, error)
- func (r *RedisCluster) RPush(key string, value string) (int64, error)
- func (r *RedisCluster) SAdd(key string, members []interface{}) (int64, error)
- func (r *RedisCluster) SPop(key string) (string, error)
- func (r *RedisCluster) Set(key string, value string, expire time.Duration) (string, error)
- func (r *RedisCluster) SetBit(key string, offset int64, value int) (int64, error)
- func (r *RedisCluster) SetNX(key string, value string, expire time.Duration) (bool, error)
- func (r *RedisCluster) ZAdd(key string, members []redis.Z) (int64, error)
- func (r *RedisCluster) ZRange(key string, start, stop int64) ([]string, error)
- func (r *RedisCluster) ZRangeByScoreWithScores(key string, min, max string, offset, limit int64) ([]*ZSetResult, error)
- func (r *RedisCluster) ZRem(key string, members ...interface{}) (int64, error)
- func (r *RedisCluster) ZRemRangeByRank(key string, start, stop int64) (int64, error)
- func (r *RedisCluster) ZRemRangeByScore(key string, min, max string) (int64, error)
- func (r *RedisCluster) ZRevRange(key string, start, stop int64) ([]string, error)
- func (r *RedisCluster) ZRevRangeByScoreWithScores(key string, min, max string, offset, limit int64) ([]*ZSetResult, error)
- type RedisClusterConf
- type RedisClusterCustomError
- type RedisConfig
- type RedisPool
- func NewPool(servers []string, password string, maxActive, maxIdle, idleTimeout int) *RedisPool
- func NewPoolRetry(servers []string, password string, maxActive, maxIdle, idleTimeout int, ...) *RedisPool
- func NewPoolRetryTimeout(servers []string, password string, maxActive, maxIdle, idleTimeout int, ...) *RedisPool
- func NewRedisPools(cfg *RedisConfig) (*RedisPool, error)
- func (p *RedisPool) ActiveCount() int
- func (p *RedisPool) BatchDo(commandname string, req []*BatchReq) (reply []*BatchReply)
- func (p *RedisPool) Close()
- func (p RedisPool) Del(key string) (err error)
- func (p *RedisPool) Do(commandName string, args ...interface{}) (reply interface{}, err error)
- func (p RedisPool) Exists(key string) (bool, error)
- func (p RedisPool) Expire(key string, time int) (int, error)
- func (p RedisPool) Get(key string) (ret string, errRet error)
- func (p RedisPool) HDel(key, field string) (err error)
- func (p RedisPool) HGet(key string, field string) (string, error)
- func (p RedisPool) HGetAll(key string) (ret map[string]string, err error)
- func (p RedisPool) HIncrBy(key, field string, val int64) (int64, error)
- func (p RedisPool) HLen(key string) (int64, error)
- func (p RedisPool) HMDel(key string, values []string) (int64, error)
- func (p RedisPool) HMGet(key string, values []string) ([]string, error)
- func (p RedisPool) HScan(key string, count int64) (ret map[string]string, err error)
- func (p RedisPool) HSet(key string, field string, value string) (err error)
- func (p RedisPool) Incr(key string) (int, error)
- func (p RedisPool) IncrBy(key string, val int64) (int64, error)
- func (p RedisPool) LIndex(key string, index int64) (string, error)
- func (p RedisPool) LPop(key string) (string, error)
- func (p RedisPool) LPush(key string, val string) (int64, error)
- func (p RedisPool) MGet(keys []string) (ret []interface{}, errRet error)
- func (p RedisPool) RPush(key, val string) (int64, error)
- func (p RedisPool) SAdd(key string, vals []string) error
- func (p RedisPool) SPop(key string) (string, error)
- func (p RedisPool) Set(key, value string) (err error)
- func (p RedisPool) SetEx(key string, expire int64, value string) (err error)
- func (p RedisPool) SetNX(key, value string, expire int) (interface{}, error)
- func (p RedisPool) ZAdd(key string, score int64, val string) error
- func (p RedisPool) ZRange(key string, start int64, end int64) ([]string, error)
- func (p RedisPool) ZRemByScore(key string, start string, end string) error
- type ZSetResult
Constants ¶
const ( DefaultDatabases = 0 DefaultTimeout = 15 * time.Second DefaultMaxIdle = 1 DefaultMaxActive = 0 )
const MAX_GOROUTINE_POOL_SIZE = 5
Variables ¶
var ErrNil = errors.New("redis: nil returned")
var TIMTOUR_ERR = fmt.Errorf("insert into gouroutine pool timeout")
Functions ¶
func MustString ¶
Types ¶
type BatchReply ¶
type FixedGoroutinePool ¶
type FixedGoroutinePool struct {
Size int64
// contains filtered or unexported fields
}
func (*FixedGoroutinePool) Close ¶
func (f *FixedGoroutinePool) Close()
func (*FixedGoroutinePool) Execute ¶
func (f *FixedGoroutinePool) Execute(function func())
func (*FixedGoroutinePool) ExecuteWithArg ¶
func (f *FixedGoroutinePool) ExecuteWithArg(function func(args ...interface{}), arg ...interface{})
func (*FixedGoroutinePool) GetGoroutineCount ¶
func (f *FixedGoroutinePool) GetGoroutineCount() int64
func (*FixedGoroutinePool) Start ¶
func (f *FixedGoroutinePool) Start() error
type FixedGoroutinePoolTimeout ¶
type FixedGoroutinePoolTimeout struct {
Size int64
Timeout time.Duration
// contains filtered or unexported fields
}
func (*FixedGoroutinePoolTimeout) Close ¶
func (f *FixedGoroutinePoolTimeout) Close()
func (*FixedGoroutinePoolTimeout) Execute ¶
func (f *FixedGoroutinePoolTimeout) Execute(function func()) error
func (*FixedGoroutinePoolTimeout) GetGoroutineCount ¶
func (f *FixedGoroutinePoolTimeout) GetGoroutineCount() int64
func (*FixedGoroutinePoolTimeout) Start ¶
func (f *FixedGoroutinePoolTimeout) Start() error
type GoRoutinePoolWithConfig ¶
type GoRoutinePoolWithConfig struct {
DefaultSize int64
ReservedConfig map[string]int64
// contains filtered or unexported fields
}
func (*GoRoutinePoolWithConfig) Close ¶
func (g *GoRoutinePoolWithConfig) Close()
func (*GoRoutinePoolWithConfig) ExecuteDefault ¶
func (g *GoRoutinePoolWithConfig) ExecuteDefault(function func())
func (*GoRoutinePoolWithConfig) ExecuteKey ¶
func (g *GoRoutinePoolWithConfig) ExecuteKey(key string, function func())
func (*GoRoutinePoolWithConfig) GetGoroutineCount ¶
func (g *GoRoutinePoolWithConfig) GetGoroutineCount(key string) int64
func (*GoRoutinePoolWithConfig) Start ¶
func (g *GoRoutinePoolWithConfig) Start() error
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) 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) HMDel ¶
func (r *RedisCluster) HMDel(key string, fields []string) (int64, error)
func (*RedisCluster) HMGet ¶
func (r *RedisCluster) HMGet(key string, fields []string) ([]interface{}, error)
*
- 异常, 返回 (nil, error)
- 正常, 返回 ([]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) HScan ¶
* 1. 若异常, 返回 (nil, error) 2. 若正常, 返回 (map[string]string, nil)
Notice: 这个函数并不能严格的限制返回count个,简单测试看起来主要和redis用的存储结构有关。
Hmap redis在存储的时候如果数据比较少(看文章是512)会使用ziplist,测试了下,在ziplist存储的状态下,会都返回,count不生效 如果数据超过512之后会使用hmap来存储,这时基本就是准确的了。所以这个函数只能保证返回 >= count
func (*RedisCluster) HSet ¶
* 1. 若异常, 返回 (false, error) 2. 若正常, 返回 (bool, nil), 其中true: field在hash中不存在且新增成功; false: field已在hash中存在且更新成功.
func (*RedisCluster) HSetNx ¶
* 1. 若异常, 返回 (false, error) 2. 若正常, 返回 (bool, nil), 其中true: field在hash中不存在且新增成功; false: field已在hash中存在, 不做更新.
func (*RedisCluster) LIndex ¶
func (r *RedisCluster) LIndex(key string, index int64) (string, 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)
*
- 若有异常, 返回 (nil,error)
- 若正常, 返回 ([]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)
*
- 若有异常, 返回 (nil,error)
- 若正常, 返回 ([]interface{}{...},nil), 其中结果里的value顺序与keys的顺序一一对应, 若某个key不存在, 所对应value为nil 例如 MGet([]string{"key1","key2","key3"}), 假设key2不存在, 返回数据如下: []interface{}{ "value1", //key1的值 nil, //不存在 "value3", //key3的值 }, nil
func (*RedisCluster) MSet2 ¶
* 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) PTtl ¶
func (r *RedisCluster) PTtl(key string) (time.Duration, error)
*
- 若正常, 返回 (true,nil)
- 若异常, 返回 (false,error) key不存在返回(false,nil)
func (*RedisCluster) SAdd ¶
func (r *RedisCluster) SAdd(key string, members []interface{}) (int64, error)
func (*RedisCluster) SetNX ¶
* 1. 若expire为0, 表示不设置过期 2. 如果 err 不为空, 则发生异常; 3. 在 err 为空的情况下, bool=false 表示key已存在set无效, bool=true表示key不存在set成功 https://redis.io/commands/setnx
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 {
//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 NewPoolRetry ¶
func NewPoolRetryTimeout ¶
func NewRedisPools ¶
func NewRedisPools(cfg *RedisConfig) (*RedisPool, error)
func (*RedisPool) ActiveCount ¶
func (*RedisPool) BatchDo ¶
func (p *RedisPool) BatchDo(commandname string, req []*BatchReq) (reply []*BatchReply)