miniredis

package module
v2.32.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 26 Imported by: 204

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 (complete)
    • AUTH -- see RequireAuth()
    • ECHO
    • HELLO -- see RequireUserAuth()
    • PING
    • SELECT
    • SWAPDB
    • QUIT
  • Key
    • COPY
    • DEL
    • EXISTS
    • EXPIRE
    • EXPIREAT
    • EXPIRETIME
    • KEYS
    • MOVE
    • PERSIST
    • PEXPIRE
    • PEXPIREAT
    • PEXPIRETIME
    • PTTL
    • RANDOMKEY -- see m.Seed(...)
    • RENAME
    • RENAMENX
    • SCAN
    • TOUCH
    • TTL
    • TYPE
    • UNLINK
  • Transactions (complete)
    • 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 (complete)
    • APPEND
    • BITCOUNT
    • BITOP
    • BITPOS
    • DECR
    • DECRBY
    • GET
    • GETBIT
    • GETRANGE
    • GETSET
    • GETDEL
    • GETEX
    • INCR
    • INCRBY
    • INCRBYFLOAT
    • MGET
    • MSET
    • MSETNX
    • PSETEX
    • SET
    • SETBIT
    • SETEX
    • SETNX
    • SETRANGE
    • STRLEN
  • Hash keys (complete)
    • HDEL
    • HEXISTS
    • HGET
    • HGETALL
    • HINCRBY
    • HINCRBYFLOAT
    • HKEYS
    • HLEN
    • HMGET
    • HMSET
    • HRANDFIELD
    • HSET
    • HSETNX
    • HSTRLEN
    • HVALS
    • HSCAN
  • List keys (complete)
    • BLPOP
    • BRPOP
    • BRPOPLPUSH
    • LINDEX
    • LINSERT
    • LLEN
    • LPOP
    • LPUSH
    • LPUSHX
    • LRANGE
    • LREM
    • LSET
    • LTRIM
    • RPOP
    • RPOPLPUSH
    • RPUSH
    • RPUSHX
    • LMOVE
    • BLMOVE
  • Pub/Sub (complete)
    • PSUBSCRIBE
    • PUBLISH
    • PUBSUB
    • PUNSUBSCRIBE
    • SUBSCRIBE
    • UNSUBSCRIBE
  • Set keys (complete)
    • 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 (complete)
    • 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
  • HyperLogLog (complete)
    • 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
    • DUMP
    • MIGRATE
    • OBJECT
    • RESTORE
    • WAIT
  • Scripting
    • 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 7.2.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

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.

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

Start a server with `s := miniredis.RunT(t)`, it'll be shutdown via a t.Cleanup(). Or do everything manual: `s, err := miniredis.Run(); 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")`.

Index

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)

	// ErrNotValidHllValue when a key is not a valid HyperLogLog string value.
	ErrNotValidHllValue = errors.New(msgNotValidHllValue)

	// ErrIntValueError can returned by INCRBY
	ErrIntValueError = errors.New(msgInvalidInt)

	// ErrFloatValueError can returned by INCRBYFLOAT
	ErrFloatValueError = errors.New(msgInvalidFloat)
)
View Source
var DumpMaxLineLen = 60

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 Run

func Run() (*Miniredis, error)

Run creates and Start()s a Miniredis.

func RunT added in v2.17.0

func RunT(t Tester) *Miniredis

RunT start a new miniredis, pass it a testing.T. It also registers the cleanup after your test is done.

func RunTLS added in v2.13.0

func RunTLS(cfg *tls.Config) (*Miniredis, error)

Run creates and Start()s a Miniredis, TLS version.

func (*Miniredis) Addr

func (m *Miniredis) 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 (*Miniredis) CheckGet

func (m *Miniredis) CheckGet(t T, key, expected string)

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

func (m *Miniredis) 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 (*Miniredis) CheckSet

func (m *Miniredis) 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 (*Miniredis) Close

func (m *Miniredis) Close()

Close shuts down a Miniredis.

func (*Miniredis) CommandCount

func (m *Miniredis) CommandCount() int

CommandCount returns the number of processed commands.

func (*Miniredis) Copy added in v2.18.0

func (m *Miniredis) Copy(srcDB int, src string, destDB int, dest string) error

Copy a value. Needs the IDs of both the source and dest DBs (which can differ). Returns ErrKeyNotFound if src does not exist. Overwrites dest if it already exists (unlike the redis command, which needs a flag to allow that).

func (*Miniredis) CurrentConnectionCount

func (m *Miniredis) CurrentConnectionCount() int

