redis

package
v0.0.0-...-58f0d11 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2014 License: BSD-3-Clause Imports: 13 Imported by: 0

README

Redis client for Golang Build Status

Supports:

  • Redis 2.6 commands except QUIT, MONITOR, SLOWLOG and SYNC.
  • Pub/sub.
  • Transactions.
  • Pipelining.
  • Connection pool.
  • TLS connections.
  • Thread safety.
  • Timeouts.

API docs: http://godoc.org/github.com/vmihailenco/redis/v2. Examples: http://godoc.org/github.com/vmihailenco/redis/v2#pkg-examples.

Installation

Install:

go get github.com/vmihailenco/redis/v2

Upgrading from previous version

Type system should catch most changes. But you have to manually change SetEx, PSetEx, Expire and PExpire to use time.Duration instead of int64.

Getting started

Let's start with connecting to Redis using TCP:

client := redis.NewTCPClient(&redis.Options{
	Addr:     "localhost:6379",
	Password: "", // no password set
	DB:       0,  // use default DB
})
defer client.Close()

ping := client.Ping()
fmt.Println(ping.Val(), ping.Err())
// Output: PONG <nil>

or using Unix socket:

client := redis.NewUnixClient(&redis.Options{
	Addr: "/tmp/redis.sock",
})
defer client.Close()

ping := client.Ping()
fmt.Println(ping.Val(), ping.Err())
// Output: PONG <nil>

Then we can start sending commands:

set := client.Set("foo", "bar")
fmt.Println(set.Err(), set.Val())

get := client.Get("foo")
fmt.Println(get.Err(), get.Val())

// Output: <nil> OK
// <nil> bar

We can also pipeline two commands together:

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

or:

client := redis.NewTCPClient(&redis.Options{
	Addr: ":6379",
})
defer client.Close()

cmds, err := client.Pipelined(func(c *redis.Pipeline) {
	c.Set("key1", "hello1")
	c.Get("key2")
})
fmt.Println(cmds, err)
// Output: [SET key1 hello1: OK GET key2: (nil)] (nil)

We can also send several commands in transaction:

incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
	get := tx.Get("key")
	if err := get.Err(); err != nil && err != redis.Nil {
		return nil, err
	}

	val, _ := strconv.ParseInt(get.Val(), 10, 64)

	return tx.Exec(func() {
		tx.Set("key", strconv.FormatInt(val+1, 10))
	})
}

client := redis.NewTCPClient(&redis.Options{
	Addr: ":6379",
})
defer client.Close()

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>

To subscribe to the channel:

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 1} <nil>
// &{mychannel hello} <nil>

To use Lua scripting:

client := redis.NewTCPClient(&redis.Options{
	Addr: ":6379",
})
defer client.Close()

setnx := redis.NewScript(`
    if redis.call("get", KEYS[1]) == false then
        redis.call("set", KEYS[1], ARGV[1])
        return 1
    end
    return 0
`)

run1 := setnx.Run(client, []string{"keynx"}, []string{"foo"})
fmt.Println(run1.Val().(int64), run1.Err())

run2 := setnx.Run(client, []string{"keynx"}, []string{"bar"})
fmt.Println(run2.Val().(int64), run2.Err())

get := client.Get("keynx")
fmt.Println(get)

// Output: 1 <nil>
// 0 <nil>
// GET keynx: foo

You can also write custom commands:

Get := func(client *redis.Client, key string) *redis.StringCmd {
	cmd := redis.NewStringCmd("GET", key)
	client.Process(cmd)
	return cmd
}

client := redis.NewTCPClient(&redis.Options{
	Addr: ":6379",
})
defer client.Close()

get := Get(client, "key_does_not_exist")
fmt.Printf("%q %s", get.Val(), get.Err())
// Output: "" (nil)

Look and feel

Some corner cases:

SORT list LIMIT 0 2 ASC
client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"})

ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2
client.ZRangeByScoreWithScores("zset", redis.ZRangeByScore{
    Min: "-inf",
    Max: "+inf",
    Offset: 0,
    Count: 2,
})

ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM
client.ZInterStore("out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2")

EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello"
client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, []string{"hello"})

Contributing

Configure Redis to allow maximum 10 clients:

maxclients 10

Run tests:

go test -gocheck.v

Run benchmarks:

go test -gocheck.b

Documentation

Overview

Package github.com/vmihailenco/redis/v2 implements a Redis client.

Example (CustomCommand)
Get := func(client *redis.Client, key string) *redis.StringCmd {
	cmd := redis.NewStringCmd("GET", key)
	client.Process(cmd)
	return cmd
}

get := Get(client, "key_does_not_exist")
fmt.Printf("%q %s", get.Val(), get.Err())
Output:

"" redis: nil

Index

Examples

Constants

This section is empty.

Variables

View Source
var Nil = errors.New("redis: nil")

Redis nil reply.

View Source
var TxFailedErr = errors.New("redis: transaction failed")

Redis transaction failed.

Functions

This section is empty.

Types

type BitCount

type BitCount struct {
	Start, End int64
}

type BoolCmd

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

func NewBoolCmd

func NewBoolCmd(args ...string) *BoolCmd

func (BoolCmd) Err

func (cmd BoolCmd) Err() error

func (BoolCmd) String

func (cmd BoolCmd) String() string

func (*BoolCmd) Val

func (cmd *BoolCmd) Val() bool

type BoolSliceCmd

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

func NewBoolSliceCmd

func NewBoolSliceCmd(args ...string) *BoolSliceCmd

func (BoolSliceCmd) Err

func (cmd BoolSliceCmd) Err() error

func (BoolSliceCmd) String

func (cmd BoolSliceCmd) String() string

func (*BoolSliceCmd) Val

func (cmd *BoolSliceCmd) Val() []bool

type Client

type Client struct {
	// contains filtered or unexported fields
}
Example
set := client.Set("foo", "bar")
fmt.Println(set.Val(), set.Err())

get := client.Get("hello")
fmt.Printf("%q %s %v", get.Val(), get.Err(), get.Err() == redis.Nil)
Output:

OK <nil>
"" redis: nil true

func NewTCPClient

func NewTCPClient(opt *Options) *Client
Example
client := redis.NewTCPClient(&redis.Options{
	Addr:     "localhost:6379",
	Password: "", // no password set
	DB:       0,  // use default DB
})
defer client.Close()

ping := client.Ping()
fmt.Println(ping.Val(), ping.Err())
Output:

PONG <nil>

func NewUnixClient

func NewUnixClient(opt *Options) *Client

func (*Client) Append

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

func (*Client) Auth

func (c *Client) Auth(password string) *StatusCmd

func (*Client) BLPop

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

func (*Client) BRPop

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

func (*Client) BRPopLPush

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

func (*Client) BgRewriteAOF

func (c *Client) BgRewriteAOF() *StatusCmd

func (*Client) BgSave

func (c *Client) BgSave() *StatusCmd

func (*Client) BitCount

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

func (*Client) BitOpAnd

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

func (*Client) BitOpNot

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

func (*Client) BitOpOr

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

func (*Client) BitOpXor

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

func (*Client) ClientKill

func (c *Client) ClientKill(ipPort string) *StatusCmd

func (*Client) ClientList

func (c *Client) ClientList() *StringCmd

func (Client) Close

func (c Client) Close() error

Close closes the client, releasing any open resources.

func (*Client) ConfigGet

func (c *Client) ConfigGet(parameter string) *SliceCmd

func (*Client) ConfigResetStat

func (c *Client) ConfigResetStat() *StatusCmd

func (*Client) ConfigSet

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

func (*Client) DbSize

func (c *Client) DbSize() *IntCmd

func (*Client) DebugObject

func (c *Client) DebugObject(key string) *StringCmd

func (*Client) Decr

func (c *Client) Decr(key string) *IntCmd

func (*Client) DecrBy

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

func (*Client) Del

func (c *Client) Del(keys ...string) *IntCmd

func (*Client) Dump

func (c *Client) Dump(key string) *StringCmd

