redis

package module
v0.0.0-...-cdc4c1e Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2013 License: Apache-2.0 Imports: 12 Imported by: 1

README

Go-Redis

Go Clients and Connectors for Redis.

The initial release provides the interface and implementation supporting the (~) full set of current Redis commands using synchrnous call semantics. (Pipelines and asychronous goodness using the goroutines and channels is next.)

Hope to add rigorous tests as soon as I have a better understanding of the Go language tools. Same applies to the makefile.
Also am not sure regarding the efficiency of the implementation (for the obvious reasons), but definitely a goal is to make this a high performance connector.

structure

The code is consolidated into a single 'redis' package and various elements of it are usable independently (for example if you wish to roll your own API but want to use the raw bytes protocol handling aspects).

Compliance

Both Go and Redis are dynamic projects and present a challenge in fully covering the possible combinations in the wild. Given the release of Go 1, this project will focus on Go 1 based Redis compatibility; we'll deal with the far off prospect of renewed major weekly changes in Go if and when that arises.

Current status is compatible with Redis 2.4.n (2.4.9 tested) and Go 1. Redis feature set is not fully covered and is WIP.

(Always refer to compliance_note.txt for current (accurate) status for specific branches.)

Getting started:

Get, build, and startup Redis:

make
chmod +x redis-server
./redis-server redis.conf

Confirm:

alphazero[13]:entrevous$ telnet localhost 6379
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying fe80::1...
telnet: connect to address fe80::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

PING
+PONG

INFO
$282
redis_version:1.07
arch_bits:32
uptime_in_seconds:72
uptime_in_days:0
connected_clients:1
connected_slaves:0
used_memory:295232
changes_since_last_save:1
bgsave_in_progress:0
last_save_time:1259873372
total_connections_received:4
total_commands_processed:5
role:master

QUIT
Connection closed by foreign host.
alphazero[14]:entrevous$ 

get, and install, Go-Redis

Go-Redis is built using the Go tool. The tool assumes the code will be in a folder $GOPATH/src/redis .

cd $GOPATH/src
git clone git://github.com/alphazero/Go-Redis.git redis
cd redis
go install

Confirm the install has created redis.a in your $GOPATH/pkg/ folder:

ls -l $GOPATH/pkg/"$GOOS"_"$GOARCH"/redis.a

e.g. on my Mac OS X (64b)

ls -l <my-gopath>/pkg/darwin_amd64

run the benchmarks

Basic benchmarks are in ~/bench. Use Go tool (go run ) to run the individual bench apps.

cd bench
# run the asyncbench.go 
go run asyncbench.go

examples

Ciao.go is a sort of hello world for redis and should get you started for the barebones necessities of getting a client and issuing commands.

cd examples
go run ciao.go

Documentation

Overview

Package redis provides both clients and connectors for the Redis server. Both synchronous and asynchronous interaction modes are supported. Asynchronous clients (using the asynchronous connection) use pipelining.

Synchronous semantics are defined by redis.Client interface type

Usage example:

func usingRedisSync () Error {
    spec := DefaultConnectionSpec();
    pipeline := NewAsynchClient(spec);

    value, reqErr := pipeline.Get("my-key");
    if reqErr != nil { return withError (reqErr); }
}

Asynchronous semantics are defined by redis.AsyncClient interface type. Note that these clients use a pipelining connector and a single instance can be used by multiple go routines. Use of pipelining increases throughput at the cost of latency. If low latency is more important to you than throughput, and you require async call semantics, then you should use only 1 go routine per AsyncClient connection.

Usage example without timeouts (caller blocks on Get until the response from Redis has been processed.)

func usingRedisAsync () Error {
    spec := DefaultConnectionSpec();
    pipeline := NewRedisPipeline(spec);

    // async invoke of GET my-key
    // futureBytes is a FutureBytes that will have the result of the
    // Redis GET operation.
    futureBytes, reqErr := pipline.Get("my-key");
    if reqErr != nil {
        return withError (reqErr);
    }

    // ... note that you could issue additional redis commands here ...

    []byte, execErr := futureBytes.Get();
    if execErr != nil {
        return withError (execErr);
    }
}

Usage example with timeouts - same Redis op as above but here we use TryGet on the Future result with a timeout of 1 msecs:

func usingRedisAsync () Error {
    spec := DefaultConnectionSpec();
    pipeline := NewRedisPipeline(spec);

    // futureBytes is a FutureBytes
    futureBytes, reqErr := pipline.Get("my-key");
    if reqErr != nil { return withError (reqErr); }

    // ... note that you could issue additional redis commands here ...

    []byte, execErr := futureBytes.Get();
    if execErr != nil {
        return withError (execErr);
    }

    timeout := 1000000; // wait 1 msec for result
    []byte, execErr, ok := futureBytes.TryGet (timeout);
    if !ok {
        .. handle timeout here
    }
    else {
        if execErr != nil {
            return withError (execErr);
        }
    }
}

Index

Constants

View Source
const (
	TCP  = "tcp"  // tcp/ip socket
	UNIX = "unix" // unix domain socket
)