CurrentConnectionCount returns the number of currently connected clients.

func (*Miniredis) DB

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

DB returns a DB by ID.

func (*Miniredis) Del

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

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

func (*Miniredis) Dump

func (m *Miniredis) Dump() string

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

Dump limits the maximum length of each key:value to "DumpMaxLineLen" characters. To increase that, call something like:

miniredis.DumpMaxLineLen = 1024
mr, _ = miniredis.Run()
mr.Dump()

func (*Miniredis) Exists

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

Exists tells whether a key exists.

func (*Miniredis) FastForward

func (m *Miniredis) FastForward(duration time.Duration)

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) Get

func (m *Miniredis) Get(k string) (string, error)

Get returns string keys added with SET.

func (*Miniredis) HDel

func (m *Miniredis) HDel(k, f string)

HDel deletes a hash key.

func (*Miniredis) HGet

func (m *Miniredis) HGet(k, f string) string

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) HIncr

func (m *Miniredis) HIncr(k, f string, delta int) (int, error)

HIncr increases a key/field by delta (int).

func (*Miniredis) HIncrBy added in v2.10.0

func (m *Miniredis) HIncrBy(k, f string, delta int) (int, error)

HIncrBy increases the integer value of a hash field by delta (int).

func (*Miniredis) HIncrByFloat added in v2.10.0

func (m *Miniredis) HIncrByFloat(k, f string, delta float64) (float64, error)

HIncrByFloat increases a key/field by delta (float).

func (*Miniredis) HIncrfloat

func (m *Miniredis) HIncrfloat(k, f string, delta float64) (float64, error)

HIncrfloat increases a key/field by delta (float).

func (*Miniredis) HKeys

func (m *Miniredis) HKeys(k string) ([]string, error)

HKeys returns all (sorted) keys ('fields') for a hash key.

func (*Miniredis) HSet

func (m *Miniredis) HSet(k string, fv ...string)

HSet sets hash keys. If there is another key by the same name it will be gone.

func (*Miniredis) Host

func (m *Miniredis) Host() string

Host returns the host part of Addr().

func (*Miniredis) Incr

func (m *Miniredis) Incr(k string, delta int) (int, error)

Incr changes a int string value by delta.

func (*Miniredis) IncrByFloat added in v2.10.0

func (m *Miniredis) IncrByFloat(k string, delta float64) (float64, error)

IncrByFloat increments the float value of a key by the given delta. is an alias for Miniredis.Incrfloat

func (*Miniredis) Incrfloat

func (m *Miniredis) Incrfloat(k string, delta float64) (float64, error)

Incrfloat changes a float string value by delta.

func (*Miniredis) IsMember

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

IsMember tells if value is in the set.

func (*Miniredis) Keys

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

Keys returns all keys from the selected database, sorted.

func (*Miniredis) List

func (m *Miniredis) 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 (*Miniredis) Lpop

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

Lpop removes and returns the last element in a list.

func (*Miniredis) Lpush

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

Lpush prepends one value to a list. Returns the new length.

func (*Miniredis) Members

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

Members returns all keys in a set, sorted.

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) PfAdd added in v2.15.0

func (m *Miniredis) PfAdd(k string, elems ...string) (int, error)

PfAdd adds keys to a hll. Returns the flag which equals to 1 if the inner hll value has been changed.

func (*Miniredis) PfCount added in v2.15.0

func (m *Miniredis) PfCount(keys ...string) (int, error)

PfCount returns an estimation of the amount of elements previously added to a hll.

func (*Miniredis) PfMerge added in v2.15.0

func (m *Miniredis) PfMerge(destKey string, sourceKeys ...string) error

PfMerge merges all the input hlls into a hll under destKey key.

func (*Miniredis) Pop

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

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

func (*Miniredis) Port

func (m *Miniredis) Port() string

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

func (*Miniredis) PubSubChannels

func (m *Miniredis) PubSubChannels(pattern string) []string

PubSubChannels is "PUBSUB CHANNELS <pattern>". An empty pattern is fine (meaning all channels). Returned channels will be ordered alphabetically.

func (*Miniredis) PubSubNumPat

func (m *Miniredis) PubSubNumPat() int

PubSubNumPat is "PUBSUB NUMPAT"

func (*Miniredis) PubSubNumSub

func (m *Miniredis) PubSubNumSub(channels ...string) map[string]int

PubSubNumSub is "PUBSUB NUMSUB [channels]". It returns all channels with their subscriber count.

func (*Miniredis) Publish

