rediqueue

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2017 License: MIT Imports: 19 Imported by: 0

README

RediQueue

Pure Go queue based on REDIS protocol.

Commands

Implemented commands:

  • Connection (complete)
    • AUTH -- see RequireAuth()
    • ECHO
    • PING
    • SELECT
    • QUIT
  • Key
    • DEL
    • EXISTS
    • EXPIRE
    • EXPIREAT
    • KEYS
    • MOVE
    • PERSIST
    • PEXPIRE
    • PEXPIREAT
    • PTTL
    • RENAME
    • RENAMENX
    • RANDOMKEY -- call math.rand.Seed(...) once before using.
    • TTL
    • TYPE
    • SCAN
  • Transactions (complete)
    • DISCARD
    • EXEC
    • MULTI
    • UNWATCH
    • WATCH
  • Server
    • DBSIZE
    • FLUSHALL
    • FLUSHDB
  • List keys (complete)
    • BLPOP
    • BRPOP
    • BRPOPLPUSH
    • LINDEX
    • LINSERT
    • LLEN
    • LPOP
    • LPUSH
    • LPUSHX
    • LRANGE
    • LREM
    • LSET
    • LTRIM
    • RPOP
    • RPOPLPUSH
    • RPUSH
    • RPUSHX
  • Set keys (complete)
    • SADD
    • SCARD
    • SDIFF
    • SDIFFSTORE
    • SINTER
    • SINTERSTORE
    • SISMEMBER
    • SMEMBERS
    • SMOVE
    • SPOP -- call math.rand.Seed(...) once before using.
    • SRANDMEMBER -- call math.rand.Seed(...) once before using.
    • SREM
    • SUNION
    • SUNIONSTORE
    • SSCAN

Not supported

Commands which will probably not be implemented:

  • CLUSTER (all)
    • CLUSTER *
    • READONLY
    • READWRITE
  • GEO (all) -- unless someone needs these
    • GEOADD
    • GEODIST
    • GEOHASH
    • GEOPOS
    • GEORADIUS
    • GEORADIUSBYMEMBER
  • HyperLogLog (all) -- unless someone needs these
    • PFADD
    • PFCOUNT
    • PFMERGE
  • Key
    • DUMP
    • MIGRATE
    • OBJECT
    • RESTORE
    • WAIT
  • Pub/Sub (all)
    • PSUBSCRIBE
    • PUBLISH
    • PUBSUB
    • PUNSUBSCRIBE
    • SUBSCRIBE
    • UNSUBSCRIBE
  • Scripting (all)
    • EVAL
    • EVALSHA
    • SCRIPT *
  • Server
    • BGSAVE
    • BGWRITEAOF
    • CLIENT *
    • COMMAND *
    • CONFIG *
    • DEBUG *
    • INFO
    • LASTSAVE
    • MONITOR
    • ROLE
    • SAVE
    • SHUTDOWN
    • SLAVEOF
    • SLOWLOG
    • SYNC
    • TIME
  • Hash
  • String
  • SortedSet

Documentation

Overview

Package rediqueue is a pure Go Redis test server, for use in Go unittests. There are no dependencies on system binaries, and every server you start will be empty.

Start a server with `s, err := rediqueue.Run()`. Stop it with `defer s.Close()`.

Point your Redis client to `s.Addr()` or `s.Host(), s.Port()`.

Set keys directly via s.Set(...) and similar commands, or use a Redis client.

For direct use you can select a Redis database with either `s.Select(12); s.Get("foo")` or `s.DB(12).Get("foo")`.

Example
package main

import (
	"github.com/chinahdkj/rediqueue"
	"github.com/garyburd/redigo/redis"
)

