Redis Universal client
TIP
To get an idea how to use go-redis client, see Getting started guide.
UniversalClient
is a wrapper client which, based on the provided options, represents either a ClusterClient
, a FailoverClient
, or a single-node Client
. This can be useful for testing cluster-specific applications locally or having different clients in different environments.
NewUniversalClient
returns a new multi client. The type of the returned client depends on the following conditions:
- If the
MasterName
option is specified, a sentinel-backedFailoverClient
is returned. - if the number of
Addrs
is two or more, aClusterClient
is returned. - Otherwise, a single-node
Client
is returned.
For example:
// rdb is *redis.Client.
rdb := NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{":6379"},
})
// rdb is *redis.ClusterClient.
rdb := NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{":6379", ":6380"},
})
// rdb is *redis.FailoverClient.
rdb := NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{":6379"},
MasterName: "mymaster",
})