func (m *Miniredis) Publish(channel, message string) int

Publish a message to subscribers. Returns the number of receivers.

func (*Miniredis) Push

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

Push add element at the end. Returns the new length.

func (*Miniredis) RPop added in v2.10.0

func (m *Miniredis) RPop(k string) (string, error)

RPop is an alias for Pop

func (*Miniredis) RPush added in v2.10.0

func (m *Miniredis) RPush(k string, v ...string) (int, error)

RPush appends one or multiple values to a list. Returns the new length. An alias for Push

func (*Miniredis) RequireAuth

func (m *Miniredis) RequireAuth(pw string)

RequireAuth makes every connection need to AUTH first. This is the old 'AUTH [password] command. Remove it by setting an empty string.

func (*Miniredis) RequireUserAuth added in v2.13.1

func (m *Miniredis) RequireUserAuth(username, pw string)

Add a username/password, for use with 'AUTH [username] [password]'. There are currently no access controls for commands implemented. Disable access for the user with an empty password.

func (*Miniredis) Restart

func (m *Miniredis) Restart() error

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

func (*Miniredis) SAdd added in v2.10.0

func (m *Miniredis) SAdd(k string, elems ...string) (int, error)

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

func (*Miniredis) SIsMember added in v2.10.0

func (m *Miniredis) SIsMember(k, v string) (bool, error)

SIsMember tells if value is in the set. Alias for IsMember

func (*Miniredis) SMembers added in v2.10.0

func (m *Miniredis) SMembers(k string) ([]string, error)

SMembers returns all keys in a set, sorted. Alias for Members.

func (*Miniredis) SRem

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

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

func (*Miniredis) Seed added in v2.9.0

func (m *Miniredis) Seed(seed int)

func (*Miniredis) Select

func (m *Miniredis) Select(i int)

Select sets the DB id for all direct commands.

func (*Miniredis) Server added in v2.10.1

func (m *Miniredis) Server() *server.Server

Server returns the underlying server to allow custom commands to be implemented

func (*Miniredis) Set

func (m *Miniredis) Set(k, v string) error

Set sets a string key. Removes expire.

func (*Miniredis) SetAdd

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

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

func (*Miniredis) SetError added in v2.13.0

func (m *Miniredis) SetError(msg string)

make every command return this message. For example:

LOADING Redis is loading the dataset in memory
MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'.

Clear it with an empty string. Don't add newlines.

func (*Miniredis) SetTTL

func (m *Miniredis) SetTTL(k string, ttl time.Duration)

SetTTL sets the TTL of a key.

func (*Miniredis) SetTime

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

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) SortedSet

func (m *Miniredis) SortedSet(k string) (map[string]float64, error)

SortedSet returns a raw string->float64 map.

func (*Miniredis) Start

func (m *Miniredis) Start() error

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

func (*Miniredis) StartAddr

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

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

func (*Miniredis) StartTLS added in v2.13.0

func (m *Miniredis) StartTLS(cfg *tls.Config) error

Start starts a server, TLS version.

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) SwapDB

func (m *Miniredis) SwapDB(i, j int) bool

SwapDB swaps DBs by IDs.

func (*Miniredis) TTL

func (m *Miniredis) TTL(k string) time.Duration

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

func (m *Miniredis) TotalConnectionCount() int

TotalConnectionCount returns the number of client connections since server start.

func (*Miniredis) Type

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

Type gives the type of a key, or ""

func (m *Miniredis) Unlink(k string) bool

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

func (m *Miniredis) XAdd(k string, id string, values []string) (string, error)

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.

func (*Miniredis) ZAdd

func (m *Miniredis) ZAdd(k string, score float64, member string) (bool, error)

ZAdd adds a score,member to a sorted set.

func (*Miniredis) ZMScore added in v2.30.3

func (m *Miniredis) ZMScore(k string, members ...string) ([]float64, error)

ZScore gives scores of a list of members in a sorted set.

func (*Miniredis) ZMembers

func (m *Miniredis) ZMembers(k string) ([]string, error)

ZMembers returns all members of a sorted set by score

func (*Miniredis) ZRem

func (m *Miniredis) ZRem(k, member string) (bool, error)

ZRem deletes a member. Returns whether the was a key.

func (*Miniredis) ZScore

func (m *Miniredis) ZScore(k, member string) (float64, error)

ZScore gives the score of a sorted set member.

type PubsubMessage

type PubsubMessage struct {
	Channel string
	Message string
}

PubsubMessage is what gets broadcasted over pubsub channels.

