redis

package
v0.0.0-...-109ffb8 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2012 License: BSD-3-Clause Imports: 9 Imported by: 0

README

godis

godis - a Redis client for Go. It supports all commands and features such as transactions and pubsub.

  1. Package docs
  2. Source code

Install godis

godis is available at github.com. Get it by running.

$ go get github.com/simonz05/godis/redis

Importing godis to your code can be done with import "github.com/simonz05/godis". Thats it!

Use godis

Checking out the code include a few examples. Here is the code for the example/strings.go.

package main

import (
    "fmt"
    "github.com/simonz05/godis/redis"
    "os"
)

func main() {
    // new client on default port 6379, select db 0 and use no password
    c := redis.New("", 0, "")

    // set the key "foo" to "Hello Redis"
    if err := c.Set("foo", "Hello Redis"); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }

    // retrieve the value of "foo". Returns an Elem obj
    elem, _ := c.Get("foo")

    // convert the obj to a string and print it 
    fmt.Println("foo:", elem.String())
}

Build the examples.

$ make 

To run it we type.

$ ./string
foo: Hello Redis

In case your redis server isn't running the output looks like this.

$ ./string 
Connection error 127.0.0.1:6379

Transactions

PipeClient supports MULTI/EXEC operations as well as buffered command execution.

Create a PipeClient. Subsequent commands will be buffered. PipeClient acts as a regular client, but implements a few extra commands; Multi, Exec, Unwatch, Watch.

c := godis.NewPipeClient("tcp:127.0.0.1:6379", 0, "")

Calling Multi() wraps subsequent commands inside MULTI .. EXEC.

c.Multi()

Commands are still issued as usual, but will return an empty *Reply.

c.Set("foo", "bar")
c.Get("foo")

To execute the buffered commands we call c.Exec(). Exec handles both MULTI/EXEC pipelines and simply buffered piplines. It returns a slice of all the *Reply objects for every command we executed.

replies := c.Exec()

See example/transaction.go for a full example.

TODO

  • Add tests server commands.
  • Allow one or more keys to be manipulated with SADD, ZADD, SET, HDEL, LPUSH, RPUSH, SREM, ZREM and ZADD.

Acknowledgment

The work on this client started as I was hacking around on Michael Hoisie's original redis client for Go. Also the recent work done by Frank Müller on his client gave me some pointers how to better handle return values.

Documentation

Overview

package redis implements a client for Redis with support for all commands and features such as transactions and pubsub.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Max connection pool size
	MaxClientConn = 2
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Rw ReaderWriter
}

func New

func New(netaddr string, db int, password string) *Client

Returns a new Client given a net address, db and password. nettaddr should be formatted using "net:addr", where ":" is acting as a separator. E.g. "unix:/path/to/redis.sock", "tcp:127.0.0.1:12345". Use an empty string for redis defaults.

func (*Client) Append

func (c *Client) Append(key string, value interface{}) (int64, error)

Append a value to a key

func (*Client) Bgrewriteaof

func (c *Client) Bgrewriteaof() error

Asynchronously rewrite the append-only file

func (*Client) Bgsave

func (c *Client) Bgsave() error

Asynchronously save the dataset to disk

func (*Client) Blpop

func (c *Client) Blpop(keys []string, timeout int64) (*Reply, error)

Remove and get the first element in a list, or block until one is available If timeout expires an error is returned errors.New("timeout expired")

func (*Client) Brpop

func (c *Client) Brpop(keys []string, timeout int64) (*Reply, error)

Remove and get the last element in a list, or block until one is available If timeout expires an error is returned errors.New("timeout expired")

func (*Client) Brpoplpush

func (c *Client) Brpoplpush(source string, destination string, timeout int64) (Elem, error)

Pop a value from a list, push it to another list and return it; or block until one is available If timeout expires an error is returned errors.New("timeout expired")

func (*Client) ConfigGet

func (c *Client) ConfigGet(parameter string) (*Reply, error)

Get the value of a configuration parameter

func (*Client) ConfigResetstat

func (c *Client) ConfigResetstat() error

Reset the stats returned by INFO

func (*Client) ConfigSet

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

Set a configuration parameter to the given value

func (*Client) Dbsize

func (c *Client) Dbsize() (int64, error)

