Skip to content

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

License

Notifications You must be signed in to change notification settings

ainsleyclark/redigo

Repository files navigation

Errors Logo

 

made-with-Go GoDoc Test codecov GoReportCard

🔥 RediGo

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

Why?

RediGo is a wrapper for the Redis V8 GoLang client that features tagging, expiration and automatic encoding and decoding using various encoders. It helps to unify various encoding techniques with a simple and easy to user interface.

Gob encoding performs drastically better in comparison to JSON which you can see from the benchmarks below.

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) // Be sure to pass a reference!
	if err != nil {
		log.Fatalln(err)
	}

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

Encoders

JSON

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

Gob

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

Message Pack

See github.com/vmihailenco/msgpack for more details.

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

Go JSON

See github.com/goccy/go-json for more details.

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

Custom

You can pass in custom encoders to the client constructor, that implement the Encode and Decode methods.

type MyEncoder struct{}

func (m MessagePack) Encode(value any) ([]byte, error) {
	// Marshal or encode value
	return []byte("hello"), nil
}

func (m MessagePack) Decode(data []byte, value any) error {
	// Unmarshal or decode value
	return nil
}

func ExampleCustom() {
	c := redigo.New(&redis.Options{}, &MyEncoder{})
}

Benchmarks

$ go version
go version go1.18.2 darwin/amd64

Encode

BenchmarkEncode/JSON-16                    54728             21813 ns/op            9294 B/op        206 allocs/op
BenchmarkEncode/Gob-16                    154272              7629 ns/op            4304 B/op        220 allocs/op
BenchmarkEncode/Message_Pack-16           113059             10468 ns/op            6820 B/op        208 allocs/op
BenchmarkEncode/Go_JSON-16                 92598             12768 ns/op             897 B/op          1 allocs/op

Graph representing ns/op.

Encoding Benchmark Graph

Decode

BenchmarkDecode/JSON/-16                   39386             30318 ns/op            7246 B/op        302 allocs/op
BenchmarkDecode/Gob/-16                    57792             20742 ns/op           12733 B/op        193 allocs/op
BenchmarkDecode/Message_Pack/-16           57416             20626 ns/op            7217 B/op        220 allocs/op
BenchmarkDecode/Go_JSON/-16                95376             12186 ns/op            8068 B/op        220 allocs/op

Graph representing ns/op.

Decoding Benchmark Graph

Contributing

Please feel free to make a pull request if you think something should be added to this package!

Credits

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

Licence

Code Copyright 2022 RediGo. Code released under the MIT Licence.