redisson

A Type-safe Golang Redis RESP2/RESP3 client.

Features

  • Check Redis commands version in development mode.
  • Check Redis deprecated commands in development mode.
  • Check Redis slot when use multiple keys in development mode.
  • Check forbid Redis commands in development mode.
  • Monitoring cost of Redis commands.
  • Monitoring status of connections.
  • Monitoring hits/miss of Redis RESP3 client side caching.
  • Support Redis RESP2/RESP3.
  • Opt-in client side caching.
  • Auto pipeline for non-blocking Redis RESP3 commands.
  • Connection pooling for blocking Redis RESP3 commands.

Requirement

  • Currently, only supports Redis < 7.x
  • Golang >= 1.8

If you can’t upgrade Golang to 1.8, install redisson/version/0.1.

Links

Getting Started

package main

import (
	"context"
	"github.com/sandwich-go/redisson"
)

func main() {
	c := redisson.MustNewClient(redisson.NewConf(
	      redisson.WithResp(redisson.RESP3), 
	      redisson.WithDevelopment(false), 
	))
	defer c.Close()

	ctx := context.Background()

	// SET key val NX
	_ = c.SetNX(ctx, "key", "val", 0).Err()
	// GET key
	_ = c.Get(ctx, "key").Val()
}

Check

Check version

If Redis < 6.0

c := redisson.MustNewClient(redisson.NewConf(
      redisson.WithResp(redisson.RESP3), 
      redisson.WithDevelopment(true), 
))
defer c.Close()

res := c.Set(ctx, "key", "10", -1)

Output:

Line 34: - redis 'SET KEEPTTL' are not supported in version "5.0.0", available since 6.0.0

Check deprecated

If Redis >= 4.0

c := redisson.MustNewClient(redisson.NewConf(
      redisson.WithResp(redisson.RESP3), 
      redisson.WithDevelopment(true), 
))
defer c.Close()

res := c.HMSet(ctx, "key", "10")

Output:

As of Redis version 4.0.0, this command is regarded as deprecated.
It can be replaced by HSET with multiple field-value pairs when migrating or writing new code.

Check slot for multiple keys

c := redisson.MustNewClient(redisson.NewConf(
      redisson.WithResp(redisson.RESP3), 
      redisson.WithDevelopment(true), 
))
defer c.Close()

res := c.MSet(ctx, "key1", "10", "key2", "20")

Output:

Line 34: - multi key command with different key slots are not allowed 

Check forbid

c := redisson.MustNewClient(redisson.NewConf(
      redisson.WithResp(redisson.RESP3), 
      redisson.WithDevelopment(true), 
))
defer c.Close()

res := c.ClusterFailover(ctx)

Output:

Line 34: - command 'CLUSTER FAILOVER' not allowed 

Monitor

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/sandwich-go/redisson"
)

var DefaultPrometheusRegistry = prometheus.NewRegistry()

c := redisson.MustNewClient(redisson.NewConf(
      redisson.WithResp(redisson.RESP3),
      redisson.WithDevelopment(true),
))
defer c.Close()

c.RegisterCollector(func(c prometheus.Collector) {
    DefaultPrometheusRegistry.Register(c)
})

Auto Pipeline

All non-blocking commands sending to a single Redis node are automatically pipelined through one tcp connection, which reduces the overall round trips and system calls, and gets higher throughput.

Notice: Only supports when use Redis RESP3 client.

Client Side Caching

The Opt-In mode of server-assisted client side caching is always enabled.

c.Cache(time.Minute).Get(ctx, "key").Val()

An explicit client side TTL is required because Redis server may not send invalidation message in time when a key is expired on the server. Please follow #6833 and #6867

Although an explicit client side TTL is required, the Cache() still sends a PTTL command to server and make sure that the client side TTL is not longer than the TTL on server side.

Notice: Only supports when use Redis RESP3 client.

GitHub

View Github