diff --git a/redis/README.md b/redis/README.md index 8f58f13d..dbb1a058 100644 --- a/redis/README.md +++ b/redis/README.md @@ -17,7 +17,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error func (s *Storage) Delete(key string) error func (s *Storage) Reset() error func (s *Storage) Close() error -func (s *Storage) Conn() *redis.Client +func (s *Storage) Conn() redis.UniversalClient ``` ### Installation Redis is tested on the 2 last [Go versions](https://golang.org/dl/) with support for modules. So make sure to initialize one first if you didn't do that yet: @@ -46,21 +46,21 @@ store := redis.New(redis.Config{ Port: 6379, Username: "", Password: "", - URL: "", Database: 0, Reset: false, TLSConfig: nil, PoolSize: 10 * runtime.GOMAXPROCS(0), }) -// Initialize Redis Sentinel Server Client +// Initialize Redis Failover Client store := redis.New(redis.Config{ - EnableFailover: true, MasterName: "master-name", - SentinelHosts: []string{":6379", ":6380", ":6381"}, - ClientName: "", - SentinelUsername: "", - SentinelPassword: "", + Addrs: []string{":6379"}, +}) + +// Initialize Redis Cluster Client +store := redis.New(redis.Config{ + Addrs: []string{":6379", ":6380"}, }) // or just the url with all information @@ -98,12 +98,37 @@ type Config struct { // Optional. Default is 0 Database int - // URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect. + // URL standard format Redis URL. If this is set all other config options, Host, Port, Username, Password, Database have no effect. // // Example: redis://:@localhost:6379/ // Optional. Default is "" URL string + // Either a single address or a seed list of host:port addresses, this enables FailoverClient and ClusterClient + // + // Optional. Default is []string{} + Addrs []string + + // MasterName is the sentinel master's name + // + // Optional. Default is "" + MasterName string + + // ClientName will execute the `CLIENT SETNAME ClientName` command for each conn. + // + // Optional. Default is "" + ClientName string + + // SentinelUsername + // + // Optional. Default is "" + SentinelUsername string + + // SentinelPassword + // + // Optional. Default is "" + SentinelPassword string + // Reset clears any existing keys in existing Collection // // Optional. Default is false @@ -118,52 +143,25 @@ type Config struct { // // Optional. Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS. PoolSize int - - // EnableFailover to use redis FailoverClient with Sentinel instead of the standard redis Client - // - // Optional. Default is false - EnableFailover bool - - // MasterName is the sentinel master's name - // - // Optional. Default is "" - MasterName string - - // SentinelHosts where the Redis Sentinel is hosted - // - // Optional. Default is []string{} - SentinelHosts []string - - // SentinelUsername - // - // Optional. Default is "" - SentinelUsername string - - // SentinelPassword - // - // Optional. Default is "" - SentinelPassword string } - ``` ### Default Config ```go var ConfigDefault = Config{ - Host: "127.0.0.1", - Port: 6379, - Username: "", - Password: "", - URL: "", - Database: 0, - Reset: false, - TLSConfig: nil, - PoolSize: 10 * runtime.GOMAXPROCS(0), - EnableFailover: false, - MasterName: "", - SentinelHosts: []string{}, - ClientName: "", - SentinelUsername: "", - SentinelPassword: "", + Host: "127.0.0.1", + Port: 6379, + Username: "", + Password: "", + URL: "", + Database: 0, + Reset: false, + TLSConfig: nil, + PoolSize: 10 * runtime.GOMAXPROCS(0), + Addrs: []string{}, + MasterName: "", + ClientName: "", + SentinelUsername: "", + SentinelPassword: "", } ``` diff --git a/redis/config.go b/redis/config.go index 976bc6a9..38212767 100644 --- a/redis/config.go +++ b/redis/config.go @@ -81,20 +81,20 @@ type Config struct { // ConfigDefault is the default config var ConfigDefault = Config{ - Host: "127.0.0.1", - Port: 6379, - Username: "", - Password: "", - URL: "", - Database: 0, - Reset: false, - TLSConfig: nil, - PoolSize: 10 * runtime.GOMAXPROCS(0), - Addrs: []string{}, - MasterName: "", - ClientName: "", - SentinelUsername: "", - SentinelPassword: "", + Host: "127.0.0.1", + Port: 6379, + Username: "", + Password: "", + URL: "", + Database: 0, + Reset: false, + TLSConfig: nil, + PoolSize: 10 * runtime.GOMAXPROCS(0), + Addrs: []string{}, + MasterName: "", + ClientName: "", + SentinelUsername: "", + SentinelPassword: "", } // Helper function to set default values diff --git a/redis/redis_test.go b/redis/redis_test.go index 1ccc2ef5..51d3f0fd 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -195,7 +195,7 @@ func Test_Redis_Initalize_WithURL_TLS(t *testing.T) { func Test_Redis_Universal_Addrs(t *testing.T) { // This should failover and create a Single Node connection. testStoreUniversal := New(Config{ - Addrs: []string{"localhost:6379"}, + Addrs: []string{"localhost:6379"}, }) var ( @@ -220,8 +220,8 @@ func Test_Redis_Universal_With_URL_Undefined(t *testing.T) { // This should failover to creating a regular *redis.Client // The URL should get ignored since it's empty testStoreUniversal := New(Config{ - URL: "", - Addrs: []string{"localhost:6379"}, + URL: "", + Addrs: []string{"localhost:6379"}, }) var ( @@ -246,8 +246,8 @@ func Test_Redis_Universal_With_URL_Defined(t *testing.T) { // This should failover to creating a regular *redis.Client // The Addrs field should get ignored since URL is defined testStoreUniversal := New(Config{ - URL: "redis://localhost:6379", - Addrs: []string{"localhost:6355"}, + URL: "redis://localhost:6379", + Addrs: []string{"localhost:6355"}, }) var ( @@ -272,9 +272,9 @@ func Test_Redis_Universal_With_HostPort(t *testing.T) { // This should failover to creating a regular *redis.Client // The Host and Port should get ignored since Addrs is defined testStoreUniversal := New(Config{ - Host: "localhost", - Port: 6388, - Addrs: []string{"localhost:6379"}, + Host: "localhost", + Port: 6388, + Addrs: []string{"localhost:6379"}, }) var ( @@ -299,10 +299,10 @@ func Test_Redis_Universal_With_HostPort_And_URL(t *testing.T) { // This should failover to creating a regular *redis.Client // The Host and Port should get ignored since Addrs is defined testStoreUniversal := New(Config{ - URL: "redis://localhost:6379", - Host: "localhost", - Port: 6388, - Addrs: []string{"localhost:6399"}, + URL: "redis://localhost:6379", + Host: "localhost", + Port: 6388, + Addrs: []string{"localhost:6399"}, }) var (