Return the number of keys in the selected database

func (*Client) DebugObject

func (c *Client) DebugObject(key string) (Elem, error)

Get debugging information about a key

func (*Client) DebugSegfault

func (c *Client) DebugSegfault() error

Make the server crash

func (*Client) Decr

func (c *Client) Decr(key string) (int64, error)

Decrement the integer value of a key by one

func (*Client) Decrby

func (c *Client) Decrby(key string, decrement int64) (int64, error)

Decrement the integer value of a key by the given number

func (*Client) Del

func (c *Client) Del(keys ...string) (int64, error)

Delete a key

func (*Client) Echo

func (c *Client) Echo(message interface{}) (Elem, error)

Echo the given string

func (*Client) Exists

func (c *Client) Exists(key string) (bool, error)

Determine if a key exists

func (*Client) Expire

func (c *Client) Expire(key string, seconds int64) (bool, error)

Set a key's time to live in seconds

func (*Client) Expireat

func (c *Client) Expireat(key string, timestamp int64) (bool, error)

Set the expiration for a key as a UNIX timestamp

func (*Client) Flushall

func (c *Client) Flushall() error

Remove all keys from all databases

func (*Client) Flushdb

func (c *Client) Flushdb() error

Remove all keys from the current database

func (*Client) Get

func (c *Client) Get(key string) (Elem, error)

Get the value of a key

func (*Client) Getbit

func (c *Client) Getbit(key string, offset int) (int64, error)

Returns the bit value at offset in the string value stored at key

func (*Client) Getrange

func (c *Client) Getrange(key string, start int, end int) (Elem, error)

Get a substring of the string stored at a key

func (*Client) Getset

func (c *Client) Getset(key string, value interface{}) (Elem, error)

Set the string value of a key and return its old value

func (*Client) Hdel

func (c *Client) Hdel(key string, field string) (bool, error)

Delete a hash field TODO: Delete one or more hash fields

func (*Client) Hexists

func (c *Client) Hexists(key string, field string) (bool, error)

Determine if a hash field exists

func (*Client) Hget

func (c *Client) Hget(key string, field string) (Elem, error)

Get the value of a hash field

func (*Client) Hgetall

func (c *Client) Hgetall(key string) (*Reply, error)

Get all the fields and values in a hash

func (*Client) Hincrby

func (c *Client) Hincrby(key string, field string, increment int64) (int64, error)

Increment the integer value of a hash field by the given number

func (*Client) Hkeys

func (c *Client) Hkeys(key string) ([]string, error)

Get all the fields in a hash

func (*Client) Hlen

func (c *Client) Hlen(key string) (int64, error)

Get the number of fields in a hash

func (*Client) Hmget

func (c *Client) Hmget(key string, fields ...string) (*Reply, error)

Get the values of all the given hash fields

func (*Client) Hmset

func (c *Client) Hmset(key string, mapping map[string]interface{}) error

Set multiple hash fields to multiple values

func (*Client) Hset

func (c *Client) Hset(key string, field string, value interface{}) (bool, error)

Set the string value of a hash field

func (*Client) Hsetnx

func (c *Client) Hsetnx(key string, field string, value interface{}) (bool, error)

Set the value of a hash field, only if the field does not exist

func (*Client) Hvals

func (c *Client) Hvals(key string) (*Reply, error)

Get all the values in a hash

func (*Client) Incr

func (c *Client) Incr(key string) (int64, error)

Increment the integer value of a key by one

func (*Client) Incrby

func (c *Client) Incrby(key string, increment int64) (int64, error)

Increment the integer value of a key by the given number

func (*Client) Info

func (c *Client) Info() (Elem, error)

Get information and statistics about the server

func (*Client) Keys

func (c *Client) Keys(pattern string) ([]string, error)

Find all keys matching the given pattern

func (*Client) Lastsave

func (c *Client) Lastsave() (int64, error)

Get the UNIX time stamp of the last successful save to disk

func (*Client) Lindex

func (c *Client) Lindex(key string, index int) (Elem, error)

Get an element from a list by its index

func (*Client) Linsert

func (c *Client) Linsert(key, where string, pivot, value interface{}) (int64, error)

Insert an element before or after another element in a list

func (*Client) Llen

