Documentation ¶
Index ¶
- Variables
- type Redis
- func (r *Redis) Append(key, value string) error
- func (r *Redis) Auth(passwd string) error
- func (r *Redis) BGRewriteAOF() error
- func (r *Redis) BGSave() error
- func (r *Redis) BLPop(key string, keys ...interface{}) ([]string, error)
- func (r *Redis) BRPop(key string, keys ...interface{}) ([]string, error)
- func (r *Redis) BRPopLPush(src, dst string, timeout int) (string, error)
- func (r *Redis) BitCount(key string, args ...int) (int64, error)
- func (r *Redis) BitOp(op, dest, src string, srcs ...string) (int64, error)
- func (r *Redis) BitPos(key string, bit bool, args ...int) (int64, error)
- func (r *Redis) ClientGetName() (string, error)
- func (r *Redis) ClientKill(args ...interface{}) (interface{}, error)
- func (r *Redis) ClientList() ([]map[string]string, error)
- func (r *Redis) ClientPause(timeout int) error
- func (r *Redis) ClientReply(arg string) error
- func (r *Redis) ClientSetName(name string) error
- func (r *Redis) Close()
- func (r *Redis) CommandCount() (int64, error)
- func (r *Redis) CommandGetKeys(command string, commands ...interface{}) ([]string, error)
- func (r *Redis) ConfigGet(parameter string) ([]string, error)
- func (r *Redis) ConfigResetStat() error
- func (r *Redis) ConfigRewrite() error
- func (r *Redis) ConfigSet(parameter, value string) error
- func (r *Redis) DBSize() (int64, error)
- func (r *Redis) Decr(key string) (int64, error)
- func (r *Redis) DecrBy(key string, n int) (int64, error)
- func (r *Redis) Del(key string, keys ...string) (int64, error)
- func (r *Redis) Discard() error
- func (r *Redis) Do(cmd string, args ...interface{}) (reply interface{}, err error)
- func (r *Redis) Echo(msg string) (string, error)
- func (r *Redis) Exec() ([]interface{}, error)
- func (r *Redis) Exists(key string, keys ...string) (bool, error)
- func (r *Redis) Expire(key string, timeout int) (bool, error)
- func (r *Redis) ExpireAt(key string, timestamp int) (bool, error)
- func (r *Redis) FlushAll() error
- func (r *Redis) FlushDB() error
- func (r *Redis) GeoAdd(key string, longitude, latitude interface{}, member string, ...) (int64, error)
- func (r *Redis) GeoDist(key, member1, member2 string, unit ...string) (float64, error)
- func (r *Redis) GeoHash(key, member string, members ...string) ([]string, error)
- func (r *Redis) GeoPos(key, member string, members ...string) ([][]string, error)
- func (r *Redis) GeoRadius(key string, longitude, latitude, radius interface{}, unit string, ...) ([]interface{}, error)
- func (r *Redis) GeoRadiusByMember(key, member string, radius interface{}, unit string, others ...interface{}) ([]interface{}, error)
- func (r *Redis) Get(key string) (string, error)
- func (r *Redis) GetBit(key string, offset int) (int64, error)
- func (r *Redis) GetRange(key string, start, end int) (string, error)
- func (r *Redis) GetSet(key, value string) (string, error)
- func (r *Redis) HDel(key, field string, fields ...string) (int64, error)
- func (r *Redis) HExists(key, field string) (bool, error)
- func (r *Redis) HGet(key, field string) (string, error)
- func (r *Redis) HGetAll(key string) ([]string, error)
- func (r *Redis) HIncrBy(key, field string, n int64) (int64, error)
- func (r *Redis) HIncrByFloat(key, field string, n float64) (float64, error)
- func (r *Redis) HKeys(key string) ([]string, error)
- func (r *Redis) HLen(key string) (int64, error)
- func (r *Redis) HMGet(key, field string, fields ...string) ([]string, error)
- func (r *Redis) HMSet(key, field string, value interface{}, fields ...interface{}) error
- func (r *Redis) HSet(key, field string, value interface{}) (bool, error)
- func (r *Redis) HSetNX(key, field string, value interface{}) (bool, error)
- func (r *Redis) HStrLen(key, field string) (int64, error)
- func (r *Redis) HVals(key string) ([]string, error)
- func (r *Redis) Incr(key string) (int64, error)
- func (r *Redis) IncrBy(key string, n int) (int64, error)
- func (r *Redis) IncrByFloat(key string, n float64) (float64, error)
- func (r *Redis) Info(section ...string) (map[string]string, error)
- func (r *Redis) Keys(pattern string) ([]string, error)
- func (r *Redis) LIndex(key string, index int) (string, error)
- func (r *Redis) LInsert(key, ba, pivot, value string) (int64, error)
- func (r *Redis) LLen(key string) (int64, error)
- func (r *Redis) LPop(key string) (string, error)
- func (r *Redis) LPush(key string, value string, values ...string) (int64, error)
- func (r *Redis) LPushX(key, value string) (int64, error)
- func (r *Redis) LRange(key string, start, stop int) ([]string, error)
- func (r *Redis) LRem(key string, count int, value string) (int64, error)
- func (r *Redis) LSet(key string, index int, value string) error
- func (r *Redis) LTrim(key string, start, stop int) error
- func (r *Redis) LastSave() (int64, error)
- func (r *Redis) MGet(key string, keys ...string) ([]string, error)
- func (r *Redis) MSet(key string, value interface{}, kvs ...interface{}) error
- func (r *Redis) MSetNX(key string, value interface{}, kvs ...interface{}) (bool, error)
- func (r *Redis) Move(key string, db int) (bool, error)
- func (r *Redis) Multi() error
- func (r *Redis) PExpire(key string, timeout int) (bool, error)
- func (r *Redis) PExpireAt(key string, timestamp int) (bool, error)
- func (r *Redis) PFAdd(key, element string, elements ...string) (bool, error)
- func (r *Redis) PFCount(key string, keys ...string) (int64, error)
- func (r *Redis) PFMerge(dstKey, srcKey string, srcKeys ...string) error
- func (r *Redis) PSetEX(key string, timeout int, value string) error
- func (r *Redis) PTTL(key string) (int64, error)
- func (r *Redis) Persist(key string) (bool, error)
- func (r *Redis) Ping(msg ...string) (string, error)
- func (r *Redis) PubSub() (redis.PubSubConn, error)
- func (r *Redis) Publish(channel, message string) (int64, error)
- func (r *Redis) Quit() error
- func (r *Redis) RPop(key string) (string, error)
- func (r *Redis) RPopLPush(src, dst string) (string, error)
- func (r *Redis) RPush(key string, value string, values ...string) (int64, error)
- func (r *Redis) RPushX(key, value string) (int64, error)
- func (r *Redis) RandomKey() (string, error)
- func (r *Redis) Rename(oldKey, newKey string) error
- func (r *Redis) RenameNX(oldKey, newKey string) (bool, error)
- func (r *Redis) SAdd(key string, member string, members ...string) (int64, error)
- func (r *Redis) SCard(key string) (int64, error)
- func (r *Redis) SDiff(key string, keys ...string) ([]string, error)
- func (r *Redis) SDiffStore(dest, key string, keys ...string) (int64, error)
- func (r *Redis) SInter(key string, keys ...string) ([]string, error)
- func (r *Redis) SInterStore(dest, key string, keys ...string) (int64, error)
- func (r *Redis) SIsMember(key, member string) (bool, error)
- func (r *Redis) SMembers(key string) ([]string, error)
- func (r *Redis) SMove(src, dst, member string) (bool, error)
- func (r *Redis) SPop(key string, count ...int) ([]string, error)
- func (r *Redis) SRandMember(key string, count ...int) ([]string, error)
- func (r *Redis) SRem(key, member string, members ...string) (int64, error)
- func (r *Redis) SUnion(key string, keys ...string) ([]string, error)
- func (r *Redis) SUnionStore(dest, key string, keys ...string) (int64, error)
- func (r *Redis) Save() error
- func (r *Redis) Select(index int) error
- func (r *Redis) Set(key string, value interface{}, args ...interface{}) (err error)
- func (r *Redis) SetBit(key string, offset int, value bool) (int64, error)
- func (r *Redis) SetEX(key string, timeout int, value string) error
- func (r *Redis) SetNX(key string, value string) (bool, error)
- func (r *Redis) SetRange(key string, offset int, value string) (int64, error)
- func (r *Redis) Shutdown(save ...string) error
- func (r *Redis) SlaveOf(host string, port interface{}) error
- func (r *Redis) StrLen(key string) (int64, error)
- func (r *Redis) SwapDB(index1, index2 int) error
- func (r *Redis) TTL(key string) (int64, error)
- func (r *Redis) Time() (seconds, microseconds int64, err error)
- func (r *Redis) Type(key string) (string, error)
- func (r *Redis) Unwatch() error
- func (r *Redis) Watch(key string, keys ...string) error
- func (r *Redis) ZAdd(key string, values ...interface{}) (interface{}, error)
- func (r *Redis) ZCard(key string) (int64, error)
- func (r *Redis) ZCount(key string, min, max interface{}) (int64, error)
- func (r *Redis) ZIncrBy(key string, incr float64, member string) (float64, error)
- func (r *Redis) ZInterStore(dstKey string, num int, key string, others ...interface{}) (int64, error)
- func (r *Redis) ZLexCount(key string, min, max interface{}) (int64, error)
- func (r *Redis) ZRange(key string, start, stop int, WITHSCORES ...bool) ([]string, error)
- func (r *Redis) ZRangeByLex(key string, min, max interface{}, limit ...interface{}) ([]string, error)
- func (r *Redis) ZRangeByScore(key string, min, max interface{}, others ...interface{}) ([]string, error)
- func (r *Redis) ZRank(key, member string) (int64, error)
- func (r *Redis) ZRem(key, member string, members ...string) (int64, error)
- func (r *Redis) ZRemRangeByLex(key string, min, max interface{}) (int64, error)
- func (r *Redis) ZRemRangeByRank(key string, start, stop int) (int64, error)
- func (r *Redis) ZRemRangeByScore(key string, min, max interface{}) (int64, error)
- func (r *Redis) ZRevRange(key string, start, stop int, WITHSCORES ...bool) ([]string, error)
- func (r *Redis) ZRevRangeByLex(key string, min, max interface{}, limit ...interface{}) ([]string, error)
- func (r *Redis) ZRevRangeByScore(key string, min, max interface{}, others ...interface{}) ([]string, error)
- func (r *Redis) ZRevRank(key, member string) (int64, error)
- func (r *Redis) ZScore(key, member string) (float64, error)
- func (r *Redis) ZUnionStore(dstKey string, num int, key string, others ...interface{}) (int64, error)
Examples ¶
- Redis.Append
- Redis.Auth
- Redis.BLPop
- Redis.BRPop
- Redis.BRPopLPush
- Redis.BitCount
- Redis.BitOp
- Redis.BitPos
- Redis.ClientGetName
- Redis.ClientList
- Redis.ClientReply
- Redis.CommandCount
- Redis.CommandGetKeys
- Redis.ConfigGet
- Redis.ConfigResetStat
- Redis.ConfigRewrite
- Redis.DBSize
- Redis.Decr
- Redis.DecrBy
- Redis.Del
- Redis.Echo
- Redis.Exec
- Redis.Exists
- Redis.Expire
- Redis.ExpireAt
- Redis.GeoHash
- Redis.GeoPos
- Redis.GeoRadius
- Redis.GeoRadiusByMember
- Redis.GetBit
- Redis.GetRange
- Redis.GetSet
- Redis.HDel
- Redis.HExists
- Redis.HGet
- Redis.HGetAll
- Redis.HIncrBy
- Redis.HIncrByFloat
- Redis.HKeys
- Redis.HLen
- Redis.HMGet
- Redis.HMSet
- Redis.HSetNX
- Redis.HStrLen
- Redis.HVals
- Redis.Incr
- Redis.IncrBy
- Redis.IncrByFloat
- Redis.Info
- Redis.Keys
- Redis.LIndex
- Redis.LInsert
- Redis.LLen
- Redis.LPop
- Redis.LPushX
- Redis.LRange
- Redis.LRem
- Redis.LSet
- Redis.LTrim
- Redis.LastSave
- Redis.MGet
- Redis.MSet
- Redis.MSetNX
- Redis.PExpire
- Redis.PExpireAt
- Redis.PFAdd
- Redis.PFMerge
- Redis.PSetEX
- Redis.PTTL
- Redis.Persist
- Redis.Ping
- Redis.PubSub
- Redis.RPop
- Redis.RPopLPush
- Redis.RPushX
- Redis.Rename
- Redis.RenameNX
- Redis.SAdd
- Redis.SCard
- Redis.SDiff
- Redis.SDiffStore
- Redis.SInter
- Redis.SInterStore
- Redis.SIsMember
- Redis.SMove
- Redis.SPop
- Redis.SRandMember
- Redis.SRem
- Redis.SUnion
- Redis.SUnionStore
- Redis.Save
- Redis.Select
- Redis.Set
- Redis.SetEX
- Redis.SetNX
- Redis.SetRange
- Redis.SlaveOf
- Redis.StrLen
- Redis.TTL
- Redis.Time
- Redis.Type
- Redis.ZCard
- Redis.ZCount
- Redis.ZIncrBy
- Redis.ZInterStore
- Redis.ZLexCount
- Redis.ZRange
- Redis.ZRangeByLex
- Redis.ZRangeByScore
- Redis.ZRank
- Redis.ZRem
- Redis.ZRemRangeByLex
- Redis.ZRemRangeByRank
- Redis.ZRemRangeByScore
- Redis.ZRevRange
- Redis.ZRevRangeByLex
- Redis.ZRevRangeByScore
- Redis.ZRevRank
- Redis.ZScore
- Redis.ZUnionStore
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidArgs is returned when the arguments of the redis command is not right. ErrInvalidArgs = fmt.Errorf("The arguments is invalid.") // ErrInvalidResult is returned when the result is not right. ErrInvalidResult = fmt.Errorf("The result is invalid.") // ErrNotExist is returned when the key or result does not exist. ErrNotExist = fmt.Errorf("Not exist") )
var RedisConnTimeout = 5 * time.Second
RedisConnTimeout is the timeout to connect to the Redis Server.
Functions ¶
This section is empty.
Types ¶
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis is a proxy to operate the redis command.
func NewRedis ¶
NewRedis return a new Redis, which uses a connection pool in the underlying implementation.
connURL is a URL to connect to the redis server. It should follow the draft IANA specification for the scheme (https://www.iana.org/assignments/uri-schemes/prov/redis). For example, "redis://password@127.0.0.1:6379/0".
poolSize is the size of the connection pool.
func (*Redis) Append ¶
Append executes the redis command APPEND.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-append" r.Del(key) r.Set(key, key) r.Append(key, "1") v, _ := r.Get(key) fmt.Println(v)
Output: test-append1
func (*Redis) Auth ¶
Auth executes the redis command AUTH.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.Auth(""); err != nil { fmt.Println(err) }
Output: ERR Client sent AUTH, but no password is set
func (*Redis) BGRewriteAOF ¶
BGRewriteAOF executes the redis command BGREWRITEAOF.
New in redis version 1.0.0.
func (*Redis) BLPop ¶
BLPop executes the redis command BLPOP.
Notice: The argument keys has one element, which is the timeout, and the type of which must be int. If keys has more then one element, the timeout is the last.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-blpop" r.Del(key) r.RPush(key, "one", "two", "three") v, _ := r.BLPop(key, 0) fmt.Println(v)
Output: [test-blpop one]
func (*Redis) BRPop ¶
BRPop executes the redis command BRPOP.
Notice: The argument keys has one element, which is the timeout, and the type of which must be int. If keys has more then one element, the timeout is the last.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-brpop" r.Del(key) r.RPush(key, "one", "two", "three") v, _ := r.BRPop(key, 0) fmt.Println(v)
Output: [test-brpop three]
func (*Redis) BRPopLPush ¶
BRPopLPush executes the redis command BRPOPLPUSH.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-brpoplpush" key2 := "test-brpoplpush2" r.Del(key) r.Del(key2) r.RPush(key, "one", "two", "three") v, _ := r.BRPopLPush(key, key2, 0) fmt.Println(v) ss, _ := r.LRange(key, 0, -1) fmt.Println(ss) ss, _ = r.LRange(key2, 0, -1) fmt.Println(ss)
Output: three [one two] [three]
func (*Redis) BitCount ¶
BitCount executes the redis command BITCOUNT.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-bitcount" r.Del(key) r.Set(key, "foobar") v, _ := r.BitCount(key) fmt.Println(v) v, _ = r.BitCount(key, 0, 0) fmt.Println(v) v, _ = r.BitCount(key, 1, 1) fmt.Println(v)
Output: 26 4 6
func (*Redis) BitOp ¶
BitOp executes the redis command BITOP.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-bitop" key1 := "test-bitop1" key2 := "test-bitop2" r.Del(key) r.Del(key1) r.Del(key2) r.Set("key1", "foobar") r.Set("key2", "abcdef") v, _ := r.BitOp("AND", key, "key1", "key2") fmt.Println(v) vv, _ := r.Get(key) fmt.Println(vv)
Output: 6 `bc`ab
func (*Redis) BitPos ¶
BitPos executes the redis command BITPOS.
For the argument, bit, true is 1 and false is 0.
New in redis version 2.8.7.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-bitpos" r.Del(key) r.Set(key, "\xff\xf0\x00") v, _ := r.BitPos(key, false) fmt.Println(v) r.Set(key, "\x00\xff\xf0") v, _ = r.BitPos(key, true, 0) fmt.Println(v) v, _ = r.BitPos(key, true, 2) fmt.Println(v) r.Set(key, "\x00\x00\x00") v, _ = r.BitPos(key, true) fmt.Println(v)
Output: 12 8 16 -1
func (*Redis) ClientGetName ¶
ClientGetName executes the redis command CLIENT GETNAME.
New in redis version 2.6.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-clientgetname" r.ClientSetName(key) v, _ := r.ClientGetName() fmt.Println(v)
Output: test-clientgetname
func (*Redis) ClientKill ¶
ClientKill executes the redis command CLIENT KILL.
Return nil or a int64.
New in redis version 2.4.0.
func (*Redis) ClientList ¶
ClientList executes the redis command CLIENT LIST.
New in redis version 2.4.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() v, _ := r.ClientList() fmt.Println(len(v) != 0)
Output: true
func (*Redis) ClientPause ¶
ClientPause executes the redis command CLIENT PAUSE.
New in redis version 2.9.50.
func (*Redis) ClientReply ¶
ClientReply executes the redis command CLIENT REPLY.
New in redis version 3.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.ClientReply("ON"); err == nil { fmt.Println("OK") }
Output: OK
func (*Redis) ClientSetName ¶
ClientSetName executes the redis command CLIENT SETNAME.
New in redis version 2.6.9.
func (*Redis) CommandCount ¶
CommandCount executes the redis command COMMAND COUNT.
New in redis version 2.8.13.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if v, err := r.CommandCount(); err == nil && v != 0 { fmt.Println(v != 0) }
Output: true
func (*Redis) CommandGetKeys ¶
CommandGetKeys executes the redis command COMMAND GETKEYS.
New in redis version 2.8.13.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() v, _ := r.CommandGetKeys("MSET", "a", "b", "c", "d", "e", "f") fmt.Println(v) v, _ = r.CommandGetKeys("EVAL", "not consulted", 3, "key1", "key2", "key3", "arg1", "arg2", "arg3", "argN") fmt.Println(v) v, _ = r.CommandGetKeys("SORT", "mylist", "ALPHA", "STORE", "outlist") fmt.Println(v)
Output: [a c e] [key1 key2 key3] [mylist outlist]
func (*Redis) ConfigGet ¶
ConfigGet executes the redis command CONFIG GET.
Notice the difference of the returned value between the Linux Redis server and the Windows Redis server. For instance, r.ConfigGet("save") returns ["save", "jd 900 jd 300"] for Windows, and ["save", "900", "1", "300", "10"] for Linux.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() r.ConfigSet("save", "900 1 300 10") vs, _ := r.ConfigGet("save") if len(vs) == 2 { // For Windows Redis: ["save", "jd 900 jd 300"] if vs[0] == "save" && vs[1] == "jd 900 jd 300" { fmt.Println("OK") } else { fmt.Println("ERR") } } else { // For Linux Redis: ["save", "900", "1", "300", "10"] if vs[0] == "save" && vs[1] == "900" && vs[2] == "1" && vs[3] == "300" && vs[4] == "10" { fmt.Println("OK") } else { fmt.Println("ERR") } }
Output: OK
func (*Redis) ConfigResetStat ¶
ConfigResetStat executes the redis command CONFIG RESETSTAT.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.ConfigResetStat(); err == nil { fmt.Println("OK") }
Output: OK
func (*Redis) ConfigRewrite ¶
ConfigRewrite executes the redis command CONFIG REWRITE.
New in redis version 2.8.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.ConfigRewrite(); err == nil { fmt.Println("OK") }
Output: OK
func (*Redis) ConfigSet ¶
ConfigSet executes the redis command CONFIG SET.
New in redis version 2.0.0.
func (*Redis) DBSize ¶
DBSize executes the redis command DBSIZE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if _, err := r.DBSize(); err == nil { fmt.Println("OK") }
Output: OK
func (*Redis) Decr ¶
Decr executes the redis command DECR.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() defer func() { if err := recover(); err != nil { fmt.Println(err) } }() key := "test-decr" r.Del(key) r.Set(key, "10") v, _ := r.Decr(key) fmt.Println(v) r.Set(key, "234293482390480948029348230948") if _, err := r.Decr(key); err != nil { fmt.Println("ERROR") }
Output: 9 ERROR
func (*Redis) DecrBy ¶
DecrBy executes the redis command DECRBY.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-decrby" r.Del(key) r.Set(key, "10") v, _ := r.DecrBy(key, 5) fmt.Println(v)
Output: 5
func (*Redis) Del ¶
Del executes the redis command DEL.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-del1" key2 := "test-del2" r.Del(key1) r.Del(key2) r.Set(key1, key1) r.Set(key2, key2) v, _ := r.Del(key1, key2) fmt.Println(v)
Output: 2
func (*Redis) Echo ¶
Echo executes the redis command ECHO.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-echo" v, _ := r.Echo(key) fmt.Println(v)
Output: test-echo
func (*Redis) Exec ¶
Exec executes the redis command EXEC.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-exec" r.Del(key) r.Multi() r.Set(key, "123") r.Get(key) rs, _ := r.Exec() fmt.Println(rs[0]) fmt.Println(string(rs[1].([]byte)))
Output: OK 123
func (*Redis) Exists ¶
Exists executes the redis command EXISTS.
For the returned value, true is 1 and false is 0.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-exists" key2 := "test-nonexisting" r.Del(key1) r.Del(key2) r.Set(key1, key1) v, _ := r.Exists(key1, key2) fmt.Println(v)
Output: true
func (*Redis) Expire ¶
Expire executes the redis command EXPIRE.
For the returned value, true is 1 and false is 0.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-expire" r.Del(key) r.Set(key, key) v, _ := r.Expire(key, 1) fmt.Println(v) v, _ = r.Expire("nonexisting", 1) fmt.Println(v)
Output: true false
func (*Redis) ExpireAt ¶
ExpireAt executes the redis command EXPIREAT.
For the returned value, true is 1 and false is 0.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-expireat" r.Del(key) r.Set(key, key) v, _ := r.ExpireAt(key, 1293840000) fmt.Println(v) v, _ = r.ExpireAt("nonexisting", 1) fmt.Println(v)
Output: true false
func (*Redis) GeoAdd ¶
func (r *Redis) GeoAdd(key string, longitude, latitude interface{}, member string, others ...interface{}) (int64, error)
GeoAdd executes the redis command GEOADD.
New in redis version 3.2.0.
func (*Redis) GeoHash ¶
GeoHash executes the redis command GEOHASH.
New in redis version 3.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-geohash" r.Del(key) v, _ := r.GeoAdd(key, "13.361389", "38.115556", "Palermo", "15.087269", "37.502669", "Catania") fmt.Println(v) ss, _ := r.GeoHash(key, "Palermo", "Catania") fmt.Println(ss)
Output: 2 [sqc8b49rny0 sqdtr74hyu0]
func (*Redis) GeoPos ¶
GeoPos executes the redis command GEOPOS.
New in redis version 3.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-geopos" r.Del(key) v, _ := r.GeoAdd(key, "13.361389", "38.115556", "Palermo", "15.087269", "37.502669", "Catania") fmt.Println(v) vs, _ := r.GeoPos(key, "Palermo", "Catania", "NonExisting") fmt.Println(vs[0][0][:17]) fmt.Println(vs[0][1][:17]) fmt.Println(vs[1][0][:17]) fmt.Println(vs[1][1][:17])
Output: 2 13.36138933897018 38.11555639549629 15.08726745843887 37.50266842333162
func (*Redis) GeoRadius ¶
func (r *Redis) GeoRadius(key string, longitude, latitude, radius interface{}, unit string, others ...interface{}) ([]interface{}, error)
GeoRadius executes the redis command GEORADIUS.
The type of the returned value []string, or [][]string If WITHCOORD, WITHDIST or WITHHASH options are specified.
New in redis version 3.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-georadius" r.Del(key) v, _ := r.GeoAdd(key, "13.361389", "38.115556", "Palermo", "15.087269", "37.502669", "Catania") fmt.Println(v) f, _ := r.GeoDist(key, "Palermo", "Catania") fmt.Println(f) f, _ = r.GeoDist(key, "Palermo", "Catania", "km") fmt.Println(f) f, _ = r.GeoDist(key, "Palermo", "Catania", "mi") fmt.Println(f) f, _ = r.GeoDist(key, "foo", "bar") fmt.Println(f) vv, _ := r.GeoRadius(key, 15, 37, 100, "km") fmt.Println(vv) vv, _ = r.GeoRadius(key, 15, 37, 200, "km") fmt.Println(vv) vv, _ = r.GeoRadius(key, 15, 37, 200, "km", "WITHDIST") fmt.Println(vv)
Output: 2 166274.1516 166.2742 103.3182 0 [Catania] [Palermo Catania] [[Palermo 190.4424] [Catania 56.4413]]
func (*Redis) GeoRadiusByMember ¶
func (r *Redis) GeoRadiusByMember(key, member string, radius interface{}, unit string, others ...interface{}) ([]interface{}, error)
GeoRadiusByMember executes the redis command GEORADIUSBYMEMBER.
The type of the returned value []string, or [][]string If WITHCOORD, WITHDIST or WITHHASH options are specified.
New in redis version 3.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-georadiusbymember" r.Del(key) r.GeoAdd(key, "13.583333", "37.316667", "Agrigento") r.GeoAdd(key, "13.361389", "38.115556", "Palermo", "15.087269", "37.502669", "Catania") v, _ := r.GeoRadiusByMember(key, "Agrigento", 100, "km") fmt.Println(v)
Output: [Agrigento Palermo]
func (*Redis) Get ¶
Get executes the redis command GET.
Return "" if the key does not exist.
New in redis version 1.0.0.
func (*Redis) GetBit ¶
GetBit executes the redis command GETBIT.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-getbit" r.Del(key) v, _ := r.SetBit(key, 7, true) fmt.Println(v) v, _ = r.GetBit(key, 0) fmt.Println(v) v, _ = r.GetBit(key, 7) fmt.Println(v) v, _ = r.GetBit(key, 100) fmt.Println(v)
Output: 0 0 1 0
func (*Redis) GetRange ¶
GetRange executes the redis command GETRANGE.
New in redis version 2.4.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-getrange" r.Del(key) r.Set(key, "This is a string") v, _ := r.GetRange(key, 0, 3) fmt.Println(v) v, _ = r.GetRange(key, -3, -1) fmt.Println(v) v, _ = r.GetRange(key, 0, -1) fmt.Println(v) v, _ = r.GetRange(key, 10, 100) fmt.Println(v)
Output: This ing This is a string string
func (*Redis) GetSet ¶
GetSet executes the redis command GETSET.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-getset" r.Del(key) r.Set(key, "1") v, _ := r.GetSet(key, "2") fmt.Println(v) v, _ = r.Get(key) fmt.Println(v)
Output: 1 2
func (*Redis) HDel ¶
HDel executes the redis command HDEL.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hdel" r.Del(key) r.HSet(key, "field1", "foo") v, _ := r.HDel(key, "field1") fmt.Println(v) v, _ = r.HDel(key, "field2") fmt.Println(v)
Output: 1 0
func (*Redis) HExists ¶
HExists executes the redis command HEXISTS.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hexists" r.Del(key) r.HSet(key, "field1", "foo") v, _ := r.HExists(key, "field1") fmt.Println(v) v, _ = r.HExists(key, "field2") fmt.Println(v)
Output: true false
func (*Redis) HGet ¶
HGet executes the redis command HGET.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hget" r.Del(key) b, _ := r.HSet(key, "field1", "foo") fmt.Println(b) v, _ := r.HGet(key, "field1") fmt.Println(v) v, _ = r.HGet(key, "field2") fmt.Println(v)
Output: true foo
func (*Redis) HGetAll ¶
HGetAll executes the redis command HGETALL.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hgetall" r.Del(key) r.HSet(key, "field1", "foo") r.HSet(key, "field2", "bar") v, _ := r.HGetAll(key) fmt.Println(v)
Output: [field1 foo field2 bar]
func (*Redis) HIncrBy ¶
HIncrBy executes the redis command HINCRBY.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hinceby" r.Del(key) r.HSet(key, "field", 5) v, _ := r.HIncrBy(key, "field", 1) fmt.Println(v) v, _ = r.HIncrBy(key, "field", -1) fmt.Println(v) v, _ = r.HIncrBy(key, "field", -10) fmt.Println(v)
Output: 6 5 -5
func (*Redis) HIncrByFloat ¶
HIncrByFloat executes the redis command HINCRBYFLOAT.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hincebyfloat" r.Del(key) r.HSet(key, "field", 10.50) v, _ := r.HIncrByFloat(key, "field", 0.1) fmt.Println(v)
Output: 10.6
func (*Redis) HKeys ¶
HKeys executes the redis command HKEYS.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hkeys" r.Del(key) r.HSet(key, "field1", "Hello") r.HSet(key, "field2", "World") v, _ := r.HKeys(key) fmt.Println(v)
Output: [field1 field2]
func (*Redis) HLen ¶
HLen executes the redis command HLEN.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hlen" r.Del(key) r.HSet(key, "field1", "Hello") r.HSet(key, "field2", "World") v, _ := r.HLen(key) fmt.Println(v)
Output: 2
func (*Redis) HMGet ¶
HMGet executes the redis command HMGet.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hmget" r.Del(key) r.HSet(key, "field1", "Hello") r.HSet(key, "field2", "World") v, _ := r.HMGet(key, "field1", "field2", "nofield") fmt.Println(v)
Output: [Hello World ]
func (*Redis) HMSet ¶
HMSet executes the redis command HMSet.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hmset" r.Del(key) r.HMSet(key, "field1", "Hello", "field2", "World") v, _ := r.HGet(key, "field1") fmt.Println(v) v, _ = r.HGet(key, "field2") fmt.Println(v)
Output: Hello World
func (*Redis) HSetNX ¶
HSetNX executes the redis command HSETNX.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hsetnx" r.Del(key) b, _ := r.HSetNX(key, "field", "Hello") fmt.Println(b) b, _ = r.HSetNX(key, "field", "World") fmt.Println(b) v, _ := r.HGet(key, "field") fmt.Println(v)
Output: true false Hello
func (*Redis) HStrLen ¶
HStrLen executes the redis command HSTRLEN.
New in redis version 3.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hstrlen" r.Del(key) r.HMSet(key, "f1", "HelloWorld", "f2", 99, "f3", -256) v, _ := r.HStrLen(key, "f1") fmt.Println(v) v, _ = r.HStrLen(key, "f2") fmt.Println(v) v, _ = r.HStrLen(key, "f3") fmt.Println(v)
Output: 10 2 4
func (*Redis) HVals ¶
HVals executes the redis command HVALS.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-hvals" r.Del(key) r.HMSet(key, "f1", "Hello", "f2", "World") v, _ := r.HVals(key) fmt.Println(v)
Output: [Hello World]
func (*Redis) Incr ¶
Incr executes the redis command INCR.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-incr" r.Del(key) r.Set(key, "10") v, _ := r.Incr(key) fmt.Println(v) vv, _ := r.Get(key) fmt.Println(vv)
Output: 11 11
func (*Redis) IncrBy ¶
IncrBy executes the redis command INCRBY.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-incrby" r.Del(key) r.Set(key, "10") v, _ := r.IncrBy(key, 5) fmt.Println(v)
Output: 15
func (*Redis) IncrByFloat ¶
IncrByFloat executes the redis command INCRBYFloat.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-incrbyfloat" r.Del(key) r.Set(key, "10.50") v, _ := r.IncrByFloat(key, 0.1) fmt.Println(v)
Output: 10.6
func (*Redis) Info ¶
Info executes the redis command INFO.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() v, _ := r.Info() fmt.Println(len(v) != 0)
Output: true
func (*Redis) Keys ¶
Keys executes the redis command KEYS。
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-keys" r.Del(key) r.Set(key, key) keys, _ := r.Keys(key + "*") fmt.Printf("len=%d, key=%s\n", len(keys), keys[0]) v, _ := r.Keys("test-not-keys*") fmt.Printf("len=%d\n", len(v))
Output: len=1, key=test-keys len=0
func (*Redis) LIndex ¶
LIndex executes the redis command LINDEX.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-lindex" r.Del(key) r.RPush(key, "a", "b", "c") v, _ := r.LIndex(key, 1) fmt.Println(v) v, _ = r.LIndex(key, -1) fmt.Println(v) v, _ = r.LIndex(key, 4) fmt.Println(v)
Output: b c
func (*Redis) LInsert ¶
LInsert executes the redis command LINSERT.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-linsert" r.Del(key) r.RPush(key, "Hello", "World") v, _ := r.LInsert(key, "before", "World", "There") fmt.Println(v)
Output: 3
func (*Redis) LLen ¶
LLen executes the redis command LLEN.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-llen" r.Del(key) r.LPush(key, "Hello", "World") v, _ := r.LLen(key) fmt.Println(v)
Output: 2
func (*Redis) LPop ¶
LPop executes the redis command LPOP.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-lpop" r.Del(key) r.RPush(key, "a", "b", "c") v, _ := r.LPop(key) fmt.Println(v)
Output: a
func (*Redis) LPushX ¶
LPushX executes the redis command LPUSHX.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-lpushx" r.Del(key) r.LPush(key, "World") v, _ := r.LPushX(key, "Hello") fmt.Println(v) v, _ = r.LPushX("nonexisting", "Hello") fmt.Println(v)
Output: 2 0
func (*Redis) LRange ¶
LRange executes the redis command LRANGE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-lrange" r.Del(key) r.RPush(key, "one", "two", "three") v, _ := r.LRange(key, 0, 0) fmt.Println(v) v, _ = r.LRange(key, -3, 2) fmt.Println(v) v, _ = r.LRange(key, -100, 100) fmt.Println(v) v, _ = r.LRange(key, 5, 10) fmt.Println(v)
Output: [one] [one two three] [one two three] []
func (*Redis) LRem ¶
LRem executes the redis command LREM.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-lrem" r.Del(key) r.RPush(key, "hello", "hello", "foo", "hello") v, _ := r.LRem(key, -2, "hello") fmt.Println(v) ss, _ := r.LRange(key, 0, -1) fmt.Println(ss)
Output: 2 [hello foo]
func (*Redis) LSet ¶
LSet executes the redis command LSET.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-lset" r.Del(key) r.RPush(key, "one", "two", "three") r.LSet(key, 0, "four") r.LSet(key, -2, "five") v, _ := r.LRange(key, 0, -1) fmt.Println(v)
Output: [four five three]
func (*Redis) LTrim ¶
LTrim executes the redis command LTRIM.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-ltrim" r.Del(key) r.RPush(key, "one", "two", "three") r.LTrim(key, 1, -1) v, _ := r.LRange(key, 0, -1) fmt.Println(v)
Output: [two three]
func (*Redis) LastSave ¶
LastSave executes the redis command LASTSAVE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() v, _ := r.LastSave() fmt.Println(v != 0)
Output: true
func (*Redis) MGet ¶
MGet executes the redis command MGET.
If a certain key does not exist, this value is "".
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-mget1" key2 := "test-mget2" r.Del(key1) r.Del(key2) r.Set(key1, "Hello") r.Set(key2, "World") v, _ := r.MGet(key1, key2, "nonexisting") fmt.Println(v)
Output: [Hello World ]
func (*Redis) MSet ¶
MSet executes the redis command MSET.
New in redis version 1.0.1.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-mset1" key2 := "test-mset2" r.Del(key1) r.Del(key2) r.MSet(key1, "Hello", key2, "World") v, _ := r.Get(key1) fmt.Println(v) v, _ = r.Get(key2) fmt.Println(v)
Output: Hello World
func (*Redis) MSetNX ¶
MSetNX executes the redis command MSETNX.
For the returned value, true is 1 and false is 0.
New in redis version 1.0.1.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-msetnx1" key2 := "test-msetnx2" key3 := "test-msetnx3" r.Del(key1) r.Del(key2) r.Del(key3) v, _ := r.MSetNX(key1, "Hello", key2, "World") fmt.Println(v) v, _ = r.MSetNX(key2, "there", key3, "there") fmt.Println(v) s, _ := r.Get(key1) fmt.Println(s) s, _ = r.Get(key2) fmt.Println(s) s, _ = r.Get(key3) fmt.Println(s)
Output: true false Hello World
func (*Redis) Move ¶
Move executes the redis command MOVE.
For the returned value, true is 1 and false is 0.
New in redis version 1.0.0.
func (*Redis) PExpire ¶
PExpire executes the redis command PEXPIRE.
For the returned value, true is 1 and false is 0.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-pexpire" r.Del(key) r.Set(key, key) v, _ := r.PExpire(key, 1) fmt.Println(v) v, _ = r.PExpire("nonexisting", 1) fmt.Println(v)
Output: true false
func (*Redis) PExpireAt ¶
PExpireAt executes the redis command PEXPIREAT.
For the returned value, true is 1 and false is 0.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-pexpireat" r.Del(key) r.Set(key, key) v, _ := r.PExpireAt(key, 1293840000) fmt.Println(v) v, _ = r.PExpireAt("nonexisting", 1) fmt.Println(v)
Output: true false
func (*Redis) PFAdd ¶
PFAdd executes the redis command PFADD.
For the returned value, true is 1 and false is 0.
New in redis version 2.8.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-pfadd" r.Del(key) b, _ := r.PFAdd(key, "a", "b", "c", "d", "e", "f", "g") fmt.Println(b) v, _ := r.PFCount(key) fmt.Println(v)
Output: true 7
func (*Redis) PFMerge ¶
PFMerge executes the redis command PFMERGE.
New in redis version 2.8.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() dst := "test-pfmerge-dst" src1 := "test-pfmerge-src1" src2 := "test-pfmerge-src2" r.Del(dst) r.Del(src1) r.Del(src2) b, _ := r.PFAdd(src1, "foo", "bar", "zap", "a") fmt.Println(b) b, _ = r.PFAdd(src2, "a", "b", "c", "foo") fmt.Println(b) r.PFMerge(dst, src1, src2) v, _ := r.PFCount(dst) fmt.Println(v)
Output: true true 6
func (*Redis) PSetEX ¶
PSetEX executes the redis command PSETEX.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-psetex" r.Del(key) r.PSetEX(key, 1000, "Hello") v, _ := r.Get(key) fmt.Println(v) time.Sleep(1200 * time.Millisecond) v, _ = r.Get(key) fmt.Println(v)
Output: Hello
func (*Redis) PTTL ¶
PTTL executes the redis command PTTL.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-pttl" r.Del(key) r.SetEX(key, 1, key) v, _ := r.PTTL(key) fmt.Println(v > 990)
Output: true
func (*Redis) Persist ¶
Persist executes the redis command PERSIST.
For the returned value, true is 1 and false is 0.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-persist" r.Del(key) r.SetEX(key, 5, key) v, _ := r.Persist(key) fmt.Println(v) v, _ = r.Persist("nonexisting") fmt.Println(v)
Output: true false
func (*Redis) Ping ¶
Ping executes the redis command PING.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-ping" v, _ := r.Ping(key) fmt.Println(v) v, _ = r.Ping() fmt.Println(v)
Output: [test-ping] PONG
func (*Redis) PubSub ¶
func (r *Redis) PubSub() (redis.PubSubConn, error)
PubSub returns a new PubSubConn object, which use a new Redis connections.
This method is used to handle the subscribed datas.
Notice: The type of the returned value is PubSubConn in the pakcage of "github.com/garyburd/redigo/redis".
If you want to publish a message to a channel, please use r.Publish(c, m).
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() psc, _ := r.PubSub() var wg sync.WaitGroup wg.Add(2) // This goroutine receives and prints pushed notifications from the server. // The goroutine exits when the connection is unsubscribed from all // channels or there is an error. go func() { defer wg.Done() for { switch n := psc.Receive().(type) { case redis.Message: fmt.Printf("Message: %s %s\n", n.Channel, n.Data) case redis.PMessage: fmt.Printf("PMessage: %s %s %s\n", n.Pattern, n.Channel, n.Data) case redis.Subscription: fmt.Printf("Subscription: %s %s %d\n", n.Kind, n.Channel, n.Count) if n.Count == 0 { return } case error: fmt.Printf("error: %v\n", n) return } } }() // This goroutine manages subscriptions for the connection. go func() { defer wg.Done() psc.Subscribe("example") psc.PSubscribe("p*") r.Publish("example", "hello") r.Publish("example", "world") r.Publish("example", "foo") r.Publish("example", "bar") r.Publish("publish", "pattern") // Unsubscribe from all connections. This will cause the receiving // goroutine to exit. psc.Unsubscribe() psc.PUnsubscribe() }() wg.Wait()
Output: Subscription: subscribe example 1 Subscription: psubscribe p* 2 Message: example hello Message: example world Message: example foo Message: example bar PMessage: p* publish pattern Subscription: unsubscribe example 1 Subscription: punsubscribe p* 0
func (*Redis) Publish ¶
Publish executes the reids command PUBLISH, that's, publishs a message to a channel, then returns the number of the clients which receive this message.
New in redis version 2.0.0.
func (*Redis) RPop ¶
RPop executes the redis command RPOP.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-rpop" r.Del(key) r.RPush(key, "a", "b", "c") v, _ := r.RPop(key) fmt.Println(v)
Output: c
func (*Redis) RPopLPush ¶
RPopLPush executes the redis command RPOPLPUSH.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-rpoplpush" key2 := "test-rpoplpush2" r.Del(key) r.Del(key2) r.RPush(key, "one", "two", "three") v, _ := r.RPopLPush(key, key2) fmt.Println(v) ss, _ := r.LRange(key, 0, -1) fmt.Println(ss) ss, _ = r.LRange(key2, 0, -1) fmt.Println(ss)
Output: three [one two] [three]
func (*Redis) RPushX ¶
RPushX executes the redis command RPUSHX.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-rpushx" r.Del(key) r.RPush(key, "Hello") v, _ := r.RPushX(key, "World") fmt.Println(v) v, _ = r.RPushX("nonexisting", "World") fmt.Println(v)
Output: 2 0
func (*Redis) RandomKey ¶
RandomKey executes the redis command RANDOMKEY.
Return "" if no key exist.
New in redis version 1.0.0.
func (*Redis) Rename ¶
Rename executes the redis command RENAME.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-rename" newKey := key + "-new" r.Del(key) r.Del(newKey) r.Set(key, key) r.Rename(key, newKey) v, _ := r.Get(newKey) fmt.Println(v)
Output: test-rename
func (*Redis) RenameNX ¶
RenameNX executes the redis command RENAMENX.
For the returned value, true is 1 and false is 0.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-renamenx" newKey := key + "-new" r.Del(key) r.Del(newKey) r.Set(key, key) r.Set(newKey, newKey) b, _ := r.RenameNX(key, newKey) fmt.Println(b) v, _ := r.Get(newKey) fmt.Println(v)
Output: false test-renamenx-new
func (*Redis) SAdd ¶
SAdd executes the redis command SADD.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-sadd" r.Del(key) r.SAdd(key, "1", "2", "3") v, _ := r.SMembers(key) fmt.Println(v)
Output: [1 2 3]
func (*Redis) SCard ¶
SCard executes the redis command SCARD.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-scard" r.Del(key) r.SAdd(key, "1", "2", "3") v, _ := r.SCard(key) fmt.Println(v)
Output: 3
func (*Redis) SDiff ¶
SDiff executes the redis command SDIFF.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-sdiff1" key2 := "test-sdiff2" if _, err := r.Del(key1, key2); err != nil { fmt.Println(err) } r.SAdd(key1, "a") r.SAdd(key1, "b") r.SAdd(key1, "c") r.SAdd(key2, "c") r.SAdd(key2, "d") r.SAdd(key2, "e") v, _ := r.SDiff(key1, key2) fmt.Println(len(v))
Output: 2
func (*Redis) SDiffStore ¶
SDiffStore executes the redis command SDIFFSTORE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-sdiffstore1" key2 := "test-sdiffstore2" dest := "test-sdiffstore-dest" r.Del(key1, key2, dest) r.SAdd(key1, "a", "b", "c") r.SAdd(key2, "c", "d", "e") v, _ := r.SDiffStore(dest, key1, key2) fmt.Println(v) ss, _ := r.SMembers(dest) fmt.Println(len(ss) == 2)
Output: 2 true
func (*Redis) SInter ¶
SInter executes the redis command SINTER.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-sinter1" key2 := "test-sinter2" r.Del(key1) r.Del(key2) r.SAdd(key1, "a", "b", "c") r.SAdd(key2, "c", "d", "e") v, _ := r.SInter(key1, key2) fmt.Println(v)
Output: [c]
func (*Redis) SInterStore ¶
SInterStore executes the redis command SINTERSTORE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-sinterstore1" key2 := "test-sinterstore2" dest := "test-sinterstore-dest" r.Del(key1) r.Del(key2) r.Del(dest) r.SAdd(key1, "a", "b", "c") r.SAdd(key2, "c", "d", "e") v, _ := r.SInterStore(dest, key1, key2) fmt.Println(v) ss, _ := r.SMembers(dest) fmt.Println(ss)
Output: 1 [c]
func (*Redis) SIsMember ¶
SIsMember executes the redis command SISMEMBER.
For the returned value, ture is 1 and false is 0.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-sismember" r.Del(key) r.SAdd(key, "a", "b", "c") v, _ := r.SIsMember(key, "a") fmt.Println(v) v, _ = r.SIsMember(key, "z") fmt.Println(v)
Output: true false
func (*Redis) SMove ¶
SMove executes the redis command SMOVE.
For the returned value, ture is 1 and false is 0.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() src := "test-smove-src" dst := "test-smove-dest" r.Del(src) r.Del(dst) r.SAdd(src, "a", "b", "c") r.SAdd(dst, "d") b, _ := r.SMove(src, dst, "c") fmt.Println(b) v, _ := r.SMembers(src) fmt.Println(v) v, _ = r.SMembers(dst) fmt.Println(v)
Output: true [b a] [d c]
func (*Redis) SPop ¶
SPop executes the redis command SPOP.
Return nil if the key does not exist.
New in redis version 1.0.0. Changed: Adding count from 3.2.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-spop" r.Del(key) r.SAdd(key, "a", "b", "c") v, _ := r.SPop(key) fmt.Println(len(v)) v, _ = r.SPop(key, 3) fmt.Println(len(v))
Output: 1 2
func (*Redis) SRandMember ¶
SRandMember executes the redis command SRANDMEMBER.
Return nil if the key does not exist.
New in redis version 1.0.0. Changed: Adding count from 2.6.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-srandmember" r.Del(key) r.SAdd(key, "a", "b", "c") v, _ := r.SRandMember(key) fmt.Println(len(v)) v, _ = r.SRandMember(key, 2) fmt.Println(len(v)) v, _ = r.SRandMember(key, -5) fmt.Println(len(v))
Output: 1 2 5
func (*Redis) SRem ¶
SRem executes the redis command SREM.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-srem" r.Del(key) r.SAdd(key, "a", "b", "c", "d") v, _ := r.SRem(key, "a", "b") fmt.Println(v) v, _ = r.SRem(key, "z") fmt.Println(v) ss, _ := r.SMembers(key) fmt.Println(ss)
Output: 2 0 [d c]
func (*Redis) SUnion ¶
SUnion executes the redis command SUNION.
Return nil if the key does not exist.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-sunion1" key2 := "test-sunion2" r.Del(key1) r.Del(key2) r.SAdd(key1, "a", "b", "c") r.SAdd(key2, "c", "d", "e") v, _ := r.SUnion(key1, key2) fmt.Println(len(v) == 5)
Output: true
func (*Redis) SUnionStore ¶
SUnionStore executes the redis command SUNIONSTORE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key1 := "test-sunionstore1" key2 := "test-sunionstore2" dest := "test-sunionstore-dest" r.Del(key1, key2, dest) r.SAdd(key1, "a", "b", "c") r.SAdd(key2, "c", "d", "e") v, _ := r.SUnionStore(dest, key1, key2) fmt.Println(v) ss, _ := r.SMembers(dest) fmt.Println(len(ss) == 5)
Output: 5 true
func (*Redis) Save ¶
Save executes the redis command SAVE.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.Save(); err == nil { fmt.Println("OK") }
Output: OK
func (*Redis) Select ¶
Select executes the redis command SELECT.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.Select(1); err != nil { fmt.Println(err) } if err := r.Select(100); err != nil { fmt.Println(err) }
Output: ERR invalid DB index
func (*Redis) Set ¶
Set executes the redis command SET.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-set-get" r.Del(key) r.Set(key, key, "EX", 10) v, _ := r.Get(key) fmt.Println(v)
Output: test-set-get
func (*Redis) SetBit ¶
SetBit executes the redis command SETBIT.
For the argument, value, true is 1 and false is 0.
New in redis version 2.2.0.
func (*Redis) SetEX ¶
SetEX executes the redis command SETEX.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-setex" r.Del(key) r.SetEX(key, 1, "Hello") v, _ := r.Get(key) fmt.Println(v) time.Sleep(1200 * time.Millisecond) v, _ = r.Get(key) fmt.Println(v)
Output: Hello
func (*Redis) SetNX ¶
SetNX executes the redis command SETNX.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-setnx" r.Del(key) v, _ := r.SetNX(key, "Hello") fmt.Println(v) v, _ = r.SetNX(key, "World") fmt.Println(v) s, _ := r.Get(key) fmt.Println(s)
Output: true false Hello
func (*Redis) SetRange ¶
SetRange executes the redis command SETRANGE.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-setrange" r.Del(key) r.Set(key, "Hello World") v, _ := r.SetRange(key, 6, "Redis") fmt.Println(v) s, _ := r.Get(key) fmt.Println(s) key2 := "test-setrange2" r.Del(key2) v, _ = r.SetRange(key2, 6, "Redis") fmt.Println(v) s, _ = r.Get(key2) b := []byte(s) fmt.Println(b[:6]) fmt.Println(string(b[6:]))
Output: 11 Hello Redis 11 [0 0 0 0 0 0] Redis
func (*Redis) Shutdown ¶
Shutdown executes the redis command SHUTDOWN.
All the clients will be halted when executed this command, and the connection will be closed.
New in redis version 1.0.0.
func (*Redis) SlaveOf ¶
SlaveOf executes the redis command SLAVEOF.
If the parameters are either NO ONE or host port, which the port must be the type of int and between 1 and b5535.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() if err := r.SlaveOf("no", "one"); err == nil { fmt.Println("no one") } if err := r.SlaveOf("not no", "not one"); err != nil { fmt.Println("Argument Error") }
Output: no one Argument Error
func (*Redis) StrLen ¶
StrLen executes the redis command STRLEN.
New in redis version 2.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-strlen" r.Del(key) r.Set(key, "Hello World") v, _ := r.StrLen(key) fmt.Println(v) v, _ = r.StrLen("nonexisting") fmt.Println(v)
Output: 11 0
func (*Redis) TTL ¶
TTL executes the redis command TTL.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-ttl" r.Del(key) r.SetEX(key, 5, key) v, _ := r.TTL(key) fmt.Println(v)
Output: 5
func (*Redis) Time ¶
Time executes the redis command TIME.
New in redis version 2.6.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() s, m, _ := r.Time() fmt.Println(s != 0, m != 0)
Output: true true
func (*Redis) Type ¶
Type executes the redis command TYPE.
Return "" if the key does not exist.
New in redis version 1.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-type" r.Del(key) r.Set(key, key) v, _ := r.Type(key) fmt.Println(v)
Output: string
func (*Redis) ZAdd ¶
ZAdd executes the redis command ZADD.
Return a float64 if giving the option INCR, or a int64.
New in redis version 1.2.0. Adding from 3.0.2: XX, NX, CH, INCR.
func (*Redis) ZCard ¶
ZCard executes the redis command ZCARD.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zcard" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZCard(key) fmt.Println(v)
Output: 3
func (*Redis) ZCount ¶
ZCount executes the redis command ZCOUNT.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zcount" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZCount(key, "-inf", "+inf") fmt.Println(v) v, _ = r.ZCount(key, "(1", 3) fmt.Println(v)
Output: 3 2
func (*Redis) ZIncrBy ¶
ZIncrBy executes the redis command ZINCRBY.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zincrby" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZIncrBy(key, 3, "one") fmt.Println(v) ss, _ := r.ZRange(key, 0, -1, true) fmt.Println(ss)
Output: 4 [two 2 three 3 one 4]
func (*Redis) ZInterStore ¶
func (r *Redis) ZInterStore(dstKey string, num int, key string, others ...interface{}) (int64, error)
ZInterStore executes the redis command ZINTERSTORE.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zinterstore" key1 := "test-zinterstore1" key2 := "test-zinterstore2" r.Del(key) r.Del(key1) r.Del(key2) r.ZAdd(key1, 1, "one", 2, "two") r.ZAdd(key2, 1, "one", 2, "two", 3, "three") v, _ := r.ZInterStore(key, 2, key1, key2, "WEIGHTS", 2, 3) fmt.Println(v) ss, _ := r.ZRange(key, 0, -1, true) fmt.Println(ss)
Output: 2 [one 5 two 10]
func (*Redis) ZLexCount ¶
ZLexCount executes the redis command ZLEXCOUNT.
New in redis version 2.8.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zlexcount" r.Del(key) r.ZAdd(key, 0, "a", 0, "b", 0, "c", 0, "d", 0, "e") r.ZAdd(key, 0, "f", 0, "g") v, _ := r.ZLexCount(key, "-", "+") fmt.Println(v) v, _ = r.ZLexCount(key, "[b", "[f") fmt.Println(v)
Output: 7 5
func (*Redis) ZRange ¶
ZRange executes the redis command ZRANGE.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrange" r.Del(key) v, _ := r.ZAdd(key, 1, "one", 2, "two", 3, "three") fmt.Println(v) ss, _ := r.ZRange(key, 0, -1) fmt.Println(ss) ss, _ = r.ZRange(key, 0, 1, true) fmt.Println(ss)
Output: 3 [one two three] [one 1 two 2]
func (*Redis) ZRangeByLex ¶
func (r *Redis) ZRangeByLex(key string, min, max interface{}, limit ...interface{}) ([]string, error)
ZRangeByLex executes the redis command ZRANGEBYLEX.
New in redis version 2.8.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrangebylex" r.Del(key) r.ZAdd(key, 0, "a", 0, "b", 0, "c", 0, "d", 0, "e") r.ZAdd(key, 0, "f", 0, "g") v, _ := r.ZRangeByLex(key, "-", "(c") fmt.Println(v) v, _ = r.ZRangeByLex(key, "[aaa", "(g") fmt.Println(v)
Output: [a b] [b c d e f]
func (*Redis) ZRangeByScore ¶
func (r *Redis) ZRangeByScore(key string, min, max interface{}, others ...interface{}) ([]string, error)
ZRangeByScore executes the redis command ZRANGEBYSCORE.
New in redis version 1.0.5.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrangebyscore" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRangeByScore(key, "-inf", "+inf") fmt.Println(v) v, _ = r.ZRangeByScore(key, 0, 2) fmt.Println(v) v, _ = r.ZRangeByScore(key, "(1", 2) fmt.Println(v) v, _ = r.ZRangeByScore(key, "(1", "(2") fmt.Println(v)
Output: [one two three] [one two] [two] []
func (*Redis) ZRank ¶
ZRank executes the redis command ZRANK.
Return the rank which is 0-based of member if member exists in the sorted set.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrank" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRank(key, "three") fmt.Println(v) v, _ = r.ZRank(key, "four") fmt.Println(v)
Output: 2 0
func (*Redis) ZRem ¶
ZRem executes the redis command ZREM.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrem" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRem(key, "one", "two") fmt.Println(v) ss, _ := r.ZRange(key, 0, -1, true) fmt.Println(ss)
Output: 2 [three 3]
func (*Redis) ZRemRangeByLex ¶
ZRemRangeByLex executes the redis command ZREMRANGEBYLEX.
New in redis version 2.8.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zremrangebylex" r.Del(key) r.ZAdd(key, 0, "a", 0, "b", 0, "c", 0, "d", 0, "e") r.ZAdd(key, 0, "f", 0, "g") ss, _ := r.ZRange(key, 0, -1) fmt.Println(ss) v, _ := r.ZRemRangeByLex(key, "[b", "[d") fmt.Println(v) ss, _ = r.ZRange(key, 0, -1) fmt.Println(ss)
Output: [a b c d e f g] 3 [a e f g]
func (*Redis) ZRemRangeByRank ¶
ZRemRangeByRank executes the redis command ZREMRANGEBYRank.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zremrangebyrank" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRemRangeByRank(key, 0, 1) fmt.Println(v) ss, _ := r.ZRange(key, 0, -1, true) fmt.Println(ss)
Output: 2 [three 3]
func (*Redis) ZRemRangeByScore ¶
ZRemRangeByScore executes the redis command ZREMRANGEBYSCORE.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zremrangebyscore" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRemRangeByScore(key, "-inf", "(2") fmt.Println(v) ss, _ := r.ZRange(key, 0, -1, true) fmt.Println(ss)
Output: 1 [two 2 three 3]
func (*Redis) ZRevRange ¶
ZRevRange executes the redis command ZREVRANGE.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrevrange" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRevRange(key, 0, -1, true) fmt.Println(v) v, _ = r.ZRevRange(key, 2, 3) fmt.Println(v)
Output: [three 3 two 2 one 1] [one]
func (*Redis) ZRevRangeByLex ¶
func (r *Redis) ZRevRangeByLex(key string, min, max interface{}, limit ...interface{}) ([]string, error)
ZRevRangeByLex executes the redis command ZREVRANGEBYLEX.
New in redis version 2.8.9.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrevrangebylex" r.Del(key) r.ZAdd(key, 0, "a", 0, "b", 0, "c", 0, "d", 0, "e") r.ZAdd(key, 0, "f", 0, "g") v, _ := r.ZRevRangeByLex(key, "[c", "-") fmt.Println(v) v, _ = r.ZRevRangeByLex(key, "(c", "-") fmt.Println(v) v, _ = r.ZRevRangeByLex(key, "(g", "[aaa") fmt.Println(v)
Output: [c b a] [b a] [f e d c b]
func (*Redis) ZRevRangeByScore ¶
func (r *Redis) ZRevRangeByScore(key string, min, max interface{}, others ...interface{}) ([]string, error)
ZRevRangeByScore executes the redis command ZREVRANGEBYSCORE.
New in redis version 1.0.5.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrevrangebyscore" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRevRangeByScore(key, "+inf", "-inf") fmt.Println(v) v, _ = r.ZRevRangeByScore(key, 2, 1) fmt.Println(v) v, _ = r.ZRevRangeByScore(key, 2, "(1") fmt.Println(v) v, _ = r.ZRevRangeByScore(key, "(2", "(1") fmt.Println(v)
Output: [three two one] [two one] [two] []
func (*Redis) ZRevRank ¶
ZRevRank executes the redis command ZREVRANK.
Return the rank which is 0-based of member if member exists in the sorted set.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zrevrank" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZRevRank(key, "one") fmt.Println(v) v, _ = r.ZRevRank(key, "four") fmt.Println(v)
Output: 2 0
func (*Redis) ZScore ¶
ZScore executes the redis command ZSCORE.
New in redis version 1.2.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zscore" r.Del(key) r.ZAdd(key, 1, "one", 2, "two", 3, "three") v, _ := r.ZScore(key, "one") fmt.Println(v)
Output: 1
func (*Redis) ZUnionStore ¶
func (r *Redis) ZUnionStore(dstKey string, num int, key string, others ...interface{}) (int64, error)
ZUnionStore executes the redis command ZUNIONSTORE.
New in redis version 2.0.0.
Example ¶
r := NewRedis("redis://127.0.0.1:6379/0", 1) defer r.Close() key := "test-zunionstore" key1 := "test-zunionstore1" key2 := "test-zunionstore2" r.Del(key) r.Del(key1) r.Del(key2) r.ZAdd(key1, 1, "one", 2, "two") r.ZAdd(key2, 1, "one", 2, "two", 3, "three") v, _ := r.ZUnionStore(key, 2, key1, key2, "WEIGHTS", 2, 3) fmt.Println(v) ss, _ := r.ZRange(key, 0, -1, true) fmt.Println(ss)
Output: 3 [one 5 three 9 two 10]