connection socket modes

View Source
const (
	DefaultReqChanSize          = 1000000
	DefaultRespChanSize         = 1000000
	DefaultTCPReadBuffSize      = 1024 * 256
	DefaultTCPWriteBuffSize     = 1024 * 256
	DefaultTCPReadTimeoutNSecs  = 1000 * time.Nanosecond
	DefaultTCPWriteTimeoutNSecs = 1000 * time.Nanosecond
	DefaultTCPLinger            = 0 // -n: finish io; 0: discard, +n: wait for n secs to finish
	DefaultTCPKeepalive         = true
	DefaultHeartbeatSecs        = 1 * time.Second
	DefaultProtocol             = REDIS_DB
)

various defaults for the connections exported for user convenience.

View Source
const (
	DefaultRedisPassword = ""
	DefaultRedisDB       = 0
	DefaultRedisPort     = 6379
	DefaultRedisHost     = "127.0.0.1"
)

Redis specific default settings exported for user convenience.

View Source
const (
	OK   = true
	PONG = true
	ERR  = false
)

Variables

This section is empty.

Functions

func CreateFuture

func CreateFuture(cmd *Command) (future interface{})

Creates a specific Future type for the given Redis command and returns it as a generic reference.

func CreateRequestBytes

func CreateRequestBytes(cmd *Command, args [][]byte) []byte

Creates the byte buffer that corresponds to the specified Command and provided command arguments.

panics on error (with redis.Error)

func GetPubSubResponse

func GetPubSubResponse(r *bufio.Reader) (msg *Message, err Error)

Fully reads and processes an expected Redis pubsub message byte sequence.

func GetResponse

func GetResponse(reader *bufio.Reader, cmd *Command) (resp Response, err Error)

Gets the response to the command.

The returned response (regardless of flavor) may have (application level) errors as sent from Redis server. (Note err will be nil in that case)

Any errors (whether runtime or bugs) are returned as redis.Error.

func NewAsynchClient

func NewAsynchClient() (c AsyncClient, err Error)

Create a new Client and connects to the Redis server using the default ConnectionSpec.

func NewAsynchClientWithSpec

func NewAsynchClientWithSpec(spec *ConnectionSpec) (client AsyncClient, err Error)

Create a new asynClient and connects to the Redis server using the specified ConnectionSpec.

func NewAsynchConnection

func NewAsynchConnection(spec *ConnectionSpec) (conn AsyncConnection, err Error)

Creates and opens a new AsyncConnection and starts the goroutines for request and response processing interaction with redis (AUTH &| SELECT)

func NewPubSubClient

func NewPubSubClient() (PubSubClient, Error)

func NewPubSubClientWithSpec

func NewPubSubClientWithSpec(spec *ConnectionSpec) (PubSubClient, Error)

func NewPubSubConnection

func NewPubSubConnection(spec *ConnectionSpec) (conn PubSubConnection, err Error)

func NewSyncConnection

func NewSyncConnection(spec *ConnectionSpec) (c SyncConnection, err Error)

Creates a new SyncConnection using the provided ConnectionSpec. Note that this function will also connect to the specified redis server.

func NewSynchClient

func NewSynchClient() (c Client, err Error)

Create a new syncClient and connects to the Redis server using the default ConnectionSpec.

func NewSynchClientWithSpec

func NewSynchClientWithSpec(spec *ConnectionSpec) (c Client, err Error)

Create a new syncClient and connects to the Redis server using the specified ConnectionSpec.

func SetFutureResult

func SetFutureResult(future interface{}, cmd *Command, r Response)

Sets the type specific result value from the response for the future reference based on the command type.

Types

type AsyncClient