type PubsubPmessage added in v2.9.0

type PubsubPmessage struct {
	Pattern string
	Channel string
	Message string
}

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) Get

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

Get returns a string key.

func (*RedisDB) HDel

func (db *RedisDB) HDel(k, f string)

HDel deletes a hash key.

func (*RedisDB) HGet

func (db *RedisDB) HGet(k, f string) string

HGet returns hash keys added with HSET. Returns empty string when the key is of a different type.

func (*RedisDB) HIncr

func (db *RedisDB) HIncr(k, f string, delta int) (int, error)

HIncr increases a key/field by delta (int).

func (*RedisDB) HIncrfloat

func (db *RedisDB) HIncrfloat(k, f string, delta float64) (float64, error)

HIncrfloat increases a key/field by delta (float).

func (*RedisDB) HKeys

func (db *RedisDB) HKeys(key string) ([]string, error)

HKeys returns all (sorted) keys ('fields') for a hash key.

func (*RedisDB) HSet

func (db *RedisDB) HSet(k string, fv ...string)

HSet sets hash keys. If there is another key by the same name it will be gone.

func (*RedisDB) HllAdd added in v2.15.0

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

HllAdd adds keys to a hll. Returns the flag which equals to true if the inner hll value has been changed.

func (*RedisDB) HllCount added in v2.15.0

func (db *RedisDB) HllCount(keys ...string) (int, error)

HllCount returns an estimation of the amount of elements previously added to a hll.

func (*RedisDB) HllMerge added in v2.15.0

func (db *RedisDB) HllMerge(destKey string, sourceKeys ...string) error

HllMerge merges all the input hlls into a hll under destKey key.

func (*RedisDB) Incr

func (db *RedisDB) Incr(k string, delta int) (int, error)

Incr changes a int string value by delta.

func (*RedisDB) Incrfloat

func (db *RedisDB) Incrfloat(k string, delta float64) (float64, error)

Incrfloat changes a float string value by delta.

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 removes and returns the last element in a list.

func (*RedisDB) Lpush

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

Lpush prepends one value to a list. 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) Set

func (db *RedisDB) Set(k, v string) error

Set sets a string key. Removes expire. Unlike redis the key can't be an existing non-string key.

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) SetTTL

func (db *RedisDB) SetTTL(k string, ttl time.Duration)

SetTTL sets the time to live of a key.

func (*RedisDB) SortedSet

func (db *RedisDB) SortedSet(k string) (map[string]float64, error)

SortedSet returns a raw string->float64 map.

func (*RedisDB) Stream added in v2.11.0

func (db *RedisDB) Stream(key string) ([]StreamEntry, error)

Stream returns a slice of stream entries. Oldest first.

func (*RedisDB) TTL

func (db *RedisDB) TTL(k string) time.Duration

TTL is the left over time to live. As set via EXPIRE, PEXPIRE, EXPIREAT, PEXPIREAT. 0 if not set.

func (*RedisDB) Type

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

Type gives the type of a key, or ""

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

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

func (db *RedisDB) XAdd(k string, id string, values []string) (string, error)

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.

func (*RedisDB) ZAdd

func (db *RedisDB) ZAdd(k string, score float64, member string) (bool, error)

ZAdd adds a score,member to a sorted set.

func (*RedisDB) ZMScore added in v2.30.3

func (db *RedisDB) ZMScore(k string, members []string) ([]float64, error)

func (*RedisDB) ZMembers

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

ZMembers returns all members of a sorted set by score

func (*RedisDB) ZRem

func (db *RedisDB) ZRem(k, member string) (bool, error)

ZRem deletes a member. Returns whether the was a key.

func (*RedisDB) ZScore

func (db *RedisDB) ZScore(k, member string) (float64, error)

ZScore gives the score of a sorted set member.

type StreamEntry added in v2.11.0

type StreamEntry struct {
	ID     string
	Values []string
}

a StreamEntry is an entry in a stream. The ID is always of the form "123-123". Values is an ordered list of key-value pairs.

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) Close

func (s *Subscriber) Close()

Close the listening channel

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.

type T

type T interface {
	Helper()
	Errorf(string, ...interface{})
}

T is implemented by Testing.T

type Tester added in v2.17.0

type Tester interface {
	Fatalf(string, ...interface{})
	Cleanup(func())
}

Tester is a minimal version of a testing.T

Directories

Path Synopsis
Package geohash provides encoding and decoding of string and integer geohashes.
Package geohash provides encoding and decoding of string and integer geohashes.

Jump to

Keyboard shortcuts

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