func (*Client) Echo

func (c *Client) Echo(message string) *StringCmd

func (*Client) Eval

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

func (*Client) EvalSha

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

func (*Client) Exists

func (c *Client) Exists(key string) *BoolCmd

func (*Client) Expire

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

func (*Client) ExpireAt

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

func (*Client) FlushAll

func (c *Client) FlushAll() *StatusCmd

func (*Client) FlushDb

func (c *Client) FlushDb() *StatusCmd

func (*Client) Get

func (c *Client) Get(key string) *StringCmd

func (*Client) GetBit

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

func (*Client) GetRange

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

func (*Client) GetSet

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

func (*Client) HDel

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

func (*Client) HExists

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

func (*Client) HGet

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

func (*Client) HGetAll

func (c *Client) HGetAll(key string) *StringSliceCmd

func (*Client) HGetAllMap

func (c *Client) HGetAllMap(key string) *StringStringMapCmd

func (*Client) HIncrBy

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

func (*Client) HIncrByFloat

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

func (*Client) HKeys

func (c *Client) HKeys(key string) *StringSliceCmd

func (*Client) HLen

func (c *Client) HLen(key string) *IntCmd

func (*Client) HMGet

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

func (*Client) HMSet

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

func (*Client) HSet

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

func (*Client) HSetNX

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

func (*Client) HVals

func (c *Client) HVals(key string) *StringSliceCmd

func (*Client) Incr

func (c *Client) Incr(key string) *IntCmd

func (*Client) IncrBy

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

func (*Client) IncrByFloat

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

func (*Client) Info

func (c *Client) Info() *StringCmd

func (*Client) Keys

func (c *Client) Keys(pattern string) *StringSliceCmd

func (*Client) LIndex

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

func (*Client) LInsert

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

func (*Client) LLen

func (c *Client) LLen(key string) *IntCmd

func (*Client) LPop

func (c *Client) LPop(key string) *StringCmd

func (*Client) LPush

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

func (*Client) LPushX

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

func (*Client) LRange

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

func (*Client) LRem

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

func (*Client) LSet

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

func (*Client) LTrim

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

func (*Client) LastSave

func (c *Client) LastSave() *IntCmd

func (*Client) MGet

func (c *Client) MGet(keys ...string) *SliceCmd

func (*Client) MSet

func (c *Client) MSet(pairs ...string) *StatusCmd

func (*Client) MSetNX

func (c *Client) MSetNX(pairs ...string) *BoolCmd

func (*Client) Migrate

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

func (*Client) Move

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

func (*Client) Multi

func (c *Client) Multi() *Multi

func (*Client) ObjectEncoding

func (c *Client) ObjectEncoding(keys ...string) *StringCmd

func (*Client) ObjectIdleTime

func (c *Client) ObjectIdleTime(keys ...string) *DurationCmd

func (*Client) ObjectRefCount

func (c *Client) ObjectRefCount(keys ...string) *IntCmd

func (*Client) PExpire

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

func (*Client) PExpireAt

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

func (*Client) PSetEx

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

func (*Client) PTTL

func (c *Client) PTTL(key string) *DurationCmd

func (*Client) Persist

func (c *Client) Persist(key string) *BoolCmd

func (*Client) Ping

func (c *Client) Ping() *StatusCmd

func (*Client) Pipeline

func (c *Client) Pipeline() *Pipeline

func (*Client) Pipelined

func (c *Client) Pipelined(f func(*Pipeline)) ([]Cmder, error)
Example
cmds, err := client.Pipelined(func(c *redis.Pipeline) {
	c.Set("key1", "hello1")
	c.Get("key1")
})
set := cmds[0].(*redis.StatusCmd)
get := cmds[1].(*redis.StringCmd)
fmt.Println(set, get, err)
Output:

SET key1 hello1: OK GET key1: hello1 <nil>

func (Client) Process

func (c Client) Process(cmd Cmder)

func (*Client) PubSub

func (c *Client) PubSub() *PubSub

func (*Client) Publish

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

func (*Client) Quit