type AsyncClient interface {

	// Redis QUIT command.
	Quit() (status FutureBool, err Error)

	// Redis GET command.
	Get(key string) (result FutureBytes, err Error)

	// Redis TYPE command.
	Type(key string) (result FutureKeyType, err Error)

	// Redis SET command.
	Set(key string, arg1 []byte) (status FutureBool, err Error)

	// Redis SAVE command.
	Save() (status FutureBool, err Error)

	// Redis KEYS command using "*" wildcard
	AllKeys() (result FutureKeys, err Error)

	// Redis KEYS command.
	Keys(key string) (result FutureKeys, err Error)

	// Redis EXISTS command.
	Exists(key string) (result FutureBool, err Error)

	// Redis RENAME command.
	Rename(key, arg1 string) (status FutureBool, err Error)

	// Redis INFO command.
	Info() (result FutureInfo, err Error)

	// Redis PING command.
	Ping() (status FutureBool, err Error)

	// Redis SETNX command.
	Setnx(key string, arg1 []byte) (result FutureBool, err Error)

	// Redis GETSET command.
	Getset(key string, arg1 []byte) (result FutureBytes, err Error)

	// Redis MGET command.
	Mget(key string, arg1 []string) (result FutureBytesArray, err Error)

	// Redis INCR command.
	Incr(key string) (result FutureInt64, err Error)

	// Redis INCRBY command.
	Incrby(key string, arg1 int64) (result FutureInt64, err Error)

	// Redis DECR command.
	Decr(key string) (result FutureInt64, err Error)

	// Redis DECRBY command.
	Decrby(key string, arg1 int64) (result FutureInt64, err Error)

	// Redis DEL command.
	Del(key string) (result FutureBool, err Error)

	// Redis RANDOMKEY command.
	Randomkey() (result FutureString, err Error)

	// Redis RENAMENX command.
	Renamenx(key string, arg1 string) (result FutureBool, err Error)

	// Redis DBSIZE command.
	Dbsize() (result FutureInt64, err Error)

	// Redis EXPIRE command.
	Expire(key string, arg1 int64) (result FutureBool, err Error)

	// Redis TTL command.
	Ttl(key string) (result FutureInt64, err Error)

	// Redis RPUSH command.
	Rpush(key string, arg1 []byte) (status FutureBool, err Error)

	// Redis LPUSH command.
	Lpush(key string, arg1 []byte) (status FutureBool, err Error)

	// Redis LSET command.
	Lset(key string, arg1 int64, arg2 []byte) (status FutureBool, err Error)

	// Redis LREM command.
	Lrem(key string, arg1 []byte, arg2 int64) (result FutureInt64, err Error)

	// Redis LLEN command.
	Llen(key string) (result FutureInt64, err Error)

	// Redis LRANGE command.
	Lrange(key string, arg1 int64, arg2 int64) (result FutureBytesArray, err Error)

	// Redis LTRIM command.
	Ltrim(key string, arg1 int64, arg2 int64) (status FutureBool, err Error)

	// Redis LINDEX command.
	Lindex(key string, arg1 int64) (result FutureBytes, err Error)

	// Redis LPOP command.
	Lpop(key string) (result FutureBytes, err Error)

	// Redis RPOP command.
	Rpop(key string) (result FutureBytes, err Error)

	// Redis RPOPLPUSH command.
	Rpoplpush(key string, arg1 string) (result FutureBytes, err Error)

	// Redis SADD command.
	Sadd(key string, arg1 []byte) (result FutureBool, err Error)

	// Redis SREM command.
	Srem(key string, arg1 []byte) (result FutureBool, err Error)

	// Redis SISMEMBER command.
	Sismember(key string, arg1 []byte) (result FutureBool, err Error)

	// Redis SMOVE command.
	Smove(key string, arg1 string, arg2 []byte) (result FutureBool, err Error)

	// Redis SCARD command.
	Scard(key string) (result FutureInt64, err Error)

	// Redis SINTER command.
	Sinter(key string, arg1 []string) (result FutureBytesArray, err Error)

	// Redis SINTERSTORE command.
	Sinterstore(key string, arg1 []string) (status FutureBool, err Error)

	// Redis SUNION command.
	Sunion(key string, arg1 []string) (result FutureBytesArray, err Error)

	// Redis SUNIONSTORE command.
	Sunionstore(key string, arg1 []string) (status FutureBool, err Error)

	// Redis SDIFF command.
	Sdiff(key string, arg1 []string) (result FutureBytesArray, err Error)

	// Redis SDIFFSTORE command.
	Sdiffstore(key string, arg1 []string) (status FutureBool, err Error)

	// Redis SMEMBERS command.
	Smembers(key string) (result FutureBytesArray, err Error)

	// Redis SRANDMEMBER command.
	Srandmember(key string) (result FutureBytes, err Error)

	// Redis ZADD command.
	Zadd(key string, arg1 float64, arg2 []byte) (result FutureBool, err Error)

	// Redis ZREM command.
	Zrem(key string, arg1 []byte) (result FutureBool, err Error)

	// Redis ZCARD command.
	Zcard(key string) (result FutureInt64, err Error)

	// Redis ZSCORE command.
	Zscore(key string, arg1 []byte) (result FutureFloat64, err Error)

	// Redis ZRANGE command.
	Zrange(key string, arg1 int64, arg2 int64) (result FutureBytesArray, err Error)

	// Redis ZREVRANGE command.
	Zrevrange(key string, arg1 int64, arg2 int64) (result FutureBytesArray, err Error)

	// Redis ZRANGEBYSCORE command.
	Zrangebyscore(key string, arg1 float64, arg2 float64) (result FutureBytesArray, err Error)

	// Redis FLUSHDB command.
	Flushdb() (status FutureBool, err Error)

	// Redis FLUSHALL command.
	Flushall() (status FutureBool, err Error)

	// Redis MOVE command.
	Move(key string, arg1 int64) (result FutureBool, err Error)

	// Redis BGSAVE command.
	Bgsave() (status FutureBool, err Error)

	// Redis LASTSAVE command.
	Lastsave() (result FutureInt64, err Error)

	// Redis PUBLISH command.
	// Publishes a message to the named channels.
	//
	// Returns the future for number of PubSub subscribers that received the message.
	// OR error if any.
	Publish(channel string, message []byte) (recieverCountFuture FutureInt64, err Error)
}

