Documentation
¶
Overview ¶
Package cache 提供缓冲相关的服务
Index ¶
- Constants
- func CheckNilErr(err error) (bool, error)
- func MsgPackDecodeBytes(bytes []byte, dest interface{}) (err error)
- func MsgPackEncodeBytes(data interface{}) (bytes []byte, err error)
- func TryLock(lockKey string, lockSencods int, paramConf *ParamConf, ...) (bool, error)
- func UnLock(lockKey string, paramConf *ParamConf, redisClient *RedisClient) error
- type Param
- type ParamConf
- func (p *ParamConf) Expire() int
- func (p *ParamConf) Group() string
- func (p *ParamConf) KeyPrefix() string
- func (p *ParamConf) NewParamKey(key string) *ParamKey
- func (p *ParamConf) NewParamKeyWithoutPrefix(key string) *ParamKey
- func (p *ParamConf) NewWithExpire(expire int) *ParamConf
- func (p *ParamConf) NewWithKeyPrefix(keyPrefix string) *ParamConf
- type ParamKey
- type Pipeline
- type PipelineReply
- type RedisClient
- func (p *RedisClient) Del(param Param) (deleted bool, err error)
- func (p *RedisClient) Do(param Param, fn func(conn redis.Conn) (interface{}, error)) (reply interface{}, err error)
- func (p *RedisClient) Eval(param Param, script *redis.Script, args ...interface{}) (reply interface{}, err error)
- func (p *RedisClient) Exists(param Param) (exists bool, err error)
- func (p *RedisClient) Expire(param Param) (expired bool, err error)
- func (p *RedisClient) Get(param Param) (reply interface{}, ok bool, err error)
- func (p *RedisClient) GetConn(param Param) (conn redis.Conn, err error)
- func (p *RedisClient) GetFloat64(param Param) (reply float64, ok bool, err error)
- func (p *RedisClient) GetGroupServers(group string) ([]*RedisServer, error)
- func (p *RedisClient) GetInt(param Param) (reply int, ok bool, err error)
- func (p *RedisClient) GetInt64(param Param) (reply int64, ok bool, err error)
- func (p *RedisClient) GetObject(param Param, dest interface{}) (ok bool, err error)
- func (p *RedisClient) GetObjects(paramConf *ParamConf, keys []string, dest interface{}, ...) error
- func (p *RedisClient) GetString(param Param) (reply string, ok bool, err error)
- func (p *RedisClient) Incr(param Param) (val int64, err error)
- func (p *RedisClient) IncrBy(param Param, increment int64) (val int64, err error)
- func (p *RedisClient) Set(param Param, data interface{}) error
- func (p *RedisClient) SetObject(param Param, data interface{}) error
- func (p *RedisClient) TTL(param Param) (val int, err error)
- type RedisConf
- type RedisConfigurer
- type RedisPoolConf
- type RedisServer
Constants ¶
const ( ReplyOK = "OK" DEL = "DEL" EXISTS = "EXISTS" EXPIRE = "EXPIRE" GET = "GET" HDEL = "HDEL" HGETALL = "HGETALL" HINCRBY = "HINCRBY" INCR = "INCR" INCRBY = "INCRBY" SET = "SET" SETEX = "SETEX" ZADD = "ZADD" ZCARD = "ZCARD" ZRANGE = "ZRANGE" ZREM = "ZREM" )
redis的命令及响应
const ( DefaultConnectTimout = 5 * 1000 DefaultReadTimeout = 5 * 1000 DefaultWriteTimeout = 5 * 1000 DefaultMaxActive = 100 DefaultMaxIdle = 2 DefaultIdleTimeout = 60 * 1000 )
Redis连接池的默认参数
Variables ¶
This section is empty.
Functions ¶
func CheckNilErr ¶
CheckNilErr check the err:
if err == nil, return true,nil; if the err is ErrNIl return false,nil;,otherwise return false ,err
func MsgPackDecodeBytes ¶
MsgPackDecodeBytes decode bytes to dest use msgpack
func MsgPackEncodeBytes ¶
MsgPackEncodeBytes encode data to bytes use msgpack
Types ¶
type Param ¶
type Param interface { //Group cache group id Group() string //Key cache key Key() string //Expire second time Expire() int }
Param is the cache param
type ParamConf ¶
type ParamConf struct {
// contains filtered or unexported fields
}
ParamConf is the cache param conf with cache group,key prefix and expire
func NewParamConf ¶
NewParamConf create ParamConf
func (*ParamConf) NewParamKey ¶
NewParamKey create new ParamKey with key
func (*ParamConf) NewParamKeyWithoutPrefix ¶
NewParamKeyWithoutPrefix create new ParamKey without p.keyPrefix
func (*ParamConf) NewWithExpire ¶
NewWithExpire create new ParamConf with new expire parameter
func (*ParamConf) NewWithKeyPrefix ¶
NewWithKeyPrefix append keyPrefix to exist ParamConf,return new ParamConf
type ParamKey ¶
type ParamKey struct { *ParamConf // contains filtered or unexported fields }
ParamKey is the cache param with key
func (*ParamKey) NewWithExpire ¶
NewWithExpire new key with expire
type Pipeline ¶
type Pipeline struct {
// contains filtered or unexported fields
}
Pipeline the command and results
func NewPipeline ¶
func NewPipeline(r *RedisClient) (*Pipeline, error)
NewPipeline new pipeline from RedisClient
func (*Pipeline) Receive ¶
func (p *Pipeline) Receive() (replies []*PipelineReply, err error)
Receive all reply from reids server
type PipelineReply ¶
type PipelineReply struct { Reply interface{} Err error }
PipelineReply reply from pipeline
type RedisClient ¶
type RedisClient struct {
// contains filtered or unexported fields
}
RedisClient redis client
func NewRedisClient ¶
func NewRedisClient(groups map[string][]*RedisServer) *RedisClient
NewRedisClient create new Redis
func NewRedisClientWithConf ¶
func NewRedisClientWithConf(conf *RedisConf) *RedisClient
NewRedisClientWithConf create redis from conf
func (*RedisClient) Del ¶
func (p *RedisClient) Del(param Param) (deleted bool, err error)
Del del the param.Key()
func (*RedisClient) Do ¶
func (p *RedisClient) Do(param Param, fn func(conn redis.Conn) (interface{}, error)) (reply interface{}, err error)
Do exec redis commands with param and key
func (*RedisClient) Eval ¶
func (p *RedisClient) Eval(param Param, script *redis.Script, args ...interface{}) (reply interface{}, err error)
Eval lua script for param.Key() with args
func (*RedisClient) Exists ¶
func (p *RedisClient) Exists(param Param) (exists bool, err error)
Exists check the param.Key() exist
func (*RedisClient) Expire ¶
func (p *RedisClient) Expire(param Param) (expired bool, err error)
Expire set timeout on key `param.Key()`,the timeout is `param.Expire()` second
func (*RedisClient) Get ¶
func (p *RedisClient) Get(param Param) (reply interface{}, ok bool, err error)
Get value from redis with param and key,if the param.Expire >0 then will EXPIRE the key
func (*RedisClient) GetConn ¶
func (p *RedisClient) GetConn(param Param) (conn redis.Conn, err error)
GetConn acquire redis.Conn in param.Group. If has mutiple servers in redis group,choose server with hash code which generated by fnv(key) % len(servers)
func (*RedisClient) GetFloat64 ¶
func (p *RedisClient) GetFloat64(param Param) (reply float64, ok bool, err error)
GetFloat64 get int64 value from redis with param
func (*RedisClient) GetGroupServers ¶
func (p *RedisClient) GetGroupServers(group string) ([]*RedisServer, error)
GetGroupServers query the servers for group
func (*RedisClient) GetInt ¶
func (p *RedisClient) GetInt(param Param) (reply int, ok bool, err error)
GetInt get int value from redis with param
func (*RedisClient) GetInt64 ¶
func (p *RedisClient) GetInt64(param Param) (reply int64, ok bool, err error)
GetInt64 get int64 value from redis with param
func (*RedisClient) GetObject ¶
func (p *RedisClient) GetObject(param Param, dest interface{}) (ok bool, err error)
GetObject get bytes whose key is param.Key(),then decode bytes to dest
func (*RedisClient) GetObjects ¶
func (p *RedisClient) GetObjects(paramConf *ParamConf, keys []string, dest interface{}, getByKey func(key string, index int) (interface{}, error)) error
GetObjects batch get struct object,use MsgPackDecodeBytes to decode bytes and append to dest
func (*RedisClient) GetString ¶
func (p *RedisClient) GetString(param Param) (reply string, ok bool, err error)
GetString get int64 value from redis with param
func (*RedisClient) Incr ¶
func (p *RedisClient) Incr(param Param) (val int64, err error)
Incr value from redis with param and key
func (*RedisClient) IncrBy ¶
func (p *RedisClient) IncrBy(param Param, increment int64) (val int64, err error)
IncrBy value from redis with param and key,if the param.Expire >0 then will EXPIRE the key
func (*RedisClient) Set ¶
func (p *RedisClient) Set(param Param, data interface{}) error
Set param.Key() with value `data`,if param.Exipre >0,then set key with expire second
func (*RedisClient) SetObject ¶
func (p *RedisClient) SetObject(param Param, data interface{}) error
SetObject set param.Key() with value `data`,if param.Exipre >0,then set key with expire second
type RedisConf ¶
type RedisConf struct { Servers []*RedisServer `yaml:"servers"` //实例列表 Groups map[string][]string `yaml:"groups"` //Redis组定义,key为组ID;value为Server的id列表 Pool *RedisPoolConf `yaml:"pool"` //默认的链接池配置 GroupPool map[string]*RedisPoolConf `yaml:"groups_pools"` //Redis组的连接池配置 // contains filtered or unexported fields }
RedisConf redis config
func (*RedisConf) RedisConfig ¶
RedisConfig implements RedisConfigurer
type RedisConfigurer ¶
type RedisConfigurer interface { c.Configurer RedisConfig() *RedisConf }
RedisConfigurer Redis配置器
type RedisPoolConf ¶
type RedisPoolConf struct { ConnectTimeout int `yaml:"connect_timeout"` //连接超时时间,单位毫秒秒 ReadTimeout int `yaml:"read_timeout"` //读取超时,单位毫秒 WriteTimeout int `yaml:"write_timeout"` //写取超时,单位毫秒 MaxIdle int `yaml:"max_idle"` //最大空闲连接 MaxActive int `yaml:"max_active"` //最大活跃连接,0表示不限制 IdleTimeout int `yaml:"idel_teimout"` //空闲连接的超时时间,单位毫秒 }
RedisPoolConf Redis连接池配置