Errors Logo

 

made-with-Go GoDoc Test codecov GoReportCard

RediGo

A Redis client for GoLang featuring Tags with Gob & JSON encoding.

Install

go get -u github.com/ainsleyclark/redigo

Quick Start

See below for a quick start to create a new Redis Client with an encoder. For more client methods see the Go Doc which includes all the client methods.

func ExampleClient() {
	ctx := context.Background()

	c := redigo.New(&redis.Options{}, redigo.NewGobEncoder())
	err := c.Ping(ctx)
	if err != nil {
		log.Fatalln(err)
	}

	err = c.Set(ctx, "my-key", "hello", redigo.Options{
		Expiration: time.Second * 100,
		Tags:       []string{"my-tag"},
	})
	if err != nil {
		log.Fatalln(err)
	}

	var val string
	err = c.Get(ctx, "my-key", &val)
	if err != nil {
		log.Fatalln(err)
	}

	err = c.Delete(ctx, "my-key")
	if err != nil {
		log.Fatalln(err)
	}
}

Encoders

JSON

Use NewJSONEncoder() in the constructor when creating a new client.

c := redigo.New(&redis.Options{}, redigo.NewJSONEncoder())

Gob

Use NewGobEncoder() in the constructor when creating a new client.

c := redigo.New(&redis.Options{}, redigo.NewGobEncoder())

Custom

You can pass in custom encoders to the client constructor. Below is a message pack example. Using github.com/vmihailenco/msgpack

import "github.com/vmihailenco/msgpack/v5"

type MessagePack struct{}

func (m MessagePack) Encode(value any) ([]byte, error) {
	return msgpack.Marshal(value)
}

func (m MessagePack) Decode(data []byte, value any) error {
	return msgpack.Unmarshal(data, value)
}

func ExampleMessagePack() {
	c := redigo.New(&redis.Options{}, &MessagePack{})
}

TODO

  • Benchmarks

Credits

Shout out to the incredible Maria Letta for her excellent Gopher illustrations

GitHub

View Github