The asynchronous client interface provides asynchronous call semantics with future results supporting both blocking and try-and-timeout result accessors.

Each method provides a type-safe future result return value, in addition to any (system) errors encountered in queuing the request.

The returned value may be ignored by clients that are not interested in the future response (for example on SET("foo", data)). Alternatively, the caller may retain the future result referenced and perform blocking and/or timed wait gets on the expected response.

Get() or TryGet() on the future result will return any Redis errors that were sent by the server, or, Go-Redis (system) errors encountered in processing the response.

type AsyncConnection

type AsyncConnection interface {
	QueueRequest(cmd *Command, args [][]byte) (*PendingResponse, Error)
}

type Client

type Client interface {

	// Redis QUIT command.
	Quit() (err Error)

	// Redis GET command.
	Get(key string) (result []byte, err Error)

	// Redis TYPE command.
	Type(key string) (result KeyType, err Error)

	// Redis SET command.
	Set(key string, arg1 []byte) Error

	// Redis SAVE command.
	Save() Error

	// Redis KEYS command using "*" wildcard
	AllKeys() (result []string, err Error)

	// Redis KEYS command.
	Keys(key string) (result []string, err Error)

	// Redis EXISTS command.
	Exists(key string) (result bool, err Error)

	// Redis RENAME command.
	Rename(key, arg1 string) Error

	// Redis INFO command.
	Info() (result map[string]string, err Error)

	// Redis PING command.
	Ping() Error

	// Redis SETNX command.
	Setnx(key string, arg1 []byte) (result bool, err Error)

	// Redis GETSET command.
	Getset(key string, arg1 []byte) (result []byte, err Error)

	// Redis MGET command.
	Mget(key string, arg1 []string) (result [][]byte, err Error)

	// Redis INCR command.
	Incr(key string) (result int64, err Error)

	// Redis INCRBY command.
	Incrby(key string, arg1 int64) (result int64, err Error)

	// Redis DECR command.
	Decr(key string) (result int64, err Error)

	// Redis DECRBY command.
	Decrby(key string, arg1 int64) (result int64, err Error)

	// Redis DEL command.
	Del(key string) (result bool, err Error)

	// Redis RANDOMKEY command.
	Randomkey() (result string, err Error)

	// Redis RENAMENX command.
	Renamenx(key string, arg1 string) (result bool, err Error)

	// Redis DBSIZE command.
	Dbsize() (result int64, err Error)

	// Redis EXPIRE command.
	Expire(key string, arg1 int64) (result bool, err Error)

	// Redis TTL command.
	Ttl(key string) (result int64, err Error)

	// Redis RPUSH command.
	Rpush(key string, arg1 []byte) Error

	// Redis LPUSH command.
	Lpush(key string, arg1 []byte) Error

	// Redis LSET command.
	Lset(key string, arg1 int64, arg2 []byte) Error

	// Redis LREM command.
	Lrem(key string, arg1 []byte, arg2 int64) (result int64, err Error)

	// Redis LLEN command.
	Llen(key string) (result int64, err Error)

	// Redis LRANGE command.
	Lrange(key string, arg1 int64, arg2 int64) (result [][]byte, err Error)

	// Redis LTRIM command.
	Ltrim(key string, arg1 int64, arg2 int64) Error

	// Redis LINDEX command.
	Lindex(key string, arg1 int64) (result []byte, err Error)

	// Redis LPOP command.
	Lpop(key string) (result []byte, err Error)

	// Redis BLPOP command.
	Blpop(key string, timeout int) (result [][]byte, err Error)

	// Redis RPOP command.
	Rpop(key string) (result []byte, err Error)

	// Redis BRPOP command.
	Brpop(key string, timeout int) (result [][]byte, err Error)

	// Redis RPOPLPUSH command.
	Rpoplpush(key string, arg1 string) (result []byte, err Error)

	// Redis BRPOPLPUSH command.
	Brpoplpush(key string, arg1 string, timeout int) (result [][]byte, err Error)

	// Redis SADD command.
	Sadd(key string, arg1 []byte) (result bool, err Error)

	// Redis SREM command.
	Srem(key string, arg1 []byte) (result bool, err Error)

	// Redis SISMEMBER command.
	Sismember(key string, arg1 []byte) (result bool, err Error)

	// Redis SMOVE command.
	Smove(key string, arg1 string, arg2 []byte) (result bool, err Error)

	// Redis SCARD command.
	Scard(key string) (result int64, err Error)

	// Redis SINTER command.
	Sinter(key string, arg1 []string) (result [][]byte, err Error)

	// Redis SINTERSTORE command.
	Sinterstore(key string, arg1 []string) Error

	// Redis SUNION command.
	Sunion(key string, arg1 []string) (result [][]byte, err Error)

	// Redis SUNIONSTORE command.
	Sunionstore(key string, arg1 []string) Error

	// Redis SDIFF command.
	Sdiff(key string, arg1 []string) (result [][]byte, err Error)

	// Redis SDIFFSTORE command.
	Sdiffstore(key string, arg1 []string) Error

	// Redis SMEMBERS command.
	Smembers(key string) (result [][]byte, err Error)

	// Redis SRANDMEMBER command.
	Srandmember(key string) (result []byte, err Error)

	// Redis ZADD command.
	Zadd(key string, arg1 float64, arg2 []byte) (result bool, err Error)

	// Redis ZREM command.
	Zrem(key string, arg1 []byte) (result bool, err Error)

	// Redis ZCARD command.
	Zcard(key string) (result int64, err Error)

	// Redis ZSCORE command.
	Zscore(key string, arg1 []byte) (result float64, err Error)

	// Redis ZRANGE command.
	Zrange(key string, arg1 int64, arg2 int64) (result [][]byte, err Error)

	// Redis ZREVRANGE command.
	Zrevrange(key string, arg1 int64, arg2 int64) (result [][]byte, err Error)

	// Redis ZRANGEBYSCORE command.
	Zrangebyscore(key string, arg1 float64, arg2 float64) (result [][]byte, err Error)

	// Redis HGET command.
	Hget(key string, hashkey string) (result []byte, err Error)

	// Redis HSET command.
	Hset(key string, hashkey string, arg1 []byte) Error

	// Redis HGETALL command.
	Hgetall(key string) (result [][]byte, err Error)

	// Redis FLUSHDB command.
	Flushdb() Error

	// Redis FLUSHALL command.
	Flushall() Error

	// Redis MOVE command.
	Move(key string, arg1 int64) (result bool, err Error)

	// Redis BGSAVE command.
	Bgsave() Error

	// Redis LASTSAVE command.
	Lastsave() (result int64, err Error)

	// Redis PUBLISH command.
	// Publishes a message to the named channels.  This is a blocking call.
	//
	// Returns the number of PubSub subscribers that received the message.
	// OR error if any.
	Publish(channel string, message []byte) (recieverCout int64, err Error)
}