func (c *Client) Llen(key string) (int64, error)

Get the length of a list

func (*Client) Lpop

func (c *Client) Lpop(key string) (Elem, error)

Remove and get the first element in a list

func (*Client) Lpush

func (c *Client) Lpush(key string, value interface{}) (int64, error)

Prepend a value to a list TODO: Prepend one or multiple values to a list

func (*Client) Lpushx

func (c *Client) Lpushx(key string, value interface{}) (int64, error)

Prepend a value to a list, only if the list exists

func (*Client) Lrange

func (c *Client) Lrange(key string, start, stop int) (*Reply, error)

Get a range of elements from a list

func (*Client) Lrem

func (c *Client) Lrem(key string, count int, value interface{}) (int64, error)

Remove elements from a list

func (*Client) Lset

func (c *Client) Lset(key string, index int, value interface{}) error

Set the value of an element in a list by its index

func (*Client) Ltrim

func (c *Client) Ltrim(key string, start int, stop int) error

Trim a list to the specified range

func (*Client) Mget

func (c *Client) Mget(keys ...string) (*Reply, error)

Get the values of all the given keys

func (*Client) Monitor

func (c *Client) Monitor() (*Reply, error)

Listen for all requests received by the server in real time

func (*Client) Move

func (c *Client) Move(key string, db int) (bool, error)

Move a key to another database

func (*Client) Mset

func (c *Client) Mset(mapping map[string]string) error

Set multiple keys to multiple values

func (*Client) Msetnx

func (c *Client) Msetnx(mapping map[string]string) (bool, error)

Set multiple keys to multiple values, only if none of the keys exist

func (*Client) Persist

func (c *Client) Persist(key string) (bool, error)

Remove the expiration from a key

func (*Client) Ping

func (c *Client) Ping() (Elem, error)

Ping the server

func (*Client) Psubscribe

func (c *Client) Psubscribe(patterns ...string) (*Sub, error)

Listen for messages published to channels matching the given patterns

func (*Client) Publish

func (c *Client) Publish(channel string, message interface{}) (int64, error)

Post a message to a channel

func (*Client) Quit

func (c *Client) Quit() error

Close the connection

func (*Client) Randomkey

func (c *Client) Randomkey() (string, error)

Return a random key from the keyspace

func (*Client) Rename

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

Rename a key

func (*Client) Renamenx

func (c *Client) Renamenx(key string, newkey string) (bool, error)

Rename a key, only if the new key does not exist

func (*Client) Rpop

func (c *Client) Rpop(key string) (Elem, error)

Remove and get the last element in a list

func (*Client) Rpoplpush

func (c *Client) Rpoplpush(source string, destination string) (Elem, error)

Remove the last element in a list, append it to another list and return it

func (*Client) Rpush

func (c *Client) Rpush(key string, value interface{}) (int64, error)

Append a value to a list TODO: Append one or multiple values to a list

func (*Client) Rpushx

func (c *Client) Rpushx(key string, value interface{}) (int64, error)

Append a value to a list, only if the list exists

func (*Client) Sadd

func (c *Client) Sadd(key string, member interface{}) (bool, error)

Add a member to a set TODO: Add one or more members to a set

func (*Client) Save

func (c *Client) Save() error

Synchronously save the dataset to disk

func (*Client) Scard

func (c *Client) Scard(key string) (int64, error)

Get the number of members in a set

func (*Client) Sdiff

func (c *Client) Sdiff(keys ...string) (*Reply, error)

Subtract multiple sets

func (*Client) Sdiffstore

func (c *Client) Sdiffstore(destination string, keys ...string) (int64, error)

Subtract multiple sets and store the resulting set in a key

func (*Client) Select

func (c *Client) Select(index int) error

Change the selected database for the current connection

func (*Client) Set

func (c *Client) Set(key string, value interface{}) error

Set the string value of a key

func (*Client) Setbit

func (c *Client) Setbit(key string, offset int, value int) (int64, error)

Sets or clears the bit at offset in the string value stored at key

func (*Client) Setex

func (c *Client) Setex(key string, seconds int64, value interface{}) error

Set the value and expiration of a key

func (*Client) Setnx

func (c *Client) Setnx(key string, value interface{}) (bool, error)

Set the value of a key, only if the key does not exist

func (*Client) Setrange

func (c *Client) Setrange(key string, offset int, value interface{}) (int64, error)

Overwrite part of a string at key starting at the specified offset

func (*Client) Shutdown

func (c *Client) Shutdown() error

Synchronously save the dataset to disk and then shut down the server

func (*Client) Sinter

func (c *Client) Sinter(keys ...string) (*Reply, error)

Intersect multiple sets

func (*Client) Sinterstore

func (c *Client) Sinterstore(destination string, keys ...string) (int64, error)

Intersect multiple sets and store the resulting set in a key

func (*Client) Sismember

func (c *Client) Sismember(key string, member interface{}) (bool, error)

Determine if a given value is a member of a set

func (*Client) Slaveof

func (c *Client) Slaveof(host string, port string) error

Change the replication settings of a slave on the fly

func (*Client) Smembers

func (c *Client) Smembers(key string) (*Reply, error)

Get all the members in a set

func (*Client) Smove

func (c *Client) Smove(source string, destination string, member interface{}) (bool, error)

Move a member from one set to another

func (*Client) Sort

func (c *Client) Sort(key string, args ...string) (*Reply, error)

Sort the elements in a list, set or sorted set

func (*Client) Spop

func (c *Client) Spop(key string) (Elem, error)

Remove and return a random member from a set

func (*Client) Srandmember

func (c *Client) Srandmember(key string) (Elem, error)

Get a random member from a set

func (*Client) Srem

func (c *Client) Srem(key string, member interface{}) (bool, error)

Remove a member from a set TODO: Remove one or more members from a set

func (*Client) Strlen

func (c *Client) Strlen(key string) (int64, error)

Get the length of the value stored in a key

func (*Client) Subscribe

func (c *Client) Subscribe(channels ...string) (*Sub, error)

Listen for messages published to the given channels

func (*Client) Sunion

func (c *Client) Sunion(keys ...string) (*Reply, error)

Add multiple sets

func (*Client) Sunionstore

func (c *Client) Sunionstore(destination string, keys ...string) (int64, error)

Add multiple sets and store the resulting set in a key

func (*Client) Ttl

func (c *Client) Ttl(key string) (int64, error)

Get the time to live for a key

func (*Client) Type

func (c *Client) Type(key string) (string, error)

Determine the type stored at key

func (*Client) Zadd

func (c *Client) Zadd(key string, score float64, member interface{}) (bool, error)

Add a member to a sorted set, or update its score if it already exists

func (*Client) Zcard

func (c *Client) Zcard(key string) (int64, error)

Get the number of members in a sorted set

func (*Client) Zcount

func (c *Client) Zcount(key string, min float64, max float64) (int64, error)

Count the members in a sorted set with scores within the given values

func (*Client) Zincrby

func (c *Client) Zincrby(key string, increment float64, member interface{}) (float64, error)

Increment the score of a member in a sorted set

func (*Client) Zinterstore

func (c *Client) Zinterstore(destination string, keys []string, args ...string) (int64, error)

Intersect multiple sorted sets and store the resulting sorted set in a new key `numkeys` is determined by the len of `keys` param

func (*Client) Zrange

func (c *Client) Zrange(key string, start int, stop int) (*Reply, error)

Return a range of members in a sorted set, by index TODO: add WITHSCORES keyword

func (*Client) Zrangebyscore

func (c *Client) Zrangebyscore(key string, min string, max string, args ...string) (*Reply, error)

Return a range of members in a sorted set, by score

func (*Client) Zrank

func (c *Client) Zrank(key string, member interface{}) (int64, error)

Determine the index of a member in a sorted set TODO: should cast an error when member does not exist

func (*Client) Zrem

func (c *Client) Zrem(key string, member interface{}) (bool, error)

Remove a member from a sorted set TODO: Remove one or more members from a sorted set

func (*Client) Zremrangebyrank

func (c *Client) Zremrangebyrank(key string, start int, stop int) (int64, error)

Remove all members in a sorted set within the given indexes

func (*Client) Zremrangebyscore

func (c *Client) Zremrangebyscore(key string, min float64, max float64) (int64, error)

Remove all members in a sorted set within the given scores

func (*Client) Zrevrange

