A Redis client for GoLang featuring Tags with Gob & JSON encoding.
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.
go get -u github.com/ainsleyclark/redigo
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)
}
}
c := redigo.New(&redis.Options{}, redigo.NewJSONEncoder())
c := redigo.New(&redis.Options{}, redigo.NewGobEncoder())
See github.com/vmihailenco/msgpack for more details.
c := redigo.New(&redis.Options{}, redigo.NewMessagePackEncoder())
See github.com/goccy/go-json for more details.
c := redigo.New(&redis.Options{}, redigo.NewMessagePackEncoder())
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{})
}
$ go version
go version go1.18.2 darwin/amd64
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
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
Please feel free to make a pull request if you think something should be added to this package!
Shout out to the incredible Maria Letta for her excellent Gopher illustrations.
Code Copyright 2022 RediGo. Code released under the MIT Licence.