func (c *Client) Quit() *StatusCmd

func (*Client) RPop

func (c *Client) RPop(key string) *StringCmd

func (*Client) RPopLPush

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

func (*Client) RPush

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

func (*Client) RPushX

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

func (*Client) RandomKey

func (c *Client) RandomKey() *StringCmd

func (*Client) Rename

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

func (*Client) RenameNX

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

func (*Client) Restore

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

func (*Client) SAdd

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

func (*Client) SCard

func (c *Client) SCard(key string) *IntCmd

func (*Client) SDiff

func (c *Client) SDiff(keys ...string) *StringSliceCmd

func (*Client) SDiffStore

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

func (*Client) SInter

func (c *Client) SInter(keys ...string) *StringSliceCmd

func (*Client) SInterStore

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

func (*Client) SIsMember

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

func (*Client) SMembers

func (c *Client) SMembers(key string) *StringSliceCmd

func (*Client) SMove

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

func (*Client) SPop

func (c *Client) SPop(key string) *StringCmd

func (*Client) SRandMember

func (c *Client) SRandMember(key string) *StringCmd

func (*Client) SRem

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

func (*Client) SUnion

func (c *Client) SUnion(keys ...string) *StringSliceCmd

func (*Client) SUnionStore

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

func (*Client) Save

func (c *Client) Save() *StatusCmd

func (*Client) ScriptExists

func (c *Client) ScriptExists(scripts ...string) *BoolSliceCmd

func (*Client) ScriptFlush

func (c *Client) ScriptFlush() *StatusCmd

func (*Client) ScriptKill

func (c *Client) ScriptKill() *StatusCmd

func (*Client) ScriptLoad

func (c *Client) ScriptLoad(script string) *StringCmd

func (*Client) Select

func (c *Client) Select(index int64) *StatusCmd

func (*Client) Set

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

func (*Client) SetBit

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

func (*Client) SetEx

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

func (*Client) SetNX

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

func (*Client) SetRange

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

func (*Client) Shutdown

func (c *Client) Shutdown() *StatusCmd

func (*Client) ShutdownNoSave

func (c *Client) ShutdownNoSave() *StatusCmd

func (*Client) ShutdownSave

func (c *Client) ShutdownSave() *StatusCmd

func (*Client) SlaveOf

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

func (*Client) SlowLog

func (c *Client) SlowLog()

func (*Client) Sort

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

func (*Client) StrLen

func (c *Client) StrLen(key string) *IntCmd

func (*Client) Sync

func (c *Client) Sync()

func (*Client) TTL

func (c *Client) TTL(key string) *DurationCmd

func (*Client) Time

func (c *Client) Time() *StringSliceCmd

func (*Client) Type

func (c *Client) Type(key string) *StatusCmd

func (*Client) ZAdd

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

func (*Client) ZCard

func (c *Client) ZCard(key string) *IntCmd

func (*Client) ZCount

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

func (*Client) ZIncrBy

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

func (*Client) ZInterStore

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

func (*Client) ZRange

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

func (*Client) ZRangeByScore

func (c *Client) ZRangeByScore(key string, opts ZRangeByScore) *StringSliceCmd

func (*Client) ZRangeByScoreWithScores

func (c *Client) ZRangeByScoreWithScores(key string, opts ZRangeByScore) *StringSliceCmd

func (*Client) ZRangeByScoreWithScoresMap

func (c *Client) ZRangeByScoreWithScoresMap(key string, opts ZRangeByScore) *StringFloatMapCmd

func (*Client) ZRangeWithScores

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

func (*Client) ZRangeWithScoresMap

func (c *Client) ZRangeWithScoresMap(key string, start, stop int64) *StringFloatMapCmd

func (*Client) ZRank

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

func (*Client) ZRem

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

func (*Client) ZRemRangeByRank

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

func (*Client) ZRemRangeByScore

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

func (*Client) ZRevRange

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

func (*Client) ZRevRangeByScore

func (c *Client) ZRevRangeByScore(key, start, stop string, offset, count int64) *StringSliceCmd

