Skip to content

Commit

Permalink
Merge pull request #47 from vtex/feature/redis-on-cluster-mode
Browse files Browse the repository at this point in the history
Allow to have Redis on cluster mode
  • Loading branch information
fabianaferreira authored Jan 3, 2023
2 parents 51f6ccf + 300a233 commit 0ef9ccf
Show file tree
Hide file tree
Showing 915 changed files with 332,528 additions and 14 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/die-net/lrucache v0.0.0-20171111232917-04b9315ab7a6
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
github.com/gin-gonic/gin v0.0.0-20171121131845-eeb57848cac0
github.com/go-redis/redis/v8 v8.11.5
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v0.0.0-20180917234931-6e3d092c77c3 // indirect
github.com/gomodule/redigo v0.0.0-20190322064113-39e2c31b7ca3
github.com/google/gofuzz v1.1.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20170128230107-5c8f21741620 // indirect
Expand All @@ -28,9 +28,8 @@ require (
github.com/smartystreets/assertions v0.0.0-20161110225557-e60cfa771e3f // indirect
github.com/smartystreets/goconvey v0.0.0-20160928205523-7befa7fd6e2e
github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83 // indirect
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.0.0-20171116090243-287cf08546ab // indirect
)
118 changes: 109 additions & 9 deletions go.sum

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions redis/cache.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package redis

import (
"context"
"encoding/json"
"fmt"
"strings"

"time"

redisCluster "github.com/go-redis/redis/v8"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"github.com/vtex/go-io/cache"
Expand All @@ -21,6 +23,7 @@ type TimeTracker func(kpiName string, startTime time.Time)

type RedisConfig struct {
Endpoint string
ClusterMode bool
KeyNamespace string
TimeTracker TimeTracker
MaxIdleConns int
Expand Down Expand Up @@ -51,6 +54,19 @@ func New(conf RedisConfig) Cache {
conf.MaxActiveConns = 20
}

if conf.ClusterMode {
cluster := redisCluster.NewClusterClient(
&redisCluster.ClusterOptions{
Addrs: []string{conf.Endpoint},
ReadTimeout: 200 * time.Millisecond,
WriteTimeout: 200 * time.Millisecond,
DialTimeout: 1 * time.Second,
},
)

return &redisC{cluster: cluster, conf: conf}
}

pool := newRedisPool(conf.Endpoint, poolOptions{
MaxIdle: conf.MaxIdleConns,
MaxActive: conf.MaxActiveConns,
Expand All @@ -60,8 +76,9 @@ func New(conf RedisConfig) Cache {
}

type redisC struct {
pool *redis.Pool
conf RedisConfig
pool *redis.Pool
cluster *redisCluster.ClusterClient
conf RedisConfig
}

func (r *redisC) Get(key string, result interface{}) (bool, error) {
Expand Down Expand Up @@ -181,6 +198,16 @@ func (r *redisC) remoteKey(key string) (string, error) {
}

func (r *redisC) doCmd(cmd string, args ...interface{}) (interface{}, error) {
if r.cluster != nil {
defer r.conf.TimeTracker(commandKpiName(cmd), time.Now())
result, err := r.cluster.Do(context.Background(), append([]interface{}{cmd}, args...)...).Result()
if err == redisCluster.Nil {
return result, nil
}

return result, err
}

conn := r.getConnection()
defer r.closeConnection(conn)

Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0ef9ccf

Please sign in to comment.