func main() {
	s, err := rediqueue.Run()
	if err != nil {
		panic(err)
	}
	defer s.Close()

	// Configure you application to connect to redis at s.Addr()
	// Any redis client should work, as long as you use redis commands which
	// rediqueue implements.
	c, err := redis.Dial("tcp", s.Addr())
	if err != nil {
		panic(err)
	}
	if _, err = c.Do("LPUSH", "foo", "bar"); err != nil {
		panic(err)
	}

	// You can ask rediqueue about keys directly, without going over the network.
	if got, err := s.Lpop("foo"); err != nil || got != "bar" {
		panic("Didn't get 'bar' back")
	}
	// Or with a DB id
	if _, err := s.DB(42).Lpop("foo"); err != rediqueue.ErrKeyNotFound {
		panic("didn't use a different database")
	}

	// Or use a Check* function which Fail()s if the key is not what we expect
	// (checks for existence, key type and the value)
	// s.CheckGet(t, "foo", "bar")

	// Check if there really was only one connection.
	if s.TotalConnectionCount() != 1 {
		panic("too many connections made")
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound is returned when a key doesn't exist.
	ErrKeyNotFound = errors.New(msgKeyNotFound)
	// ErrWrongType when a key is not the right type.
	ErrWrongType = errors.New(msgWrongType)
	// ErrIntValueError can returned by INCRBY
	ErrIntValueError = errors.New(msgInvalidInt)
	// ErrFloatValueError can returned by INCRBYFLOAT
	ErrFloatValueError = errors.New(msgInvalidFloat)
)

Functions

This section is empty.

Types

type RediQueue

type RediQueue struct {
	sync.Mutex
	// contains filtered or unexported fields
}

RediQueue is a Redis server implementation.

func NewRediQueue

func NewRediQueue() *RediQueue

NewRediQueue makes a new, non-started, RediQueue object.

func Run

func Run(bgsave bool) (*RediQueue, error)

Run creates and Start()s a RediQueue.

func RunAddr

func RunAddr(addr string, saveDuration int) (*RediQueue, error)

func (*RediQueue) Addr

func (m *RediQueue) Addr() string

Addr returns '127.0.0.1:12345'. Can be given to a Dial(). See also Host() and Port(), which return the same things.

func (*RediQueue) CheckList

func (m *RediQueue) CheckList(t T, key string, expected ...string)

CheckList does not call Errorf() iff there is a list key with the expected values. Normal use case is `m.CheckGet(t, "favorite_colors", "red", "green", "infrared")`.

func (*RediQueue) CheckSet

func (m *RediQueue) CheckSet(t T, key string, expected ...string)

CheckSet does not call Errorf() iff there is a set key with the expected values. Normal use case is `m.CheckSet(t, "visited", "Rome", "Stockholm", "Dublin")`.

func (*RediQueue) Close

func (m *RediQueue) Close()

Close shuts down a RediQueue.

func (*RediQueue) CommandCount

func (m *RediQueue) CommandCount() int

CommandCount returns the number of processed commands.

func (*RediQueue) CurrentConnectionCount

func (m *RediQueue) CurrentConnectionCount() int

CurrentConnectionCount returns the number of currently connected clients.

func (*RediQueue) DB

func (m *RediQueue) DB(i int) *RedisDB

DB returns a DB by ID.

func (*RediQueue) Del

func (m *RediQueue) Del(k string) bool

Del deletes a key and any expiration value. Returns whether there was a key.

func (*RediQueue) Dump

func (m *RediQueue) Dump() string

Dump returns a text version of the selected DB, usable for debugging.

func (*RediQueue) Exists

func (m *RediQueue) Exists(k string) bool

Exists tells whether a key exists.

func (*RediQueue) FlushAll

func (m *RediQueue) FlushAll()

FlushAll removes all keys from all databases.

func (*RediQueue) FlushDB

func (m *RediQueue) FlushDB()

FlushDB removes all keys from the selected database.

func (*RediQueue) Host

func (m *RediQueue) Host() string

Host returns the host part of Addr().

func (*RediQueue) IsMember

func (m *RediQueue) IsMember(k, v string) (bool, error)

IsMember tells if value is in the set.

func (*RediQueue) Keys

func (m *RediQueue) Keys() []string

Keys returns all keys from the selected database, sorted.

func (*RediQueue) List

func (m *RediQueue) List(k string) ([]string, error)

List returns the list k, or an error if it's not there or something else. This is the same as the Redis command `LRANGE 0 -1`, but you can do your own range-ing.

func (*RediQueue) Load

func (m *RediQueue) Load()

func (*RediQueue) Lpop

func (m *RediQueue) Lpop(k string) (string, error)

Lpop is a shift. Returns the popped element.

func (*RediQueue) Lpush

func (m *RediQueue) Lpush(k, v string) (int, error)

Lpush is an unshift. Returns the new length.

func (*RediQueue) Members

func (m *RediQueue) Members(k string) ([]string, error)

Members gives all set keys. Sorted.

func (*RediQueue) Pop

func (m *RediQueue) Pop(k string) (string, error)

Pop removes and returns the last element. Is called RPOP in Redis.

func (*RediQueue) Port

func (m *RediQueue) Port() string

Port returns the (random) port part of Addr().

func (*RediQueue) Push

func (m *RediQueue) Push(k string, v ...string) (int, error)

Push add element at the end. Is called RPUSH in redis. Returns the new length.

func (*RediQueue) RequireAuth

func (m *RediQueue) RequireAuth(pw string)

RequireAuth makes every connection need to AUTH first. Disable again by setting an empty string.

func (*RediQueue) Restart

func (m *RediQueue) Restart() error

Restart restarts a Close()d server on the same port. Values will be preserved.

func (*RediQueue) SRem

func (m *RediQueue) SRem(k string, fields ...string) (int, error)

SRem removes fields from a set. Returns number of deleted fields.

func (*RediQueue) Save

func (m *RediQueue) Save()

func (*RediQueue) Select

func (m *RediQueue) Select(i int)

Select sets the DB id for all direct commands.

func (*RediQueue) SetAdd

func (m *RediQueue) SetAdd(k string, elems ...string) (int, error)

SetAdd adds keys to a set. Returns the number of new keys.

func (*RediQueue) SetTime

func (m *RediQueue) SetTime(t time.Time)

SetTime sets the time against which EXPIREAT values are compared. EXPIREAT will use time.Now() if this is not set.

func (*RediQueue) Start

func (m *RediQueue) Start() error

Start starts a server. It listens on a random port on localhost. See also Addr().

func (*RediQueue) StartAddr

func (m *RediQueue) StartAddr(addr string) error

StartAddr runs rediqueue with a given addr. Examples: "127.0.0.1:6379", ":6379", or "127.0.0.1:0"

func (*RediQueue) TotalConnectionCount

func (m *RediQueue) TotalConnectionCount() int

TotalConnectionCount returns the number of client connections since server start.

func (*RediQueue) Type

func (m *RediQueue) Type(k string) string

Type gives the type of a key, or ""

type RedisDB

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

RedisDB holds a single (numbered) Redis database.

func (*RedisDB) Del

func (db *RedisDB) Del(k string) bool

Del deletes a key and any expiration value. Returns whether there was a key.

func (*RedisDB) Exists

func (db *RedisDB) Exists(k string) bool

Exists tells whether a key exists.

func (*RedisDB) FlushDB

func (db *RedisDB) FlushDB()

FlushDB removes all keys.

func (*RedisDB) IsMember

func (db *RedisDB) IsMember(k, v string) (bool, error)

IsMember tells if value is in the set.

func (*RedisDB) Keys

func (db *RedisDB) Keys() []string

Keys returns all keys, sorted.

func (*RedisDB) List

func (db *RedisDB) List(k string) ([]string, error)

List returns the list k, or an error if it's not there or something else. This is the same as the Redis command `LRANGE 0 -1`, but you can do your own range-ing.

func (*RedisDB) Lpop

func (db *RedisDB) Lpop(k string) (string, error)

Lpop is a shift. Returns the popped element.

func (*RedisDB) Lpush

func (db *RedisDB) Lpush(k, v string) (int, error)

Lpush is an unshift. Returns the new length.

func (*RedisDB) Members

func (db *RedisDB) Members(k string) ([]string, error)

Members gives all set keys. Sorted.

func (*RedisDB) Pop

func (db *RedisDB) Pop(k string) (string, error)

Pop removes and returns the last element. Is called RPOP in Redis.

func (*RedisDB) Push

func (db *RedisDB) Push(k string, v ...string) (int, error)

Push add element at the end. Is called RPUSH in redis. Returns the new length.

func (*RedisDB) SRem

func (db *RedisDB) SRem(k string, fields ...string) (int, error)

SRem removes fields from a set. Returns number of deleted fields.

func (*RedisDB) SetAdd

func (db *RedisDB) SetAdd(k string, elems ...string) (int, error)

SetAdd adds keys to a set. Returns the number of new keys.

func (*RedisDB) Type

func (db *RedisDB) Type(k string) string

Type gives the type of a key, or ""

type T

type T interface {
	Fail()
}

T is implemented by Testing.T

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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