func (*Client) ZRevRangeByScoreWithScores

func (c *Client) ZRevRangeByScoreWithScores(key, start, stop string, offset, count int64) *StringSliceCmd

func (*Client) ZRevRangeByScoreWithScoresMap

func (c *Client) ZRevRangeByScoreWithScoresMap(
	key, start, stop string, offset, count int64) *StringFloatMapCmd

func (*Client) ZRevRangeWithScores

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

func (*Client) ZRevRangeWithScoresMap

func (c *Client) ZRevRangeWithScoresMap(key, start, stop string) *StringFloatMapCmd

func (*Client) ZRevRank

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

func (*Client) ZScore

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

func (*Client) ZUnionStore

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

type Cmd

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

func NewCmd

func NewCmd(args ...string) *Cmd

func (Cmd) Err

func (cmd Cmd) Err() error

func (Cmd) String

func (cmd Cmd) String() string

func (*Cmd) Val

func (cmd *Cmd) Val() interface{}

type Cmder

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

type DurationCmd

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

func NewDurationCmd

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

func (DurationCmd) Err

func (cmd DurationCmd) Err() error

func (DurationCmd) String

func (cmd DurationCmd) String() string

func (*DurationCmd) Val

func (cmd *DurationCmd) Val() time.Duration

type FloatCmd

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

func NewFloatCmd

func NewFloatCmd(args ...string) *FloatCmd

func (FloatCmd) Err

func (cmd FloatCmd) Err() error

func (FloatCmd) String

func (cmd FloatCmd) String() string

func (*FloatCmd) Val

func (cmd *FloatCmd) Val() float64

type IntCmd

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

func NewIntCmd

func NewIntCmd(args ...string) *IntCmd

func (IntCmd) Err

func (cmd IntCmd) Err() error

func (IntCmd) String

func (cmd IntCmd) String() string

func (*IntCmd) Val

func (cmd *IntCmd) Val() int64

type Message

type Message struct {
	Channel string
	Payload string
}

type Multi

type Multi struct {
	*Client
}

Not thread-safe.

Example
incr := func(tx *redis.Multi) ([]redis.Cmder, error) {
	get := tx.Get("key")
	if err := get.Err(); err != nil && err != redis.Nil {
		return nil, err
	}

	val, _ := strconv.ParseInt(get.Val(), 10, 64)

	return tx.Exec(func() {
		tx.Set("key", strconv.FormatInt(val+1, 10))
	})
}

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>

func (*Multi) Close

func (c *Multi) Close() error

func (*Multi) Discard

func (c *Multi) Discard() error

func (*Multi) Exec

func (c *Multi) Exec(f func()) ([]Cmder, error)

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

func (Multi) Process

func (c Multi) Process(cmd Cmder)

func (*Multi) Unwatch

func (c *Multi) Unwatch(keys ...string) *StatusCmd

func (*Multi) Watch

func (c *Multi) Watch(keys ...string) *StatusCmd

type Options

type Options struct {
	Addr     string
	Password string
	DB       int64

	PoolSize int

	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
}

type PMessage

type PMessage struct {
	Channel string
	Pattern string
	Payload string
}

type Pipeline

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

Not thread-safe.

Example
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

func (*Pipeline) Close

func (c *Pipeline) Close() error

func (*Pipeline) Discard

func (c *Pipeline) Discard() error

func (*Pipeline) Exec

func (c *Pipeline) Exec() ([]Cmder, error)

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

func (Pipeline) Process

func (c Pipeline) Process(cmd Cmder)

type PubSub

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

Not thread-safe.

Example
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 1} <nil>
&{mychannel hello} <nil>

func (PubSub) Close

func (c PubSub) Close() error

Close closes the client, releasing any open resources.

func (*PubSub) PSubscribe

func (c *PubSub) PSubscribe(patterns ...string) error

func (*PubSub) PUnsubscribe

func (c *PubSub) PUnsubscribe(patterns ...string) error

func (PubSub) Process

func (c PubSub) Process(cmd Cmder)

func (*PubSub) Receive

