redis

package module
v0.0.0-...-3ece3cf Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-redis

Supply the high-level API interface based on https://github.com/garyburd/redigo. It uses a pool to handle the redis connection in the underlying implementation, but you don't have to care about it, and it should be transparent to the user.

This package has no third-party dependencies, but https://github.com/garyburd/redigo and github.com/xgfone/go-tools/pools.

About the API

For the meaning of APIs, please see the corresponding redis command doc.

Name

The API name is the name of the redis command, but the API name is the format of CamelCase. For instance,

Redis Command API Name
SET Set
SETNX SetNX
SETRANGE SetRange
CONFIG GET ConfigGet
Arguments

The arguments of API is the same as the redis command. See the redis doc.

Return

The correspondences is as follows.

Redis Return API Return
String("OK") NO RETURN VALUE
Integer int64
Integer(0/1) bool
Float String float64
Bulk String string
Array []string
Maybe Two Types interface{}
Maybe Two Slice Types []interface{}

Notice:

  1. If the redis connection has an error, or the redis server returns an error, the API will return the error.
  2. All commands return an error to indicate whether the call is successful. If the error is nil, you can use the result value if existing.
  3. For the specail commands of INFO and CLIENT LIST, the corresponding APIs, Info and ClientList, will return the parsed Key-Values, that's, map[string]string.

Implemented Commands Table

Implemented Commands Unimplemented Commands
Connection AUTH, ECHO, PING, QUIT, SELECT, SWAPDB(required redis 4.0)
Geo GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUSBYMEMBER
Hashes HDEL, HEXISTS, HGET, HGETALL, HINCRBY, HINCRBYFLOAT, HKEYS, HLEN, HMGET, HMSET, HSET, HSETNX, HSTRLEN, HVALS HSCAN
HyperLogLog PFADD, PFCOUNT, PFMERGE
Keys DEL, DUMP, EXISTS, EXPIRE, EXPIREAT, KEYS, MOVE, PERSIST, PEXPIRE, PEXPIREAT, PTTL, RANDOMKEY, RENAME, RENAMENX, TTL, TYPE MIGRATE, OBJECT, RESTORE, SORT, WAIT, SCAN
Lists BLPOP, BRPOP, BRPOPLPUSH, LINDEX, LINSERT, LLEN, LPOP, LPUSH, LPUSHX, LRANGE, LREM, LSET, LTRIM, RPOP, RPOPLPUSH, RPUSH, RPUSHX
Pub/Sub PSUBSCRIBE, PUNSUBSCRIBE, SUBSCRIBE, UNSUBSCRIBE, PUBLISH, PUBSUB
Server BGREWRITEAOF, BGSAVE, CLIENT GETNAME, CLIENT SETNAME, CLIENT KILL, CLIENT LIST, CLIENT PAUSE, CLIENT REPLY, COMMAND COUNT, COMMAND GETKEYS, CONFIG GET, CONFIG SET, CONFIG RESETSTAT, CONFIG REWRITE, DBSIZE, FLUSHALL, FLUSHDB, INFO, LASTSAVE, SAVE, SHUTDOWN, SLAVEOF, TIME COMMAND, COMMAND INFO, DEBUG OBJECT, DEBUG SEGFAULT, MONITOR, ROLE, SLOWLOG, SYNC
Sets SADD, SCARD, SDIFF, SDIFFSTORE, SINTER, SINTERSTORE, SISMEMBER, SMEMBERS, SMOVE, SPOP, SRANDMEMBER, SREM, SUNION, SUNIONSTORE SSCAN
Sorted Sets ZADD, ZCARD, ZCOUNT, ZINCRBY, ZINTERSTORE, ZLEXCOUNT, ZRANGE, ZRANGEBYLEX, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYLEX, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYLEX, ZREVRANGEBYSCORE, ZREVRANK, ZSCORE, ZUNIONSTORE ZSCAN
String APPEND, BITCOUNT, BITOP, BITPOS, DECR, DECRBY, GET, GETBIT, GETRANGE, GETSET, INCR, INCRBY, INCRBYFLOAT, MGET, MSET, MSETNX, PSETEX, SET, SETBIT, SETEX, SETNX, SETRANGE, STRLEN BITFIELD
Transactions DISCARD, EXEC, MULTI, WATCH, UNWATCH
Cluster In future
Scripting In future
Notice

