redis

package
v0.0.0-...-a1c9142 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package redis provides a very powerful as well as convenient client for the Redis database.

After opening the database with Open() a pooled connection can be retrieved using db.Connection(). It has be returnded to the pool with with conn.Return(), optimally done using a defer after retrieving. The connection provides a conn.Do() method to execute any command. It returns a result set with helpers to access the returned values and convert them into Go types. For typical returnings there are conn.DoXxx() methods.

All conn.Do() methods work atomically and are able to run all commands except subscriptions. Also the execution of scripts is possible that way. Additionally the execution of commands can be pipelined. The pipeline can be retrieved db.Pipeline(). It provides a ppl.Do() method for the execution of individual commands. Their results can be collected with ppl.Collect(), which returns a sice of result sets containing the responses of the commands.

Due to the nature of the subscription the client provides an own type which can be retrieved with db.Subscription(). Here channels, in the sense of the Redis Pub/Sub, can be subscribed or unsubscribed. Published values can be retrieved with sub.Pop(). If the subscription is not needed anymore it can be closed using sub.Close().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

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

Connection manages one connection to a Redis database.

func (*Connection) Do

func (conn *Connection) Do(cmd string, args ...interface{}) (*ResultSet, error)

Do executes one Redis command and returns the result as result set.

func (*Connection) DoBool

func (conn *Connection) DoBool(cmd string, args ...interface{}) (bool, error)

DoBool executes one Redis command and interpretes the result as bool value.

func (*Connection) DoHash

func (conn *Connection) DoHash(cmd string, args ...interface{}) (Hash, error)

DoHash executes on Redis command and interpretes the result as a hash.

func (*Connection) DoInt

func (conn *Connection) DoInt(cmd string, args ...interface{}) (int, error)

DoInt executes one Redis command and interpretes the result as int value.

func (*Connection) DoKeyValues

func (conn *Connection) DoKeyValues(cmd string, args ...interface{}) (KeyValues, error)

DoKeyValues executes on Redis command and interpretes the result as a list of keys and values.

func (*Connection) DoOK

func (conn *Connection) DoOK(cmd string, args ...interface{}) (bool, error)

DoOK executes one Redis command and checks if it returns the OK string.

func (*Connection) DoScan

func (conn *Connection) DoScan(cmd string, args ...interface{}) (int, *ResultSet, error)

DoScan executes one Redis command which should be one of the scan commands. It returns the cursor and the result set containing the key, values or scored values depending on the scan command.

func (*Connection) DoScoredValues

func (conn *Connection) DoScoredValues(cmd string, args ...interface{}) (ScoredValues, error)

DoScoredValues executes on Redis command and interpretes the result as scored values.

func (*Connection) DoString

func (conn *Connection) DoString(cmd string, args ...interface{}) (string, error)

DoString executes one Redis command and interpretes the result as string value.

func (*Connection) DoStrings

func (conn *Connection) DoStrings(cmd string, args ...interface{}) ([]string, error)

DoStrings executes one Redis command and interpretes the result as a slice of strings.

func (*Connection) DoValue

func (conn *Connection) DoValue(cmd string, args ...interface{}) (Value, error)

DoValue executes one Redis command and returns a single value.

func (*Connection) Return

func (conn *Connection) Return() error

Return passes the connection back into the database pool.

type Database

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

Database provides access to a Redis database.

func Open

func Open(options ...Option) (*Database, error)

Open opens the connection to a Redis database based on the passed options.

func (*Database) Close

func (db *Database) Close() error

Close closes the database client.

func (*Database) Connection

func (db *Database) Connection() (*Connection, error)

Connection returns one of the pooled connections to the Redis server. It has to be returned with conn.Return() after usage.

func (*Database) Options

func (db *Database) Options() Options

Options returns the configuration of the database.

func (*Database) Pipeline

func (db *Database) Pipeline() (*Pipeline, error)

Pipeline returns one of the pooled connections to the Redis server running in pipeline mode. Calling ppl.Collect() collects all results and returns the connection.

func (*Database) String

func (db *Database) String() string

String implements the Stringer interface and returns address plus index.

func (*Database) Subscription

func (db *Database) Subscription() (*Subscription, error)

Subscription returns a subscription with a connection to the Redis server. It has to be closed with sub.Close() after usage.

type Hash