func (c *PubSub) Receive() (interface{}, error)

func (*PubSub) ReceiveTimeout

func (c *PubSub) ReceiveTimeout(timeout time.Duration) (interface{}, error)

func (*PubSub) Subscribe

func (c *PubSub) Subscribe(channels ...string) error

func (*PubSub) Unsubscribe

func (c *PubSub) Unsubscribe(channels ...string) error

type Script

type Script struct {
	// contains filtered or unexported fields
}
Example
setnx := redis.NewScript(`
        if redis.call("get", KEYS[1]) == false then
            redis.call("set", KEYS[1], ARGV[1])
            return 1
        end
        return 0
    `)

run1 := setnx.Run(client, []string{"keynx"}, []string{"foo"})
fmt.Println(run1.Val().(int64), run1.Err())

run2 := setnx.Run(client, []string{"keynx"}, []string{"bar"})
fmt.Println(run2.Val().(int64), run2.Err())

get := client.Get("keynx")
fmt.Println(get)
Output:

1 <nil>
0 <nil>
GET keynx: foo

func NewScript

func NewScript(src string) *Script

func (*Script) Eval

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

func (*Script) EvalSha

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

func (*Script) Exists

func (s *Script) Exists(c scripter) *BoolSliceCmd

func (*Script) Load

func (s *Script) Load(c scripter) *StringCmd

func (*Script) Run

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

type SliceCmd

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

func NewSliceCmd

func NewSliceCmd(args ...string) *SliceCmd

func (SliceCmd) Err

func (cmd SliceCmd) Err() error

func (SliceCmd) String

func (cmd SliceCmd) String() string

func (*SliceCmd) Val

func (cmd *SliceCmd) Val() []interface{}

type Sort

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

type StatusCmd

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

func NewStatusCmd

func NewStatusCmd(args ...string) *StatusCmd

func (StatusCmd) Err

func (cmd StatusCmd) Err() error

func (StatusCmd) String

func (cmd StatusCmd) String() string

func (*StatusCmd) Val

func (cmd *StatusCmd) Val() string

type StringCmd

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

func NewStringCmd

func NewStringCmd(args ...string) *StringCmd

func (StringCmd) Err

func (cmd StringCmd) Err() error

func (StringCmd) String

func (cmd StringCmd) String() string

func (*StringCmd) Val

func (cmd *StringCmd) Val() string

type StringFloatMapCmd

type StringFloatMapCmd struct {
	// contains filtered or unexported fields
}

func NewStringFloatMapCmd

func NewStringFloatMapCmd(args ...string) *StringFloatMapCmd

func (StringFloatMapCmd) Err

func (cmd StringFloatMapCmd) Err() error

func (StringFloatMapCmd) String

func (cmd StringFloatMapCmd) String() string

func (*StringFloatMapCmd) Val

func (cmd *StringFloatMapCmd) Val() map[string]float64

type StringSliceCmd

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

func NewStringSliceCmd

func NewStringSliceCmd(args ...string) *StringSliceCmd

func (StringSliceCmd) Err

func (cmd StringSliceCmd) Err() error

func (StringSliceCmd) String

func (cmd StringSliceCmd) String() string

func (*StringSliceCmd) Val

func (cmd *StringSliceCmd) Val() []string

type StringStringMapCmd

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

func NewStringStringMapCmd

func NewStringStringMapCmd(args ...string) *StringStringMapCmd

func (StringStringMapCmd) Err

func (cmd StringStringMapCmd) Err() error

func (StringStringMapCmd) String

func (cmd StringStringMapCmd) String() string

func (*StringStringMapCmd) Val

func (cmd *StringStringMapCmd) Val() map[string]string

type Subscription

type Subscription struct {
	Kind    string
	Channel string
	Count   int
}

type Z

type Z struct {
	Score  float64
	Member string
}

type ZRangeByScore

type ZRangeByScore struct {
	Min, Max string

	Offset, Count int64
}

type ZStore

type ZStore struct {
	Weights   []int64
	Aggregate string
}

Jump to

Keyboard shortcuts

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