For the unimplemented commands, you can use the Do method,

func (r *Redis) Do(cmd string, args ...interface{}) (reply interface{}, err error)

to implement it.

Welcome to give me a pull request.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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

func NewRedis(connURL string, poolSize int) *Redis

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

func (r *Redis) Append(key, value string) error

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

func (r *Redis) Auth(passwd string) error

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

func (r *Redis) BGRewriteAOF() error

BGRewriteAOF executes the redis command BGREWRITEAOF.

New in redis version 1.0.0.

func (*Redis) BGSave

func (r *Redis) BGSave() error

BGSave executes the redis command BGSAVE.

New in redis version 1.0.0.

func (*Redis) BLPop

func (r *Redis) BLPop(key string, keys ...interface{}) ([]string, error)

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

func (r *Redis) BRPop(key string, keys ...interface{}) ([]string, error)

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

func (r *Redis) BRPopLPush(src, dst string, timeout int) (string, error)

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

func (r *Redis) BitCount(key string, args ...int) (int64, error)

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

func (r *Redis) BitOp(op, dest, src string, srcs ...string) (int64, error)

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

func (r *Redis) BitPos(key string, bit bool, args ...int) (int64, error)

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

func (r *Redis) ClientGetName() (string, error)

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

func (r *Redis) ClientKill(args ...interface{}) (interface{}, error)

ClientKill executes the redis command CLIENT KILL.

Return nil or a int64.

New in redis version 2.4.0.

func (*Redis) ClientList

func (r *Redis) ClientList() ([]map[string]string, error)

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

func (r *Redis) ClientPause(timeout int) error

ClientPause executes the redis command CLIENT PAUSE.

New in redis version 2.9.50.

func (*Redis) ClientReply

func (r *Redis) ClientReply(arg string) error

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

func (r *Redis) ClientSetName(name string) error

ClientSetName executes the redis command CLIENT SETNAME.

New in redis version 2.6.9.

func (*Redis) Close

func (r *Redis) Close()

Close closes the Redis connection.

func (*Redis) CommandCount

func (r *Redis) CommandCount() (int64, error)

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

func (r *Redis) CommandGetKeys(command string, commands ...interface{}) ([]string, error)

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

func (r *Redis) ConfigGet(parameter string) ([]string, error)

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

func (r *Redis) ConfigResetStat() error

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

func (r *Redis) ConfigRewrite() error

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

func (r *Redis) ConfigSet(parameter, value string) error

ConfigSet executes the redis command CONFIG SET.

New in redis version 2.0.0.

func (*Redis) DBSize

func (r *Redis) DBSize() (int64, error)

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

func (r *Redis) Decr(key string) (int64, error)

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

func (r *Redis) DecrBy(key string, n int) (int64, error)

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

func (r *Redis) Del(key string, keys ...string) (int64, error)

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) Discard

func (r *Redis) Discard() error

Discard executes the redis command DISCARD.

New in redis version 2.0.0.

func (*Redis) Do

func (r *Redis) Do(cmd string, args ...interface{}) (reply interface{}, err error)

Do executes the Redis command, then returns the result.

func (*Redis) Echo

func (r *Redis) Echo(msg string) (string, error)

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

func (r *Redis) Exec() ([]interface{}, error)

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

func (r *Redis) Exists(key string, keys ...string) (bool, error)

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

func (r *Redis) Expire(key string, timeout int) (bool, error)

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

func (r *Redis) ExpireAt(key string, timestamp int) (bool, error)

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) FlushAll

func (r *Redis) FlushAll() error

FlushAll executes the redis command FLUSHALL.

New in redis version 1.0.0.

func (*Redis) FlushDB

func (r *Redis) FlushDB() error

FlushDB executes the redis command FLUSHDB.

New in redis version 1.0.0.

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) GeoDist

func (r *Redis) GeoDist(key, member1, member2 string, unit ...string) (float64, error)

GeoDist executes the redis command GEODIST.

New in redis version 3.2.0.

func (*Redis) GeoHash

func (r *Redis) GeoHash(key, member string, members ...string) ([]string, error)

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

