Documentation ¶
Overview ¶
Package miniredis 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 := miniredis.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 ¶
s, err := miniredis.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 // miniredis implements. c, err := redis.Dial("tcp", s.Addr()) if err != nil { panic(err) } if _, err = c.Do("SET", "foo", "bar"); err != nil { panic(err) } // You can ask miniredis about keys directly, without going over the network. if got, err := s.Get("foo"); err != nil || got != "bar" { panic("Didn't get 'bar' back") } // Or with a DB id if _, err := s.DB(42).Get("foo"); err != miniredis.ErrKeyNotFound { panic("didn't use a different database") } // Test key with expiration s.SetTTL("foo", 60*time.Second) s.FastForward(60 * time.Second) if s.Exists("foo") { panic("expect key to be expired") } // 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 ¶
- Variables
- type Miniredis
- func (m *Miniredis) Addr() string
- func (m *Miniredis) CheckGet(t T, key, expected string)
- func (m *Miniredis) CheckList(t T, key string, expected ...string)
- func (m *Miniredis) CheckSet(t T, key string, expected ...string)
- func (m *Miniredis) Close()
- func (m *Miniredis) CommandCount() int
- func (m *Miniredis) CurrentConnectionCount() int
- func (m *Miniredis) DB(i int) *RedisDB
- func (m *Miniredis) Del(k string) bool
- func (m *Miniredis) Dump() string
- func (m *Miniredis) Exists(k string) bool
- func (m *Miniredis) FastForward(duration time.Duration)
- func (m *Miniredis) FlushAll()
- func (m *Miniredis) FlushDB()
- func (m *Miniredis) Get(k string) (string, error)
- func (m *Miniredis) HDel(k, f string)
- func (m *Miniredis) HGet(k, f string) string
- func (m *Miniredis) HIncr(k, f string, delta int) (int, error)
- func (m *Miniredis) HIncrBy(k, f string, delta int) (int, error)
- func (m *Miniredis) HIncrByFloat(k, f string, delta float64) (float64, error)
- func (m *Miniredis) HIncrfloat(k, f string, delta float64) (float64, error)
- func (m *Miniredis) HKeys(k string) ([]string, error)
- func (m *Miniredis) HSet(k string, fv ...string)
- func (m *Miniredis) Host() string
- func (m *Miniredis) Incr(k string, delta int) (int, error)
- func (m *Miniredis) IncrByFloat(k string, delta float64) (float64, error)
- func (m *Miniredis) Incrfloat(k string, delta float64) (float64, error)
- func (m *Miniredis) IsMember(k, v string) (bool, error)
- func (m *Miniredis) Keys() []string
- func (m *Miniredis) List(k string) ([]string, error)
- func (m *Miniredis) Lpop(k string) (string, error)
- func (m *Miniredis) Lpush(k, v string) (int, error)
- func (m *Miniredis) Members(k string) ([]string, error)
- func (m *Miniredis) NewSubscriber() *Subscriber
- func (m *Miniredis) Pop(k string) (string, error)
- func (m *Miniredis) Port() string
- func (m *Miniredis) PubSubChannels(pattern string) []string
- func (m *Miniredis) PubSubNumPat() int
- func (m *Miniredis) PubSubNumSub(channels ...string) map[string]int
- func (m *Miniredis) Publish(channel, message string) int
- func (m *Miniredis) Push(k string, v ...string) (int, error)
- func (m *Miniredis) RPop(k string) (string, error)
- func (m *Miniredis) RPush(k string, v ...string) (int, error)
- func (m *Miniredis) RequireAuth(pw string)
- func (m *Miniredis) Restart() error
- func (m *Miniredis) SAdd(k string, elems ...string) (int, error)
- func (m *Miniredis) SIsMember(k, v string) (bool, error)
- func (m *Miniredis) SMembers(k string) ([]string, error)
- func (m *Miniredis) SRem(k string, fields ...string) (int, error)
- func (m *Miniredis) Seed(seed int)
- func (m *Miniredis) Select(i int)
- func (m *Miniredis) Server() *server.Server
- func (m *Miniredis) Set(k, v string) error
- func (m *Miniredis) SetAdd(k string, elems ...string) (int, error)
- func (m *Miniredis) SetTTL(k string, ttl time.Duration)
- func (m *Miniredis) SetTime(t time.Time)
- func (m *Miniredis) SortedSet(k string) (map[string]float64, error)
- func (m *Miniredis) Start() error
- func (m *Miniredis) StartAddr(addr string) error
- func (m *Miniredis) Stream(k string) ([]StreamEntry, error)
- func (m *Miniredis) SwapDB(i, j int) bool
- func (m *Miniredis) TTL(k string) time.Duration
- func (m *Miniredis) TotalConnectionCount() int
- func (m *Miniredis) Type(k string) string
- func (m *Miniredis) Unlink(k string) bool
- func (m *Miniredis) XAdd(k string, id string, values []string) (string, error)
- func (m *Miniredis) ZAdd(k string, score float64, member string) (bool, error)
- func (m *Miniredis) ZMembers(k string) ([]string, error)
- func (m *Miniredis) ZRem(k, member string) (bool, error)
- func (m *Miniredis) ZScore(k, member string) (float64, error)
- type PubsubMessage
- type PubsubPmessage
- type RedisDB
- func (db *RedisDB) Del(k string) bool
- func (db *RedisDB) Exists(k string) bool
- func (db *RedisDB) FlushDB()
- func (db *RedisDB) Get(k string) (string, error)
- func (db *RedisDB) HDel(k, f string)
- func (db *RedisDB) HGet(k, f string) string
- func (db *RedisDB) HIncr(k, f string, delta int) (int, error)
- func (db *RedisDB) HIncrfloat(k, f string, delta float64) (float64, error)
- func (db *RedisDB) HKeys(key string) ([]string, error)
- func (db *RedisDB) HSet(k string, fv ...string)
- func (db *RedisDB) Incr(k string, delta int) (int, error)
- func (db *RedisDB) Incrfloat(k string, delta float64) (float64, error)
- func (db *RedisDB) IsMember(k, v string) (bool, error)
- func (db *RedisDB) Keys() []string
- func (db *RedisDB) List(k string) ([]string, error)
- func (db *RedisDB) Lpop(k string) (string, error)
- func (db *RedisDB) Lpush(k, v string) (int, error)
- func (db *RedisDB) Members(k string) ([]string, error)
- func (db *RedisDB) Pop(k string) (string, error)
- func (db *RedisDB) Push(k string, v ...string) (int, error)
- func (db *RedisDB) SRem(k string, fields ...string) (int, error)
- func (db *RedisDB) Set(k, v string) error
- func (db *RedisDB) SetAdd(k string, elems ...string) (int, error)
- func (db *RedisDB) SetTTL(k string, ttl time.Duration)
- func (db *RedisDB) SortedSet(k string) (map[string]float64, error)
- func (db *RedisDB) Stream(k string) ([]StreamEntry, error)
- func (db *RedisDB) TTL(k string) time.Duration
- func (db *RedisDB) Type(k string) string
- func (db *RedisDB) Unlink(k string) bool
- func (db *RedisDB) XAdd(k string, id string, values []string) (string, error)
- func (db *RedisDB) ZAdd(k string, score float64, member string) (bool, error)
- func (db *RedisDB) ZMembers(k string) ([]string, error)
- func (db *RedisDB) ZRem(k, member string) (bool, error)
- func (db *RedisDB) ZScore(k, member string) (float64, error)
- type StreamEntry
- type Subscriber
- func (s *Subscriber) Channels() []string
- func (s *Subscriber) Close()
- func (s *Subscriber) Count() int
- func (s *Subscriber) Messages() <-chan PubsubMessage
- func (s *Subscriber) Patterns() []string
- func (s *Subscriber) Pmessages() <-chan PubsubPmessage
- func (s *Subscriber) Psubscribe(pat string) int
- func (s *Subscriber) Publish(c, msg string) int
- func (s *Subscriber) Punsubscribe(pat string) int
- func (s *Subscriber) Subscribe(c string) int
- func (s *Subscriber) Unsubscribe(c string) int
- type T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 Miniredis ¶
type Miniredis struct { sync.Mutex Ctx context.Context CtxCancel context.CancelFunc // contains filtered or unexported fields }
Miniredis is a Redis server implementation.
func NewMiniRedis ¶
func NewMiniRedis() *Miniredis
NewMiniRedis makes a new, non-started, Miniredis object.
func (*Miniredis) Addr ¶
Addr returns '127.0.0.1:12345'. Can be given to a Dial(). See also Host() and Port(), which return the same things.
func (*Miniredis) CheckGet ¶
CheckGet does not call Errorf() iff there is a string key with the expected value. Normal use case is `m.CheckGet(t, "username", "theking")`.
func (*Miniredis) CheckList ¶
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 (*Miniredis) CheckSet ¶
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 (*Miniredis) CommandCount ¶
CommandCount returns the number of processed commands.
func (*Miniredis) CurrentConnectionCount ¶
CurrentConnectionCount returns the number of currently connected clients.
func (*Miniredis) Del ¶
Del deletes a key and any expiration value. Returns whether there was a key.
func (*Miniredis) FastForward ¶
FastForward decreases all TTLs by the given duration. All TTLs <= 0 will be expired.
func (*Miniredis) FlushAll ¶
func (m *Miniredis) FlushAll()
FlushAll removes all keys from all databases.
func (*Miniredis) FlushDB ¶
func (m *Miniredis) FlushDB()
FlushDB removes all keys from the selected database.
func (*Miniredis) HGet ¶
HGet returns hash keys added with HSET. This will return an empty string if the key is not set. Redis would return a nil. Returns empty string when the key is of a different type.
func (*Miniredis) HIncrBy ¶ added in v2.10.0
HIncrBy increases the integer value of a hash field by delta (int).
func (*Miniredis) HIncrByFloat ¶ added in v2.10.0
HIncrByFloat increases a key/field by delta (float).
func (*Miniredis) HIncrfloat ¶
HIncrfloat increases a key/field by delta (float).
func (*Miniredis) HSet ¶
HSet sets hash keys. If there is another key by the same name it will be gone.
func (*Miniredis) IncrByFloat ¶ added in v2.10.0
IncrByFloat increments the float value of a key by the given delta. is an alias for Miniredis.Incrfloat
func (*Miniredis) List ¶
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 (*Miniredis) NewSubscriber ¶
func (m *Miniredis) NewSubscriber() *Subscriber
Start a new pubsub subscriber. It can (un) subscribe to channels and patterns, and has a channel to get published messages. Close it with Close(). Does not close itself when there are no subscriptions left.
func (*Miniredis) PubSubChannels ¶
PubSubChannels is "PUBSUB CHANNELS <pattern>". An empty pattern is fine (meaning all channels). Returned channels will be ordered alphabetically.
func (*Miniredis) PubSubNumPat ¶
PubSubNumPat is "PUBSUB NUMPAT"
func (*Miniredis) PubSubNumSub ¶
PubSubNumSub is "PUBSUB NUMSUB [channels]". It returns all channels with their subscriber count.
func (*Miniredis) RPush ¶ added in v2.10.0
RPush appends one or multiple values to a list. Returns the new length. An alias for Push
func (*Miniredis) RequireAuth ¶
RequireAuth makes every connection need to AUTH first. Disable again by setting an empty string.
func (*Miniredis) Restart ¶
Restart restarts a Close()d server on the same port. Values will be preserved.
func (*Miniredis) SAdd ¶ added in v2.10.0
SAdd adds keys to a set. Returns the number of new keys. Alias for SetAdd
func (*Miniredis) SIsMember ¶ added in v2.10.0
SIsMember tells if value is in the set. Alias for IsMember
func (*Miniredis) SMembers ¶ added in v2.10.0
SMembers returns all keys in a set, sorted. Alias for Members.
func (*Miniredis) Server ¶ added in v2.10.1
Server returns the underlying server to allow custom commands to be implemented
func (*Miniredis) SetTime ¶
SetTime sets the time against which EXPIREAT values are compared, and the time used in stream entry IDs. Will use time.Now() if this is not set.
func (*Miniredis) Start ¶
Start starts a server. It listens on a random port on localhost. See also Addr().
func (*Miniredis) StartAddr ¶
StartAddr runs miniredis with a given addr. Examples: "127.0.0.1:6379", ":6379", or "127.0.0.1:0"
func (*Miniredis) Stream ¶ added in v2.11.0
func (m *Miniredis) Stream(k string) ([]StreamEntry, error)
Stream returns a slice of stream entries. Oldest first.
func (*Miniredis) TTL ¶
TTL is the left over time to live. As set via EXPIRE, PEXPIRE, EXPIREAT, PEXPIREAT. Note: this direct function returns 0 if there is no TTL set, unlike redis, which returns -1.
func (*Miniredis) TotalConnectionCount ¶
TotalConnectionCount returns the number of client connections since server start.
func (*Miniredis) Unlink ¶ added in v2.10.0
Unlink deletes a key and any expiration value. Returns where there was a key. It's exactly the same as Del() and is not async. It is here for the consistency.
func (*Miniredis) XAdd ¶ added in v2.11.0
XAdd adds an entry to a stream. `id` can be left empty or be '*'. If a value is given normal XADD rules apply. Values should be an even length.
type PubsubMessage ¶
PubsubMessage is what gets broadcasted over pubsub channels.
type PubsubPmessage ¶ added in v2.9.0
type RedisDB ¶
type RedisDB struct {
// contains filtered or unexported fields
}
RedisDB holds a single (numbered) Redis database.
func (*RedisDB) HGet ¶
HGet returns hash keys added with HSET. Returns empty string when the key is of a different type.
func (*RedisDB) HIncrfloat ¶
HIncrfloat increases a key/field by delta (float).
func (*RedisDB) HSet ¶
HSet sets hash keys. If there is another key by the same name it will be gone.
func (*RedisDB) List ¶
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) Push ¶
Push add element at the end. Is called RPUSH in redis. Returns the new length.
func (*RedisDB) Set ¶
Set sets a string key. Removes expire. Unlike redis the key can't be an existing non-string key.
func (*RedisDB) Stream ¶ added in v2.11.0
func (db *RedisDB) Stream(k string) ([]StreamEntry, error)
Stream returns a slice of stream entries. Oldest first.
func (*RedisDB) TTL ¶
TTL is the left over time to live. As set via EXPIRE, PEXPIRE, EXPIREAT, PEXPIREAT. 0 if not set.
func (*RedisDB) Unlink ¶ added in v2.10.0
Unlink deletes a key and any expiration value. Returns where there was a key. It's exactly the same as Del() and is not async. It is here for the consistency.
func (*RedisDB) XAdd ¶ added in v2.11.0
XAdd adds an entry to a stream. `id` can be left empty or be '*'. If a value is given normal XADD rules apply. Values should be an even length.
type StreamEntry ¶ added in v2.11.0
A StreamEntry is an entry in a stream. The ID is always of the form "123-123". Values should have an even length of entries.
type Subscriber ¶
type Subscriber struct {
// contains filtered or unexported fields
}
Subscriber has the (p)subscriptions.
func (*Subscriber) Channels ¶
func (s *Subscriber) Channels() []string
List all subscribed channels, in alphabetical order
func (*Subscriber) Count ¶
func (s *Subscriber) Count() int
Count the total number of channels and patterns
func (*Subscriber) Messages ¶
func (s *Subscriber) Messages() <-chan PubsubMessage
The channel to read messages for this subscriber. Only for messages matching a SUBSCRIBE.
func (*Subscriber) Patterns ¶
func (s *Subscriber) Patterns() []string
List all subscribed patterns, in alphabetical order
func (*Subscriber) Pmessages ¶ added in v2.9.0
func (s *Subscriber) Pmessages() <-chan PubsubPmessage
The channel to read messages for this subscriber. Only for messages matching a PSUBSCRIBE.
func (*Subscriber) Psubscribe ¶
func (s *Subscriber) Psubscribe(pat string) int
Subscribe to a pattern. Returns the total number of (p)subscriptions after subscribing.
func (*Subscriber) Publish ¶
func (s *Subscriber) Publish(c, msg string) int
Publish a message. Will return return how often we sent the message (can be a match for a subscription and for a psubscription.
func (*Subscriber) Punsubscribe ¶
func (s *Subscriber) Punsubscribe(pat string) int
Unsubscribe a pattern. Returns the total number of (p)subscriptions after unsubscribing.
func (*Subscriber) Subscribe ¶
func (s *Subscriber) Subscribe(c string) int
Subscribe to a channel. Returns the total number of (p)subscriptions after subscribing.
func (*Subscriber) Unsubscribe ¶
func (s *Subscriber) Unsubscribe(c string) int
Unsubscribe a channel. Returns the total number of (p)subscriptions after unsubscribing.
Source Files ¶
- check.go
- cmd_cluster.go
- cmd_command.go
- cmd_connection.go
- cmd_generic.go
- cmd_geo.go
- cmd_hash.go
- cmd_list.go
- cmd_pubsub.go
- cmd_scripting.go
- cmd_server.go
- cmd_set.go
- cmd_sorted_set.go
- cmd_stream.go
- cmd_string.go
- cmd_transactions.go
- db.go
- direct.go
- geo.go
- keys.go
- lua.go
- miniredis.go
- pubsub.go
- redis.go
- sorted_set.go
- stream.go