The synchronous call semantics Client interface.

Method names map one to one to the Redis command set. All methods may return an redis.Error, which is either a system error (runtime issue or bug) or Redis error (i.e. user error) See Error in this package for details of its interface.

The synchronous client interface provides blocking call semantics supported by a distinct request/reply sequence at the connector level.

Method names map one to one to the Redis command set.

All methods may return an redis.Error, which is either a Redis error (from the server), or a system error indicating a runtime issue (or bug). See Error in this package for details of its interface.

type Command

type Command struct {
	Code     string
	ReqType  RequestType
	RespType ResponseType
}

Describes a given Redis command

var (
	AUTH          Command = Command{"AUTH", KEY, STATUS}
	PING          Command = Command{"PING", NO_ARG, STATUS}
	QUIT          Command = Command{"QUIT", NO_ARG, VIRTUAL}
	SET           Command = Command{"SET", KEY_VALUE, STATUS}
	GET           Command = Command{"GET", KEY, BULK}
	GETSET        Command = Command{"GETSET", KEY_VALUE, BULK}
	MGET          Command = Command{"MGET", MULTI_KEY, MULTI_BULK}
	SETNX         Command = Command{"SETNX", KEY_VALUE, BOOLEAN}
	INCR          Command = Command{"INCR", KEY, NUMBER}
	INCRBY        Command = Command{"INCRBY", KEY_NUM, NUMBER}
	DECR          Command = Command{"DECR", KEY, NUMBER}
	DECRBY        Command = Command{"DECRBY", KEY_NUM, NUMBER}
	EXISTS        Command = Command{"EXISTS", KEY, BOOLEAN}
	DEL           Command = Command{"DEL", KEY, BOOLEAN}
	TYPE          Command = Command{"TYPE", KEY, STRING}
	KEYS          Command = Command{"KEYS", KEY, MULTI_BULK}
	RANDOMKEY     Command = Command{"RANDOMKEY", NO_ARG, BULK}
	RENAME        Command = Command{"RENAME", KEY_KEY, STATUS}
	RENAMENX      Command = Command{"RENAMENX", KEY_KEY, BOOLEAN}
	DBSIZE        Command = Command{"DBSIZE", NO_ARG, NUMBER}
	EXPIRE        Command = Command{"EXPIRE", KEY_NUM, BOOLEAN}
	TTL           Command = Command{"TTL", KEY, NUMBER}
	RPUSH         Command = Command{"RPUSH", KEY_VALUE, STATUS}
	LPUSH         Command = Command{"LPUSH", KEY_VALUE, STATUS}
	LLEN          Command = Command{"LLEN", KEY, NUMBER}
	LRANGE        Command = Command{"LRANGE", KEY_NUM_NUM, MULTI_BULK}
	LTRIM         Command = Command{"LTRIM", KEY_NUM_NUM, STATUS}
	LINDEX        Command = Command{"LINDEX", KEY_NUM, BULK}
	LSET          Command = Command{"LSET", KEY_IDX_VALUE, STATUS}
	LREM          Command = Command{"LREM", KEY_CNT_VALUE, NUMBER}
	LPOP          Command = Command{"LPOP", KEY, BULK}
	BLPOP         Command = Command{"BLPOP", KEY, MULTI_BULK}
	RPOP          Command = Command{"RPOP", KEY, BULK}
	BRPOP         Command = Command{"BRPOP", KEY, MULTI_BULK}
	RPOPLPUSH     Command = Command{"RPOPLPUSH", KEY_VALUE, BULK}
	BRPOPLPUSH    Command = Command{"BRPOPLPUSH", KEY_VALUE, MULTI_BULK}
	SADD          Command = Command{"SADD", KEY_VALUE, BOOLEAN}
	SREM          Command = Command{"SREM", KEY_VALUE, BOOLEAN}
	SCARD         Command = Command{"SCARD", KEY, NUMBER}
	SISMEMBER     Command = Command{"SISMEMBER", KEY_VALUE, BOOLEAN}
	SINTER        Command = Command{"SINTER", MULTI_KEY, MULTI_BULK}
	SINTERSTORE   Command = Command{"SINTERSTORE", MULTI_KEY, STATUS}
	SUNION        Command = Command{"SUNION", MULTI_KEY, MULTI_BULK}
	SUNIONSTORE   Command = Command{"SUNIONSTORE", MULTI_KEY, STATUS}
	SDIFF         Command = Command{"SDIFF", MULTI_KEY, MULTI_BULK}
	SDIFFSTORE    Command = Command{"SDIFFSTORE", MULTI_KEY, STATUS}
	SMEMBERS      Command = Command{"SMEMBERS", KEY, MULTI_BULK}
	SMOVE         Command = Command{"SMOVE", KEY_KEY_VALUE, BOOLEAN}
	SRANDMEMBER   Command = Command{"SRANDMEMBER", KEY, BULK}
	HGET          Command = Command{"HGET", KEY_KEY, BULK}
	HSET          Command = Command{"HSET", KEY_KEY_VALUE, STATUS}
	HGETALL       Command = Command{"HGETALL", KEY, MULTI_BULK}
	ZADD          Command = Command{"ZADD", KEY_IDX_VALUE, BOOLEAN}
	ZREM          Command = Command{"ZREM", KEY_VALUE, BOOLEAN}
	ZCARD         Command = Command{"ZCARD", KEY, NUMBER}
	ZSCORE        Command = Command{"ZSCORE", KEY_VALUE, BULK}
	ZRANGE        Command = Command{"ZRANGE", KEY_NUM_NUM, MULTI_BULK}
	ZREVRANGE     Command = Command{"ZREVRANGE", KEY_NUM_NUM, MULTI_BULK}
	ZRANGEBYSCORE Command = Command{"ZRANGEBYSCORE", KEY_NUM_NUM, MULTI_BULK}
	SELECT        Command = Command{"SELECT", KEY, STATUS}
	FLUSHDB       Command = Command{"FLUSHDB", NO_ARG, STATUS}
	FLUSHALL      Command = Command{"FLUSHALL", NO_ARG, STATUS}
	MOVE          Command = Command{"MOVE", KEY_NUM, BOOLEAN}
	SORT          Command = Command{"SORT", KEY_SPEC, MULTI_BULK}
	SAVE          Command = Command{"SAVE", NO_ARG, STATUS}
	BGSAVE        Command = Command{"BGSAVE", NO_ARG, STATUS}
	LASTSAVE      Command = Command{"LASTSAVE", NO_ARG, NUMBER}
	SHUTDOWN      Command = Command{"SHUTDOWN", NO_ARG, VIRTUAL}
	INFO          Command = Command{"INFO", NO_ARG, BULK}
	MONITOR       Command = Command{"MONITOR", NO_ARG, VIRTUAL}
	// TODO	SORT		(RequestType.MULTI_KEY,		ResponseType.MULTI_BULK),
	PUBLISH      Command = Command{"PUBLISH", KEY_VALUE, NUMBER}
	SUBSCRIBE    Command = Command{"SUBSCRIBE", MULTI_KEY, MULTI_BULK}
	UNSUBSCRIBE  Command = Command{"UNSUBSCRIBE", MULTI_KEY, MULTI_BULK}
	PSUBSCRIBE   Command = Command{"PSUBSCRIBE", MULTI_KEY, MULTI_BULK}
	PUNSUBSCRIBE Command = Command{"PUNSUBSCRIBE", MULTI_KEY, MULTI_BULK}
)