type Hash map[string]Value

Hash maps multiple fields of a hash to the according result values.

func NewFilledHash

func NewFilledHash(kvs map[string]interface{}) Hash

NewFilledHash creates a hash with the passed keys and values.

func NewHash

func NewHash() Hash

NewHash creates a new empty hash.

func (Hash) Bool

func (h Hash) Bool(key string) (bool, error)

Bool returns the value of a key as bool.

func (Hash) Bytes

func (h Hash) Bytes(key string) []byte

Bytes returns the value of a key as byte slice.

func (Hash) Float64

func (h Hash) Float64(key string) (float64, error)

Float64 returns the value of a key as float64.

func (Hash) Int

func (h Hash) Int(key string) (int, error)

Int returns the value of a key as int.

func (Hash) Int64

func (h Hash) Int64(key string) (int64, error)

Int64 returns the value of a key as int64.

func (Hash) Len

func (h Hash) Len() int

Len returns the number of elements in the hash.

func (Hash) Set

func (h Hash) Set(key string, value interface{}) Hash

Set sets a key to the given value.

func (Hash) String

func (h Hash) String(key string) (string, error)

String returns the value of a key as string.

func (Hash) StringMap

func (h Hash) StringMap(key string) map[string]string

StringMap returns the value of a key as string map.

func (Hash) StringSlice

func (h Hash) StringSlice(key string) []string

StringSlice returns the value of a key as string slice.

func (Hash) Uint64

func (h Hash) Uint64(key string) (uint64, error)

Uint64 returns the value of a key as uint64.

type Hashable

type Hashable interface {
	Len() int
	GetHash() Hash
	SetHash(h Hash)
}

Hashable represents types for Redis hashes.

type KeyValue

type KeyValue struct {
	Key   string
	Value Value
}

KeyValue combines a key and a value

func (KeyValue) String

func (kv KeyValue) String() string

String returs the key/value pair as string.

type KeyValues

type KeyValues []KeyValue

KeyValues is a set of KeyValues.

func (KeyValues) Len

func (kvs KeyValues) Len() int

Len returns the number of keys and values in the set.

func (KeyValues) String

func (kvs KeyValues) String() string

String returs the key/value pairs as string.

type Option

type Option func(d *Database) error

Option defines a function setting an option.

func Index

func Index(index int, password string) Option

Index selects the database and sets the authentication. The default database is the 0, the default password is empty.

func PoolSize

func PoolSize(poolsize int) Option

PoolSize sets the pool size of the database. The default is 10.

func TCPConnection

func TCPConnection(address string, timeout time.Duration) Option

TCPConnection sets the connection to use TCP/IP. The default address is "127.0.0.1:6379". The default timeout to connect are 30 seconds.

func UnixConnection

func UnixConnection(socket string, timeout time.Duration) Option

UnixConnection sets the connection to use a Unix socket. The default is "/tmp/redis.sock". The default timeout to connect are 30 seconds.

type Options

type Options struct {
	Address  string
	Network  string
	Timeout  time.Duration
	Index    int
	Password string
	PoolSize int
	Logging  bool
}

Options is returned when calling Options() on Database to provide information about the database configuration.

type Pipeline

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

Pipeline manages a Redis connection executing pipelined commands.

func (*Pipeline) Collect

func (ppl *Pipeline) Collect() ([]*ResultSet, error)

Collect collects all the result sets of the commands and returns the connection back into the pool.

func (*Pipeline) Do

func (ppl *Pipeline) Do(cmd string, args ...interface{}) error

Do executes one Redis command and returns the result as result set.

type PublishedValue

type PublishedValue struct {
	Kind    string
	Channel string
	Count   int
	Value   Value
}

PublishedValue contains a published value and its channel channel pattern.

type ResultSet

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

ResultSet contains a number of values or nested result sets.

func (*ResultSet) BoolAt

func (rs *ResultSet) BoolAt(index int) (bool, error)

BoolAt returns the value at index as bool. This is a convenience method as the bool is needed very often.

func (*ResultSet) Hash

func (rs *ResultSet) Hash() (Hash, error)

Hash returns the values of the result set as hash.

func (*ResultSet) IntAt

func (rs *ResultSet) IntAt(index int) (int, error)

IntAt returns the value at index as int. This is a convenience method as the integer is needed very often.