func (c *Client) Zrevrange(key string, start int, stop int, args ...string) (*Reply, error)

Return a range of members in a sorted set, by index, with scores ordered from high to low

func (*Client) Zrevrangebyscore

func (c *Client) Zrevrangebyscore(key string, max float64, min float64, args ...string) (*Reply, error)

Return a range of members in a sorted set, by score, with scores ordered from high to low

func (*Client) Zrevrank

func (c *Client) Zrevrank(key string, member interface{}) (int64, error)

Determine the index of a member in a sorted set, with scores ordered from high to low

func (*Client) Zscore

func (c *Client) Zscore(key string, member interface{}) (float64, error)

Get the score associated with the given member in a sorted set

func (*Client) Zunionstore

func (c *Client) Zunionstore(destination string, keys []string, args ...string) (int64, error)

Add multiple sorted sets and store the resulting sorted set in a new key `numkeys` is determined by the len of `keys` param

type Elem

type Elem []byte

func (Elem) Bytes

func (e Elem) Bytes() []byte

func (Elem) Float64

func (e Elem) Float64() float64

func (Elem) Int64

func (e Elem) Int64() int64

func (Elem) String

func (e Elem) String() string

type Message

type Message struct {
	Channel string
	Elem    Elem
}

type Pipe

type Pipe struct {
	*Sync
	// contains filtered or unexported fields
}

type PipeClient

type PipeClient struct {
	*Client
}

func NewPipeClient

func NewPipeClient(netaddr string, db int, password string) *PipeClient

PipeClient include support for MULTI/EXEC operations. It implements Exec() which executes all buffered commands. Set transaction to true to wrap buffered commands inside MULTI .. EXEC block. PipeClient is not thread-safe.

func NewPipeClientFromClient

func NewPipeClientFromClient(c *Client) *PipeClient

Uses the connection settings from a existing client to create a new PipeClient

func (*PipeClient) Exec

func (pc *PipeClient) Exec() []*Reply

Execute all commands issued after EXEC or buffered in the current pipe. Returns a slice of Replies.

func (*PipeClient) Multi

func (pc *PipeClient) Multi() error

Mark the start of a transaction block

func (*PipeClient) Unwatch

func (pc *PipeClient) Unwatch() error

Forget about all watched keys

func (*PipeClient) Watch

func (pc *PipeClient) Watch(keys ...string) error

Watch the given keys to determine execution of the MULTI/EXEC block

type ReaderWriter

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

type Reply

type Reply struct {
	Err   error
	Elem  Elem
	Elems []*Reply
	// contains filtered or unexported fields
}

func Send

func Send(rw ReaderWriter, args ...[]byte) *Reply

writes a command a and returns single the Reply object

func SendIface

func SendIface(rw ReaderWriter, name string, args ...interface{}) *Reply

uses reflection to create a bytestring of the name and args parameters, then calls Send()

func SendStr

func SendStr(rw ReaderWriter, name string, args ...string) *Reply

creates a bytestring of the name and args parameters, then calls Send()

func (*Reply) BytesArray

func (r *Reply) BytesArray() [][]byte

func (*Reply) IntArray

func (r *Reply) IntArray() []int64

func (*Reply) Message

func (r *Reply) Message() *Message

func (*Reply) StringArray

func (r *Reply) StringArray() []string

func (*Reply) StringMap

func (r *Reply) StringMap() map[string]string

type Sub

type Sub struct {
	Messages chan *Message
	// contains filtered or unexported fields
}

func NewSub

func NewSub(addr string, db int, password string) *Sub

func (*Sub) Close

func (s *Sub) Close()

Free the connection and close the chan

func (*Sub) Psubscribe

func (s *Sub) Psubscribe(patterns ...string) error

Listen for messages published to channels matching the given patterns

func (*Sub) Punsubscribe

func (s *Sub) Punsubscribe(patterns ...string) error

Stop listening for messages posted to channels matching the given patterns

func (*Sub) Subscribe

func (s *Sub) Subscribe(channels ...string) error

Listen for messages published to the given channels

func (*Sub) Unsubscribe

func (s *Sub) Unsubscribe(channels ...string) error

Stop listening for messages posted to the given channels

type Sync

type Sync struct {
	Addr     string
	Db       int
	Password string
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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