The supported Command set, with one to one mapping to eponymous Redis command.

type ConnectionSpec

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

Defines the set of parameters that are used by the client connections

func DefaultSpec

func DefaultSpec() *ConnectionSpec

Creates a ConnectionSpec using default settings. using the DefaultXXX consts of redis package.

func (*ConnectionSpec) Db

func (spec *ConnectionSpec) Db(db int) *ConnectionSpec

Sets the db for connection spec and returns the reference Note that you should not this after you have already connected.

func (*ConnectionSpec) Heartbeat

func (spec *ConnectionSpec) Heartbeat(period time.Duration) *ConnectionSpec

return the address as string.

func (*ConnectionSpec) Host

func (spec *ConnectionSpec) Host(host string) *ConnectionSpec

Sets the host for connection spec and returns the reference Note that you should not this after you have already connected.

func (*ConnectionSpec) Password

func (spec *ConnectionSpec) Password(password string) *ConnectionSpec

Sets the password for connection spec and returns the reference Note that you should not this after you have already connected.

func (*ConnectionSpec) Port

func (spec *ConnectionSpec) Port(port int) *ConnectionSpec

Sets the port for connection spec and returns the reference Note that you should not this after you have already connected.

func (*ConnectionSpec) Protocol

func (spec *ConnectionSpec) Protocol(protocol Protocol) *ConnectionSpec