func (r *Redis) GeoPos(key, member string, members ...string) ([][]string, error)

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

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

Get executes the redis command GET.

Return "" if the key does not exist.

New in redis version 1.0.0.

func (*Redis) GetBit

func (r *Redis) GetBit(key string, offset int) (int64, error)

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

func (r *Redis) GetRange(key string, start, end int) (string, error)

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

func (r *Redis) GetSet(key, value string) (string, error)

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

func (r *Redis) HDel(key, field string, fields ...string) (int64, error)

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

func (r *Redis) HExists(key, field string) (bool, error)

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

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

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

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

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

func (r *Redis) HIncrBy(key, field string, n int64) (int64, error)

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

func (r *Redis) HIncrByFloat(key, field string, n float64) (float64, error)

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

func (r *Redis) HKeys(key string) ([]string, error)

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

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

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

func (r *Redis) HMGet(key, field string, fields ...string) ([]string, error)

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

func (r *Redis) HMSet(key, field string, value interface{}, fields ...interface{}) error

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) HSet

func (r *Redis) HSet(key, field string, value interface{}) (bool, error)

HSet executes the redis command HSET.

New in redis version 2.0.0.

func (*Redis) HSetNX

func (r *Redis) HSetNX(key, field string, value interface{}) (bool, error)

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

func (r *Redis) HStrLen(key, field string) (int64, error)

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

func (r *Redis) HVals(key string) ([]string, error)

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

func (r *Redis) Incr(key string) (int64, error)

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

func (r *Redis) IncrBy(key string, n int) (int64, error)

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

func (r *Redis) IncrByFloat(key string, n float64) (float64, error)

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

func (r *Redis) Info(section ...string) (map[string]string, error)

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

func (r *Redis) Keys(pattern string) ([]string, error)

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

func (r *Redis) LIndex(key string, index int) (string, error)

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

func (r *Redis) LInsert(key, ba, pivot, value string) (int64, error)

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

func (r *Redis) LLen(key string) (int64, error)

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

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

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) LPush

func (r *Redis) LPush(key string, value string, values ...string) (int64, error)

LPush executes the redis command LPUSH.

New in redis version 1.0.0.

func (*Redis) LPushX

func (r *Redis) LPushX(key, value string) (int64, error)

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

func (r *Redis) LRange(key string, start, stop int) ([]string, error)

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

func (r *Redis) LRem(key string, count int, value string) (int64, error)

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

func (r *Redis) LSet(key string, index int, value string) error

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

func (r *Redis) LTrim(key string, start, stop int) error

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

func (r *Redis) LastSave() (int64, error)

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

func (r *Redis) MGet(key string, keys ...string) ([]string, error)

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

func (r *Redis) MSet(key string, value interface{}, kvs ...interface{}) error

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

func (r *Redis) MSetNX(key string, value interface{}, kvs ...interface{}) (bool, error)

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

func (r *Redis) Move(key string, db int) (bool, error)

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) Multi

func (r *Redis) Multi() error

Multi executes the redis command MULTI.

New in redis version 1.2.0.

func (*Redis) PExpire

func (r *Redis) PExpire(key string, timeout int) (bool, error)

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

func (r *Redis) PExpireAt(key string, timestamp int) (bool, error)

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

func (r *Redis) PFAdd(key, element string, elements ...string) (bool, error)

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) PFCount

func (r *Redis) PFCount(key string, keys ...string) (int64, error)

PFCount executes the redis command PFCOUNT.

New in redis version 2.8.9.

func (*Redis) PFMerge

func (r *Redis) PFMerge(dstKey, srcKey string, srcKeys ...string) error

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

func (r *Redis) PSetEX(key string, timeout int, value string) error

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

func (r *Redis) PTTL(key string) (int64, error)

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

func (r *Redis) Persist(key string) (bool, error)

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

func (r *Redis) Ping(msg ...string) (string, error)

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

func (r *Redis) Publish(channel, message string) (int64, error)

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) Quit

func (r *Redis) Quit() error

Quit executes the redis command QUIT.

New in redis version 1.0.0.

func (*Redis) RPop

func (r *Redis) RPop(key string) (string, error)

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

func (r *Redis) RPopLPush(src, dst string) (string, error)

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) RPush

func (r *Redis) RPush(key string, value string, values ...string) (int64, error)

