MCPcopy
hub / github.com/alicebob/miniredis

github.com/alicebob/miniredis @v2.38.0 sqlite

repository ↗ · DeepWiki ↗ · release v2.38.0 ↗
1,272 symbols 8,767 edges 103 files 664 documented · 52%
README

Miniredis

Pure Go Redis test server, used in Go unittests.

Sometimes you want to test code which uses Redis, without making it a full-blown integration test. Miniredis implements (parts of) the Redis server, to be used in unittests. It enables a simple, cheap, in-memory, Redis replacement, with a real TCP interface. Think of it as the Redis version of net/http/httptest.

It saves you from using mock code, and since the redis server lives in the test process you can query for values directly, without going through the server stack.

There are no dependencies on external binaries, so you can easily integrate it in automated build processes.

Be sure to import v2:

import "github.com/alicebob/miniredis/v2"

Commands

Implemented commands:

  • Connection
  • AUTH -- see RequireAuth()
  • ECHO
  • HELLO -- see RequireUserAuth()
  • PING
  • SELECT
  • SWAPDB
  • QUIT
  • Key
  • COPY
  • DEL
  • DUMP -- partly, only handles string keys
  • EXISTS
  • EXPIRE
  • EXPIREAT
  • EXPIRETIME
  • KEYS
  • MOVE
  • PERSIST
  • PEXPIRE
  • PEXPIREAT
  • PEXPIRETIME
  • PTTL
  • RANDOMKEY -- see m.Seed(...)
  • RENAME
  • RENAMENX
  • RESTORE -- partly, only handles string keys
  • SCAN
  • TOUCH
  • TTL
  • TYPE
  • UNLINK
  • WAIT -- no-op
  • Transactions
  • DISCARD
  • EXEC
  • MULTI
  • UNWATCH
  • WATCH
  • Server
  • DBSIZE
  • FLUSHALL
  • FLUSHDB
  • TIME -- returns time.Now() or value set by SetTime()
  • COMMAND -- partly
  • INFO -- partly, returns only "clients" section with one field "connected_clients"
  • String keys
  • APPEND
  • BITCOUNT
  • BITOP
  • BITPOS
  • DECR
  • DECRBY
  • DELEX -- partly
  • GET
  • GETBIT
  • GETDEL
  • GETEX
  • GETRANGE
  • GETSET
  • INCR
  • INCRBY
  • INCRBYFLOAT
  • MGET
  • MSET
  • MSETNX
  • PSETEX
  • SET
  • SETBIT
  • SETEX
  • SETNX
  • SETRANGE
  • STRLEN
  • Hash keys
  • HDEL
  • HEXISTS
  • HGET
  • HGETALL
  • HINCRBY
  • HINCRBYFLOAT
  • HKEYS
  • HLEN
  • HMGET
  • HMSET
  • HRANDFIELD
  • HSET
  • HSETNX
  • HSTRLEN
  • HVALS
  • HSCAN
  • List keys
  • BLPOP
  • BRPOP
  • BRPOPLPUSH
  • LINDEX
  • LINSERT
  • LLEN
  • LPOP
  • LPUSH
  • LPUSHX
  • LRANGE
  • LREM
  • LSET
  • LTRIM
  • RPOP
  • RPOPLPUSH
  • RPUSH
  • RPUSHX
  • LMOVE
  • BLMOVE
  • Pub/Sub
  • PSUBSCRIBE
  • PUBLISH
  • PUBSUB
  • PUNSUBSCRIBE
  • SUBSCRIBE
  • UNSUBSCRIBE
  • Set keys
  • SADD
  • SCARD
  • SDIFF
  • SDIFFSTORE
  • SINTER
  • SINTERSTORE
  • SINTERCARD
  • SISMEMBER
  • SMEMBERS
  • SMISMEMBER
  • SMOVE
  • SPOP -- see m.Seed(...)
  • SRANDMEMBER -- see m.Seed(...)
  • SREM
  • SSCAN
  • SUNION
  • SUNIONSTORE
  • Sorted Set keys
  • ZADD
  • ZCARD
  • ZCOUNT
  • ZINCRBY
  • ZINTER
  • ZINTERSTORE
  • ZLEXCOUNT
  • ZPOPMIN
  • ZPOPMAX
  • ZRANDMEMBER
  • ZRANGE
  • ZRANGEBYLEX
  • ZRANGEBYSCORE
  • ZRANK
  • ZREM
  • ZREMRANGEBYLEX
  • ZREMRANGEBYRANK
  • ZREMRANGEBYSCORE
  • ZREVRANGE
  • ZREVRANGEBYLEX
  • ZREVRANGEBYSCORE
  • ZREVRANK
  • ZSCORE
  • ZUNION
  • ZUNIONSTORE
  • ZSCAN
  • Stream keys
  • XACK
  • XADD
  • XAUTOCLAIM
  • XCLAIM
  • XDEL
  • XGROUP CREATE
  • XGROUP CREATECONSUMER
  • XGROUP DESTROY
  • XGROUP DELCONSUMER
  • XINFO STREAM -- partly
  • XINFO GROUPS
  • XINFO CONSUMERS -- partly
  • XLEN
  • XRANGE
  • XREAD
  • XREADGROUP
  • XREVRANGE
  • XPENDING
  • XTRIM
  • Scripting
  • EVAL
  • EVALSHA
  • SCRIPT LOAD
  • SCRIPT EXISTS
  • SCRIPT FLUSH
  • GEO
  • GEOADD
  • GEODIST
  • ~~GEOHASH~~
  • GEOPOS
  • GEORADIUS
  • GEORADIUS_RO
  • GEORADIUSBYMEMBER
  • GEORADIUSBYMEMBER_RO
  • Cluster
  • CLUSTER SLOTS
  • CLUSTER KEYSLOT
  • CLUSTER NODES
  • CLUSTER SHARDS
  • HyperLogLog
  • PFADD
  • PFCOUNT
  • PFMERGE