return the address as string.

type Error

type Error interface {
	error
	// if true Error is a RedisError
	IsRedisError() bool
}

Go-Redis API level error type

func Btof64

func Btof64(buff []byte) (num float64, e Error)

type FutureBool

type FutureBool interface {
	Get() (val bool, error Error)
	TryGet(timeoutnano time.Duration) (value bool, error Error, timedout bool)
	// contains filtered or unexported methods
}

FutureBool

type FutureBytes

type FutureBytes interface {
	Get() (vale []byte, error Error)
	TryGet(timeoutnano time.Duration) (value []byte, error Error, timedout bool)
	// contains filtered or unexported methods
}

FutureBytes (for []byte)

type FutureBytesArray

type FutureBytesArray interface {
	Get() (vale [][]byte, error Error)
	TryGet(timeoutnano time.Duration) (value [][]byte, error Error, timedout bool)
	// contains filtered or unexported methods
}

FutureBytesArray (for [][]byte)

type FutureFloat64

type FutureFloat64 interface {
	Get() (float64, Error)
	TryGet(timeoutnano time.Duration) (v float64, error Error, timedout bool)
}

FutureFloat64

type FutureInfo

type FutureInfo interface {
	Get() (map[string]string, Error)
	TryGet(timeoutnano time.Duration) (keys map[string]string, error Error, timedout bool)
}

FutureInfo

type FutureInt64

type FutureInt64 interface {
	Get() (int64, Error)
	TryGet(timeoutnano time.Duration) (value int64, error Error, timedout bool)
	// contains filtered or unexported methods
}

FutureInt64

type FutureKeyType

type FutureKeyType interface {
	Get() (KeyType, Error)
	TryGet(timeoutnano time.Duration) (keys KeyType, error Error, timedout bool)
}

FutureKeyType

type FutureKeys

type FutureKeys interface {
	Get() ([]string, Error)
	TryGet(timeoutnano time.Duration) (keys []string, error Error, timedout bool)
}

FutureKeys

type FutureResult

type FutureResult interface {
	// contains filtered or unexported methods
}

FutureResult

A generic future. All type-safe Futures support this interface

type FutureString

type FutureString interface {
	Get() (string, Error)
	TryGet(timeoutnano time.Duration) (value string, error Error, timedout bool)
	// contains filtered or unexported methods
}

FutureString

type KeyType

type KeyType byte

Redis 'KeyType'

const (
	RT_NONE KeyType = iota
	RT_STRING
	RT_SET
	RT_LIST
	RT_ZSET
)

Known Redis key types

func GetKeyType

func GetKeyType(typename string) (keytype KeyType)

Returns KeyType by name

type Message

type Message struct {
	Type            PubSubMType
	Topic           string
	Body            []byte
	SubscriptionCnt int
}

Conforms to the payload as received from wire. If Type is MESSAGE, then Body will contain a message, and SubscriptionCnt will be -1. otherwise, it is expected that SubscriptionCnt will contain subscription-info, e.g. number of subscribed channels, and data will be nil.

func (Message) String

func (m Message) String() string

type MethodSpec

type MethodSpec struct {
	NoPanics         bool
	NoRedisErr       bool
	NoNilResultValue bool
	NoNilFuture      bool
}

For use by the generated tests.

func GetMethodSpec

func GetMethodSpec(client, method string) (spec *MethodSpec)

For use by the generated tests.

Get MethodSpec for the given client type (name) and Method.

type PendingResponse

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

Handle to a future response

type Protocol

type Protocol int
const (
	REDIS_DB Protocol = iota
	REDIS_PUBSUB
)

func (Protocol) String

func (p Protocol) String() string

type PubSubChannel

type PubSubChannel <-chan []byte

PubSubChannels are used by clients to forward received PubSub messages from Redis See PubSubClient interface for details.

type PubSubClient