RPush executes the redis command RPUSH.

New in redis version 1.0.0.

func (*Redis) RPushX

func (r *Redis) RPushX(key, value string) (int64, error)

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

func (r *Redis) RandomKey() (string, error)

RandomKey executes the redis command RANDOMKEY.

Return "" if no key exist.

New in redis version 1.0.0.

func (*Redis) Rename

func (r *Redis) Rename(oldKey, newKey string) error

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

func (r *Redis) RenameNX(oldKey, newKey string) (bool, error)

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

func (r *Redis) SAdd(key string, member string, members ...string) (int64, error)

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

func (r *Redis) SCard(key string) (int64, error)

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

func (r *Redis) SDiff(key string, keys ...string) ([]string, error)

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

func (r *Redis) SDiffStore(dest, key string, keys ...string) (int64, error)

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

func (r *Redis) SInter(key string, keys ...string) ([]string, error)

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

func (r *Redis) SInterStore(dest, key string, keys ...string) (int64, error)

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

func (r *Redis) SIsMember(key, member string) (bool, error)

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) SMembers

func (r *Redis) SMembers(key string) ([]string, error)

SMembers executes the redis command SMEMBERS.

New in redis version 1.0.0.

func (*Redis) SMove

func (r *Redis) SMove(src, dst, member string) (bool, error)

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

func (r *Redis) SPop(key string, count ...int) ([]string, error)

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

func (r *Redis) SRandMember(key string, count ...int) ([]string, error)

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

func (r *Redis) SRem(key, member string, members ...string) (int64, error)

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

func (r *Redis) SUnion(key string, keys ...string) ([]string, error)

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

func (r *Redis) SUnionStore(dest, key string, keys ...string) (int64, error)

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

func (r *Redis) Save() error

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

func (r *Redis) Select(index int) error

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

func (r *Redis) Set(key string, value interface{}, args ...interface{}) (err error)

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

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

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

func (r *Redis) SetEX(key string, timeout int, value string) error

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

func (r *Redis) SetNX(key string, value string) (bool, error)

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

func (r *Redis) SetRange(key string, offset int, value string) (int64, error)

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

func (r *Redis) Shutdown(save ...string) error

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

func (r *Redis) SlaveOf(host string, port interface{}) error

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

func (r *Redis) StrLen(key string) (int64, error)

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) SwapDB

func (r *Redis) SwapDB(index1, index2 int) error

SwapDB executes the redis command SWAPDB.

New in redis version 4.0.0.

func (*Redis) TTL

func (r *Redis) TTL(key string) (int64, error)

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

func (r *Redis) Time() (seconds, microseconds int64, err error)

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

func (r *Redis) Type(key string) (string, error)

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) Unwatch

func (r *Redis) Unwatch() error

Unwatch executes the redis command UNWATCH.

New in redis version 2.2.0.

func (*Redis) Watch

func (r *Redis) Watch(key string, keys ...string) error

Watch executes the redis command WATCH.

New in redis version 2.2.0.

func (*Redis) ZAdd

func (r *Redis) ZAdd(key string, values ...interface{}) (interface{}, error)

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

func (r *Redis) ZCard(key string) (int64, error)

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

func (r *Redis) ZCount(key string, min, max interface{}) (int64, error)

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

func (r *Redis) ZIncrBy(key string, incr float64, member string) (float64, error)

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

func (r *Redis) ZLexCount(key string, min, max interface{}) (int64, error)

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

func (r *Redis) ZRange(key string, start, stop int, WITHSCORES ...bool) ([]string, error)

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

func (r *Redis) ZRank(key, member string) (int64, error)

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

func (r *Redis) ZRem(key, member string, members ...string) (int64, error)

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

func (r *Redis) ZRemRangeByLex(key string, min, max interface{}) (int64, error)

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

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

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

func (r *Redis) ZRemRangeByScore(key string, min, max interface{}) (int64, error)

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

func (r *Redis) ZRevRange(key string, start, stop int, WITHSCORES ...bool) ([]string, error)

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

func (r *Redis) ZRevRank(key, member string) (int64, error)

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

func (r *Redis) ZScore(key, member string) (float64, error)

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]

Jump to

Keyboard shortcuts

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