func (*ResultSet) KeyValues

func (rs *ResultSet) KeyValues() (KeyValues, error)

KeyValues returns the alternating values as key/value slice.

func (*ResultSet) Len

func (rs *ResultSet) Len() int

Len returns the number of items in the result set.

func (*ResultSet) ResultSetAt

func (rs *ResultSet) ResultSetAt(index int) (*ResultSet, error)

ResultSetAt returns the nested result set at index.

func (*ResultSet) Scanned

func (rs *ResultSet) Scanned() (int, *ResultSet, error)

Scanned returns the cursor and the keys or values of a scan operation.

func (*ResultSet) ScoredValues

func (rs *ResultSet) ScoredValues(withscores bool) (ScoredValues, error)

ScoredValues returns the alternating values as scored values slice. If withscores is false the result set contains no scores and so they are set to 0.0 in the returned scored values.

func (*ResultSet) String

func (rs *ResultSet) String() string

String returns the result set in a human readable form.

func (*ResultSet) StringAt

func (rs *ResultSet) StringAt(index int) (string, error)

StringAt returns the value at index as string. This is a convenience method as the string is needed very often.

func (*ResultSet) Strings

func (rs *ResultSet) Strings() []string

Strings returns all values/arrays of the array as a slice of strings.

func (*ResultSet) ValueAt

func (rs *ResultSet) ValueAt(index int) (Value, error)

ValueAt returns the value at index.

func (*ResultSet) Values

func (rs *ResultSet) Values() Values

Values returnes a flattened list of all values.

type ScoredValue

type ScoredValue struct {
	Score float64
	Value Value
}

ScoredValue helps to add a set member together with its score.

func (ScoredValue) String

func (sv ScoredValue) String() string

String returs the scored value as string.

type ScoredValues

type ScoredValues []ScoredValue

ScoredValues is a set of ScoreValues.

func (ScoredValues) Len

func (svs ScoredValues) Len() int

Len returns the number of scored values in the set.

func (ScoredValues) String

func (svs ScoredValues) String() string

String returs the scored values as string.

type Subscription

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

Subscription manages a subscription to Redis channels and allows to subscribe and unsubscribe from channels.

func (*Subscription) Close

func (sub *Subscription) Close() error

Close ends the subscription.

func (*Subscription) Pop

func (sub *Subscription) Pop() (*PublishedValue, error)

Pop waits for a published value and returns it.

func (*Subscription) Subscribe

func (sub *Subscription) Subscribe(channels ...string) error

Subscribe adds one or more channels to the subscription.

func (*Subscription) Unsubscribe

func (sub *Subscription) Unsubscribe(channels ...string) error

Unsubscribe removes one or more channels from the subscription.

type Value

type Value []byte

Value is simply a byte slice.

func NewValue

func NewValue(value interface{}) Value

NewValue creates a value out of the passed data.

func (Value) Bool

func (v Value) Bool() (bool, error)

Bool return the value as bool.

func (Value) Bytes

func (v Value) Bytes() []byte

Bytes returns the value as byte slice.

func (Value) Float64

func (v Value) Float64() (float64, error)

Float64 returns the value as float64.

func (Value) Int

func (v Value) Int() (int, error)

Int returns the value as int.

func (Value) Int64

func (v Value) Int64() (int64, error)

Int64 returns the value as int64.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil returns true if the value is the Redis nil value.

func (Value) IsOK

func (v Value) IsOK() bool

IsOK returns true if the value is the Redis OK value.

func (Value) String

func (v Value) String() string

String returns the value as string (alternative to type conversion).

func (Value) StringMap

func (v Value) StringMap() map[string]string

StringMap returns the value as a map of strings when separated by CRLF and colons between key and value.

func (Value) StringSlice

func (v Value) StringSlice() []string

StringSlice returns the value as slice of strings when separated by CRLF.

func (Value) Uint64

func (v Value) Uint64() (uint64, error)

Uint64 returns the value as uint64.

func (Value) Unpack

func (v Value) Unpack() Value

Unpack removes the braces of a list value.

type Values

type Values []Value

Values is a set of values.

func (Values) Len

func (vs Values) Len() int

Len returns the number of values.

func (Values) Strings

func (vs Values) Strings() []string

Strings returns all values as strings.

Jump to

Keyboard shortcuts

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