type PubSubClient interface {

	// returns the incoming messages channel for this client, or nil
	// if no such subscription is active.
	// In event of Unsubscribing from a Redis channel, the
	// client will close this channel.
	Messages(topic string) PubSubChannel

	// return the subscribed channel ids, whether specificly named, or
	// pattern based.
	Subscriptions() []string

	// Redis PSUBSCRIBE command.
	// Subscribes to one or more pubsub channels.
	// This is a blocking call.
	// Channel names can be explicit or pattern based (ending in '*')
	//
	// Returns the number of currently subscribed channels OR error (if any)
	//	Subscribe(channel string, otherChannels ...string) (messages PubSubChannel, subscriptionCount int, err Error)
	Subscribe(topic string, otherTopics ...string) (err Error)

	// Redis PUNSUBSCRIBE command.
	// unsubscribe from 1 or more pubsub channels.  If arg is nil,
	// client unsubcribes from ALL subscribed channels.
	// This is a blocking call.
	// Channel names can be explicit or pattern based (ending in '*')
	//
	// Returns the number of currently subscribed channels OR error (if any)
	Unsubscribe(channels ...string) (err Error)

	// Quit closes the client and client reference can be disposed.
	// This is a blocking call.
	// Returns error, if any, e.g. network issues.
	Quit() Error
}

PubSub Client

Strictly speaking, this client can only subscribe, receive messages, and unsubscribe. Publishing to Redis PubSub channels is done via the standard clients (either sync or async); see the Publish() method on Client and AsyncClient.

Once created, the PubSub client has a message channel (of type <-chan []byte) that the end-user can select, dequeue, etc.

This client (very) slightly modifies the native pubsub client's semantics in that it does NOT post the 'subscribe' or 'unsubscribe' ACKs of Redis server on the exposed chan. These ACKs are effectively captured and returned via the returned results of the PubSubClient's Subscribe() and Unsubscribe() methods, respectively.

The subscribe and unsubscribe methods are both blocking (synchronous). The messages published via the incoming chan are naturally asynchronous.

Given the fact that Redis PSUBSCRIBE to channel names that do NOT end in * is identical to SUBSCRIBE to the same, PubSubClient only exposes Subscribe and Unsubscribe methods and supporting implementation are expected to always use the Redis PSUBSCRIBE and PUNSUBSCRIBE commands. For example, if one issues PSUBSCRIBE foo/* (via telnet) to Redis, and then UNSUBSCRIBE or PUNSUBSCRIBE foo/bar, messages published to foo/bar will still be received in the (telnet) client. So given that Redis does NOT filter subscriptions and it merely has a 1-1 mapping to subscribed and unsubscribed patterns, PSUBSCRIBE foo is equivalent to SUBSCRIBE foo. These facts inform the design decision to keep the API of PubSubClient simple and not expose explicit pattern or explicit (un)subscription.

Also note that (per Redis semantics) ALL subscribed channels will publish to the single chan exposed by this client. For practical applications, you will minimally want to use one PubSubClient per PubSub channel priority category. For example, if your system has general priority application level and high priority critical system level PubSub channels, you should at least create 2 clients, one per priority category.

Like all Go-Redis clients, you can (and should) Quit() once you are done with the client.

type PubSubConnection

type PubSubConnection interface {
	Subscriptions() map[string]*Subscription
	ServiceRequest(cmd *Command, args [][]byte) (pending map[string]FutureBool, err Error)
}

type PubSubMType

type PubSubMType int
const (
	SUBSCRIBE_ACK PubSubMType = iota
	UNSUBSCRIBE_ACK
	MESSAGE
)

func (PubSubMType) String

func (t PubSubMType) String() string

type RedisError

type RedisError interface {
	Message() string
}

ERR Errors returned by the Redis server e.g. for bad AUTH password.

type RequestType

type RequestType int

Request type defines the characteristic pattern of a cmd request

const (
	NO_ARG RequestType
	KEY
	KEY_KEY
	KEY_NUM
	KEY_SPEC
	KEY_NUM_NUM
	KEY_VALUE
	KEY_IDX_VALUE
	KEY_KEY_VALUE
	KEY_CNT_VALUE
	MULTI_KEY
)

type Response

type Response interface {
	IsError() bool
	GetMessage() string
	GetBooleanValue() bool
	GetNumberValue() int64
	GetStringValue() string
	GetBulkData() []byte
	GetMultiBulkData() [][]byte
}

type ResponseType

type ResponseType int
const (
	VIRTUAL ResponseType = iota
	BOOLEAN
	NUMBER
	STRING // REVU - rename to TYPE
	STATUS
	BULK
	MULTI_BULK
)

type Status

type Status bool

Not yet used -- TODO: decide if returning status (say for Set) for non error cases really buys us anything beyond (useless) consistency.

type Subscription

type Subscription struct {

	//	closed FutureBool // REVU - for unsubscribe - necessary?
	//	activated chan bool
	Channel  chan []byte
	IsActive bool // REVU - not necessary
	// contains filtered or unexported fields
}

REVU - why is this exported?

type SyncConnection

type SyncConnection interface {
	ServiceRequest(cmd *Command, args [][]byte) (Response, Error)
}

type SystemError

type SystemError interface {
	Cause() error
}

A system level error, ranging from connectivity issues to detected bugs. Basically anything other than an Redis Server ERR.

System errors can (and typically do) have an underlying Go std. lib or 3rd party lib error cause, such as net.Error, etc.

Directories

Path Synopsis
gen

Jump to

Keyboard shortcuts

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