Pkg.go.dev is a new destination for Go discovery & docs. Check it out at pkg.go.dev/gopkg.in/redis.v2 and share your feedback.

package redis

import "gopkg.in/redis.v2"

Package redis implements a Redis client.

Example (CustomCommand) Code: Get := func(client *redis.Client, key string) *redis.StringCmd { cmd := redis.NewStringCmd("GET", key) client.Process(cmd) return cmd } v, err := Get(client, "key_does_not_exist").Result() fmt.Printf("%q %s", v, err) Output: "" redis: nil

command.go commands.go doc.go error.go multi.go parser.go pipeline.go pool.go pubsub.go rate_limit.go redis.go script.go sentinel.go

❖ var Nil = errorf("redis: nil")

Redis nil reply.

❖ var TxFailedErr = errorf("redis: transaction failed")

Redis transaction failed.

❖ type BitCount struct { Start , End int64 }

❖ type BoolCmd struct { // contains filtered or unexported fields }

❖ type BoolSliceCmd struct { // contains filtered or unexported fields }

❖ type Client struct { // contains filtered or unexported fields }

Example Code: if err := client.Set("foo", "bar").Err(); err != nil { panic(err) } v, err := client.Get("hello").Result() fmt.Printf("%q %q %v", v, err, err == redis.Nil) Output: "" "redis: nil" true

Example Code: client := redis.NewFailoverClient(&redis.FailoverOptions{ MasterName: "master", SentinelAddrs: []string{":26379"}, }) pong, err := client.Ping().Result() fmt.Println(pong, err) Output: PONG <nil>

Deprecated. Use NewClient instead.

Example Code: client := redis.NewTCPClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) pong, err := client.Ping().Result() fmt.Println(pong, err) Output: PONG <nil>

Deprecated. Use NewClient instead.

❖ func (c *Client) Append(key, value string) *IntCmd

❖ func (c *Client) BLPop(timeout int64, keys ...string) *StringSliceCmd

❖ func (c *Client) BRPop(timeout int64, keys ...string) *StringSliceCmd

❖ func (c *Client) BRPopLPush(source, destination string, timeout int64) *StringCmd

❖ func (c *Client) BitCount(key string, bitCount *BitCount) *IntCmd

❖ func (c *Client) BitOpAnd(destKey string, keys ...string) *IntCmd

❖ func (c *Client) BitOpNot(destKey string, key string) *IntCmd

❖ func (c *Client) BitOpOr(destKey string, keys ...string) *IntCmd

❖ func (c *Client) BitOpXor(destKey string, keys ...string) *IntCmd

Close closes the client, releasing any open resources.

❖ func (c *Client) ConfigSet(parameter, value string) *StatusCmd

❖ func (c *Client) DecrBy(key string, decrement int64) *IntCmd

❖ func (c *Client) Eval(script string, keys []string, args []string) *Cmd

❖ func (c *Client) EvalSha(sha1 string, keys []string, args []string) *Cmd

❖ func (c *Client) Expire(key string, dur time.Duration) *BoolCmd

❖ func (c *Client) ExpireAt(key string, tm time.Time) *BoolCmd

❖ func (c *Client) GetBit(key string, offset int64) *IntCmd

❖ func (c *Client) GetRange(key string, start, end int64) *StringCmd

❖ func (c *Client) GetSet(key, value string) *StringCmd

❖ func (c *Client) HDel(key string, fields ...string) *IntCmd

❖ func (c *Client) HExists(key, field string) *BoolCmd

❖ func (c *Client) HGet(key, field string) *StringCmd

❖ func (c *Client) HIncrBy(key, field string, incr int64) *IntCmd

❖ func (c *Client) HIncrByFloat(key, field string, incr float64) *FloatCmd

❖ func (c *Client) HMGet(key string, fields ...string) *SliceCmd

❖ func (c *Client) HMSet(key, field, value string, pairs ...string) *StatusCmd

❖ func (c *Client) HScan(key string, cursor int64, match string, count int64) *ScanCmd

❖ func (c *Client) HSet(key, field, value string) *BoolCmd

❖ func (c *Client) HSetNX(key, field, value string) *BoolCmd

Example Code: if err := client.Incr("counter").Err(); err != nil { panic(err) } n, err := client.Get("counter").Int64() fmt.Println(n, err) Output: 1 <nil>

❖ func (c *Client) IncrBy(key string, value int64) *IntCmd

❖ func (c *Client) IncrByFloat(key string, value float64) *FloatCmd

❖ func (c *Client) LIndex(key string, index int64) *StringCmd

❖ func (c *Client) LInsert(key, op, pivot, value string) *IntCmd

❖ func (c *Client) LPush(key string, values ...string) *IntCmd

❖ func (c *Client) LPushX(key, value string) *IntCmd

❖ func (c *Client) LRange(key string, start, stop int64) *StringSliceCmd

❖ func (c *Client) LRem(key string, count int64, value string) *IntCmd

❖ func (c *Client) LSet(key string, index int64, value string) *StatusCmd

❖ func (c *Client) LTrim(key string, start, stop int64) *StatusCmd

❖ func (c *Client) Migrate(host, port, key string, db, timeout int64) *StatusCmd

❖ func (c *Client) Move(key string, db int64) *BoolCmd

❖ func (c *Client) PExpire(key string, dur time.Duration) *BoolCmd

❖ func (c *Client) PExpireAt(key string, tm time.Time) *BoolCmd

❖ func (c *Client) PSetEx(key string, dur time.Duration, value string) *StatusCmd

Example Code: cmds, err := client.Pipelined(func(c *redis.Pipeline) error { c.Set("key1", "hello1") c.Get("key1") return nil }) fmt.Println(err) set := cmds[0].(*redis.StatusCmd) fmt.Println(set) get := cmds[1].(*redis.StringCmd) fmt.Println(get) Output: <nil> SET key1 hello1: OK GET key1: hello1

❖ func (c *Client) Publish(channel, message string) *IntCmd

❖ func (c *Client) RPopLPush(source, destination string) *StringCmd

❖ func (c *Client) RPush(key string, values ...string) *IntCmd

❖ func (c *Client) RPushX(key string, value string) *IntCmd

❖ func (c *Client) Rename(key, newkey string) *StatusCmd

❖ func (c *Client) RenameNX(key, newkey string) *BoolCmd

❖ func (c *Client) Restore(key string, ttl int64, value string) *StatusCmd

❖ func (c *Client) SAdd(key string, members ...string) *IntCmd

❖ func (c *Client) SDiffStore(destination string, keys ...string) *IntCmd

❖ func (c *Client) SInterStore(destination string, keys ...string) *IntCmd

❖ func (c *Client) SIsMember(key, member string) *BoolCmd

❖ func (c *Client) SMove(source, destination, member string) *BoolCmd

❖ func (c *Client) SRem(key string, members ...string) *IntCmd

❖ func (c *Client) SScan(key string, cursor int64, match string, count int64) *ScanCmd

❖ func (c *Client) SUnionStore(destination string, keys ...string) *IntCmd

❖ func (c *Client) Scan(cursor int64, match string, count int64) *ScanCmd

❖ func (c *Client) Set(key, value string) *StatusCmd

❖ func (c *Client) SetBit(key string, offset int64, value int) *IntCmd

❖ func (c *Client) SetEx(key string, dur time.Duration, value string) *StatusCmd

❖ func (c *Client) SetNX(key, value string) *BoolCmd

❖ func (c *Client) SetRange(key string, offset int64, value string) *IntCmd

❖ func (c *Client) SlaveOf(host, port string) *StatusCmd

❖ func (c *Client) Sort(key string, sort Sort) *StringSliceCmd

❖ func (c *Client) ZAdd(key string, members ...Z) *IntCmd

❖ func (c *Client) ZCount(key, min, max string) *IntCmd

❖ func (c *Client) ZIncrBy(key string, increment float64, member string) *FloatCmd

❖ func (c *Client) ZInterStore( destination string, store ZStore, keys ...string, ) *IntCmd

❖ func (c *Client) ZRange(key string, start, stop int64) *StringSliceCmd

❖ func (c *Client) ZRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd

❖ func (c *Client) ZRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd

❖ func (c *Client) ZRangeWithScores(key string, start, stop int64) *ZSliceCmd

❖ func (c *Client) ZRank(key, member string) *IntCmd

❖ func (c *Client) ZRem(key string, members ...string) *IntCmd

❖ func (c *Client) ZRemRangeByRank(key string, start, stop int64) *IntCmd

❖ func (c *Client) ZRemRangeByScore(key, min, max string) *IntCmd

❖ func (c *Client) ZRevRange(key, start, stop string) *StringSliceCmd

❖ func (c *Client) ZRevRangeByScore(key string, opt ZRangeByScore) *StringSliceCmd

❖ func (c *Client) ZRevRangeByScoreWithScores(key string, opt ZRangeByScore) *ZSliceCmd

❖ func (c *Client) ZRevRangeWithScores(key, start, stop string) *ZSliceCmd

❖ func (c *Client) ZRevRank(key, member string) *IntCmd

❖ func (c *Client) ZScan(key string, cursor int64, match string, count int64) *ScanCmd

❖ func (c *Client) ZScore(key, member string) *FloatCmd

❖ func (c *Client) ZUnionStore( destination string, store ZStore, keys ...string, ) *IntCmd

❖ type Cmd struct { // contains filtered or unexported fields }

❖ type Cmder interface { Err () error String () string // contains filtered or unexported methods }

❖ type DurationCmd struct { // contains filtered or unexported fields }

❖ func NewDurationCmd(precision time.Duration, args ...string) *DurationCmd

❖ type FailoverOptions struct { MasterName string SentinelAddrs []string Password string DB int64 PoolSize int DialTimeout time.Duration ReadTimeout time.Duration WriteTimeout time.Duration IdleTimeout time.Duration }

❖ type FloatCmd struct { // contains filtered or unexported fields }

❖ type IntCmd struct { // contains filtered or unexported fields }

❖ type Message struct { Channel string Payload string }

❖ type Multi struct { *Client }

Not thread-safe.

Example Code: incr := func(tx *redis.Multi) ([]redis.Cmder, error) { s, err := tx.Get("key").Result() if err != nil && err != redis.Nil { return nil, err } n, _ := strconv.ParseInt(s, 10, 64) return tx.Exec(func() error { tx.Set("key", strconv.FormatInt(n+1, 10)) return nil }) } client.Del("key") tx := client.Multi() defer tx.Close() watch := tx.Watch("key") _ = watch.Err() for { cmds, err := incr(tx) if err == redis.TxFailedErr { continue } else if err != nil { panic(err) } fmt.Println(cmds, err) break } Output: [SET key 1: OK] <nil>

Exec always returns list of commands. If transaction fails TxFailedErr is returned. Otherwise Exec returns error of the first failed command or nil.

❖ type Options struct { Network string Addr string // Dialer creates new network connection and has priority over // Network and Addr options. Dialer func() (net.Conn, error) Password string DB int64 DialTimeout time.Duration ReadTimeout time.Duration WriteTimeout time.Duration PoolSize int IdleTimeout time.Duration }

❖ type PMessage struct { Channel string Pattern string Payload string }

❖ type Pipeline struct { *Client // contains filtered or unexported fields }

Not thread-safe.

Example Code: pipeline := client.Pipeline() set := pipeline.Set("key1", "hello1") get := pipeline.Get("key1") cmds, err := pipeline.Exec() fmt.Println(cmds, err) fmt.Println(set) fmt.Println(get) Output: [SET key1 hello1: OK GET key1: hello1] <nil> SET key1 hello1: OK GET key1: hello1

Exec always returns list of commands and error of the first failed command if any.

❖ type PubSub struct { // contains filtered or unexported fields }

Not thread-safe.

Example Code: pubsub := client.PubSub() defer pubsub.Close() err := pubsub.Subscribe("mychannel") _ = err msg, err := pubsub.Receive() fmt.Println(msg, err) pub := client.Publish("mychannel", "hello") _ = pub.Err() msg, err = pubsub.Receive() fmt.Println(msg, err) Output: subscribe: mychannel <nil> Message<mychannel: hello> <nil>

Close closes the client, releasing any open resources.

❖ type ScanCmd struct { // contains filtered or unexported fields }

❖ type Script struct { // contains filtered or unexported fields }

Example Code: setnx := redis.NewScript(` if redis.call("get", KEYS[1]) == false then redis.call("set", KEYS[1], ARGV[1]) return 1 end return 0 `) v1, err := setnx.Run(client, []string{"keynx"}, []string{"foo"}).Result() fmt.Println(v1.(int64), err) v2, err := setnx.Run(client, []string{"keynx"}, []string{"bar"}).Result() fmt.Println(v2.(int64), err) get := client.Get("keynx") fmt.Println(get) Output: 1 <nil> 0 <nil> GET keynx: foo

❖ func (s *Script) Eval(c scripter, keys []string, args []string) *Cmd

❖ func (s *Script) EvalSha(c scripter, keys []string, args []string) *Cmd

❖ func (s *Script) Run(c *Client, keys []string, args []string) *Cmd

❖ type SliceCmd struct { // contains filtered or unexported fields }

❖ type Sort struct { By string Offset , Count float64 Get []string Order string IsAlpha bool Store string }

❖ type StatusCmd struct { // contains filtered or unexported fields }

❖ type StringCmd struct { // contains filtered or unexported fields }

❖ type StringSliceCmd struct { // contains filtered or unexported fields }

❖ type StringStringMapCmd struct { // contains filtered or unexported fields }

❖ type Subscription struct { Kind string Channel string Count int }

❖ type Z struct { Score float64 Member string }

❖ type ZRangeByScore struct { Min , Max string Offset , Count int64 }

❖ type ZSliceCmd struct { // contains filtered or unexported fields }

❖ type ZStore struct { Weights []int64 Aggregate string }