TTLs, key expiration, and time

Since miniredis is intended to be used in unittests TTLs don't decrease automatically. You can use TTL() to get the TTL (as a time.Duration) of a key. It will return 0 when no TTL is set.

m.FastForward(d) can be used to decrement all TTLs. All TTLs which become <= 0 will be removed.

EXPIREAT and PEXPIREAT values will be converted to a duration. For that you can either set m.SetTime(t) to use that time as the base for the (P)EXPIREAT conversion, or don't call SetTime(), in which case time.Now() will be used.

SetTime() also sets the value returned by TIME, which defaults to time.Now(). It is not updated by FastForward, only by SetTime.

Randomness and Seed()

Miniredis will use math/rand's global RNG for randomness unless a seed is provided by calling m.Seed(...). If a seed is provided, then miniredis will use its own RNG based on that seed.

Commands which use randomness are: RANDOMKEY, SPOP, and SRANDMEMBER.

Example


import (
    ...
    "github.com/alicebob/miniredis/v2"
    ...
)

func TestSomething(t *testing.T) {
    s := miniredis.RunT(t)

    // Optionally set some keys your code expects:
    s.Set("foo", "bar")
    s.HSet("some", "other", "key")

    // Run your code and see if it behaves.
    // An example using the redigo library from "github.com/gomodule/redigo/redis":
    c, err := redis.Dial("tcp", s.Addr())
    _, err = c.Do("SET", "foo", "bar")

    // Optionally check values in redis...
    if got, err := s.Get("foo"); err != nil || got != "bar" {
        t.Error("'foo' has the wrong value")
    }
    // ... or use a helper for that:
    s.CheckGet(t, "foo", "bar")

    // TTL and expiration:
    s.Set("foo", "bar")
    s.SetTTL("foo", 10*time.Second)
    s.FastForward(11 * time.Second)
    if s.Exists("foo") {
        t.Fatal("'foo' should not have existed anymore")
    }
}

Not supported

Commands which will probably not be implemented:

  • CLUSTER (all)
    • ~~CLUSTER *~~
    • ~~READONLY~~
    • ~~READWRITE~~
  • Key
    • ~~MIGRATE~~
    • ~~OBJECT~~
  • Scripting
    • ~~FCALL / FCALL_RO *~~
    • ~~FUNCTION *~~
    • ~~SCRIPT DEBUG~~
    • ~~SCRIPT KILL~~
  • Server
    • ~~BGSAVE~~
    • ~~BGWRITEAOF~~
    • ~~CLIENT *~~
    • ~~CONFIG *~~
    • ~~DEBUG *~~
    • ~~LASTSAVE~~
    • ~~MONITOR~~
    • ~~ROLE~~
    • ~~SAVE~~
    • ~~SHUTDOWN~~
    • ~~SLAVEOF~~
    • ~~SLOWLOG~~
    • ~~SYNC~~

&c.

Integration tests are run against Redis 8.4.0. The ./integration subdir compares miniredis against a real redis instance.

The Redis 6 RESP3 protocol is supported. If there are problems, please open an issue.

If you want to test Redis Sentinel have a look at minisentinel.

A changelog is kept at CHANGELOG.md.

Go Reference

Extension points exported contracts — how you extend this code

Tester (Interface)
Tester is a minimal version of a testing.T
miniredis.go
T (Interface)
T is implemented by Testing.T
check.go
Cmd (FuncType)
Cmd is what Register expects
server/server.go
CmdOption (FuncType)
CmdOption is a function that configures command metadata
server/cmdmeta.go
Hook (FuncType)
Hook is can be added to run before every cmd. Return true if the command is done.
server/server.go
DisconnectHandler (FuncType)
(no doc)
server/server.go

Core symbols most depended-on inside this repo

Do
called by 2409
integration/test.go
Error
called by 1282
integration/test.go
Error
called by 696
proto/types.go
String
called by 687
proto/types.go
WriteError
called by 433
server/server.go
Int
called by 429
proto/types.go
Array
called by 407
proto/types.go
errWrongNumber
called by 283
redis.go

Shape

Function 654
Method 550
Struct 42
TypeAlias 17
FuncType 6
Interface 3

Languages

Go100%

Modules by API surface

direct.go97 symbols
miniredis.go75 symbols
server/server.go57 symbols
db.go53 symbols
cmd_sorted_set.go37 symbols
cmd_stream.go33 symbols
stream.go32 symbols
cmd_string.go32 symbols
cmd_generic.go30 symbols
geohash/geohash.go27 symbols
cmd_list_test.go27 symbols
cmd_string_test.go26 symbols

Dependencies from manifests, versioned

For agents

$ claude mcp add miniredis \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact