Redis
Less than 1 minutecomponentredis
Redis Usage Example
goredis
is a Redis client packaged based on go-redis/v9, supporting multiple modes such as Redis master-replica, sentinel, and cluster.
Redis Configuration
Set the redis
field in the YAML file under the configs
directory, supporting setting sentinel and cluster:
redis:
# dsn format: [user]:<pass>@127.0.0.1:6379/[db], default user is default, user is supported in Redis 6.0 and above.
dsn: "default:123456@127.0.0.1:6379/0"
dialTimeout: 10 # Dial timeout, unit (seconds)
readTimeout: 2 # Read timeout, unit (seconds)
writeTimeout: 2 # Write timeout, unit (seconds)
# sentinelAddrs: ["127.0.0.1:6379", "127.0.0.1:6380"]
# clusterAddrs: ["127.0.0.1:6379", "127.0.0.1:6380"]
Redis Initialization
Single
// way 1: redis version 6.0 or above
redisCli, err := goredis.Init("default:123456@127.0.0.1:6379") // can set parameters such as timeout, tls, tracing, such as goredis.Withxxx()
if err != nil {
panic("goredis.Init error: " + err.Error())
}
// way 2: redis version 5.0 or below
redisCli, err := goredis.InitSingle("127.0.0.1:6379", "123456", 0) // can set parameters such as timeout, tls, tracing, such as goredis.Withxxx()
Sentinel
addrs := []string{"127.0.0.1:6380", "127.0.0.1:6381", "127.0.0.1:6382","127.0.0.1:26380", "127.0.0.1:26381", "127.0.0.1:26382"}
rdbCli, err : := goredis.InitSentinel("mymaster", addrs, "", "123456") // can set parameters such as timeout, tls, tracing, such as goredis.Withxxx()
Cluster
addrs := []string{"127.0.0.1:6380", "127.0.0.1:6381", "127.0.0.1:6382","127.0.0.1:6383", "127.0.0.1:6384", "127.0.0.1:6385"}
clusterRdb, err : := goredis.InitCluster(addrs, "", "123456") // can set parameters such as timeout, tls, tracing, such as goredis.Withxxx()