Skip to content

Commit

Permalink
Remove direct dependency of clients for obtaining connection (#62)
Browse files Browse the repository at this point in the history
* Change go-redis client to universal client interface #53 

* Abstracts clients to support different types of redis connections

* Adding support for different types of connections provided by libraries #63
  • Loading branch information
Shivam010 authored Feb 17, 2022
1 parent 16e335f commit 0f5ba3d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
11 changes: 8 additions & 3 deletions clients/goredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import (
"github.com/nitishm/go-rejson/v4/rjs"
)

// GoRedisClientConn - an abstracted interface for goredis.Client, goredis.ClusterClient, goredis.Ring,
// or goredis.UniversalClient
type GoRedisClientConn interface {
Do(ctx context.Context, args ...interface{}) *goredis.Cmd
}

// GoRedis implements ReJSON interface for Go-Redis/Redis Redis client
// Link: https://github.com/go-redis/redis
type GoRedis struct {
Conn *goredis.Client // import goredis "github.com/go-redis/redis/v8"

Conn GoRedisClientConn
// ctx defines context for the provided connection
ctx context.Context
}

// NewGoRedisClient returns a new GoRedis ReJSON client with the provided context
// and connection, if ctx is nil default context.Background will be used
func NewGoRedisClient(ctx context.Context, conn *goredis.Client) *GoRedis {
func NewGoRedisClient(ctx context.Context, conn GoRedisClientConn) *GoRedis {
if ctx == nil {
ctx = context.Background()
}
Expand Down
8 changes: 6 additions & 2 deletions clients/redigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package clients

import (
"fmt"
redigo "github.com/gomodule/redigo/redis"
"github.com/nitishm/go-rejson/v4/rjs"
"strings"
)

// RedigoClientConn - an abstracted interface for redigo.Conn and redigo.ConnWithTimeout
type RedigoClientConn interface {
Do(commandName string, args ...interface{}) (reply interface{}, err error)
}

// Redigo implements ReJSON interface for GoModule/Redigo Redis client
// Link: https://github.com/gomodule/redigo
type Redigo struct {
Conn redigo.Conn // import redigo "github.com/gomodule/redigo"
Conn RedigoClientConn
}

// JSONSet used to set a json object
Expand Down
9 changes: 6 additions & 3 deletions rejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rejson
import (
"context"
"encoding/json"
"github.com/nitishm/go-rejson/v4/clients"
"reflect"
"testing"

Expand Down Expand Up @@ -45,7 +46,9 @@ func (t *TestClient) init() []helper {
}

// GoRedis Test Client
goredisCli := goredis.NewClient(&goredis.Options{Addr: "localhost:6379"})
goredisCli := goredis.NewUniversalClient(&goredis.UniversalOptions{
Addrs: []string{"localhost:6379"},
})

return []helper{
{cli: redigoCli, name: "Redigo ", closeFunc: func() {
Expand Down Expand Up @@ -73,10 +76,10 @@ func (t *TestClient) SetTestingClient(conn interface{}) {
t.conn = conn

switch conn := conn.(type) {
case redigo.Conn:
case clients.RedigoClientConn:
t.name = "Redigo-"
t.rh.SetRedigoClient(conn)
case *goredis.Client:
case clients.GoRedisClientConn:
t.name = "GoRedis-"
t.rh.SetGoRedisClient(conn)
default:
Expand Down
2 changes: 1 addition & 1 deletion rjs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (g GetOption) Value() []interface{} {
}

// SetValue will set the values in the options
func (g GetOption) SetValue(arg string) {
func (g *GetOption) SetValue(arg string) {
g.Arg = arg
}

Expand Down
14 changes: 6 additions & 8 deletions set_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package rejson

import (
"context"
goredis "github.com/go-redis/redis/v8"
redigo "github.com/gomodule/redigo/redis"
"github.com/nitishm/go-rejson/v4/clients"
"github.com/nitishm/go-rejson/v4/rjs"
)

// RedisClient provides interface for Client handling in the ReJSON Handler
type RedisClient interface {
SetClientInactive()
SetRedigoClient(redigo.Conn)
SetGoRedisClient(conn *goredis.Client)
SetRedigoClient(conn clients.RedigoClientConn)
SetGoRedisClient(conn clients.GoRedisClientConn)
}

// SetClientInactive resets the handler and unset any client, set to the handler
Expand All @@ -24,20 +22,20 @@ func (r *Handler) SetClientInactive() {

// SetRedigoClient sets Redigo (https://github.com/gomodule/redigo/redis) client
// to the handler
func (r *Handler) SetRedigoClient(conn redigo.Conn) {
func (r *Handler) SetRedigoClient(conn clients.RedigoClientConn) {
r.clientName = "redigo"
r.implementation = &clients.Redigo{Conn: conn}
}

// SetGoRedisClient sets Go-Redis (https://github.com/go-redis/redis) client to
// the handler. It is left for backward compatibility.
func (r *Handler) SetGoRedisClient(conn *goredis.Client) {
r.SetGoRedisClientWithContext(context.TODO(), conn)
func (r *Handler) SetGoRedisClient(conn clients.GoRedisClientConn) {
r.SetGoRedisClientWithContext(context.Background(), conn)
}

// SetGoRedisClientWithContext sets Go-Redis (https://github.com/go-redis/redis) client to
// the handler with a global context for the connection
func (r *Handler) SetGoRedisClientWithContext(ctx context.Context, conn *goredis.Client) {
func (r *Handler) SetGoRedisClientWithContext(ctx context.Context, conn clients.GoRedisClientConn) {
r.clientName = "goredis"
r.implementation = clients.NewGoRedisClient(ctx, conn)
}

0 comments on commit 0f5ba3d

Please sign in to comment.