The gredis component supports two methods for managing redis configurations and obtaining redis objects: one is through the configuration component + singleton object approach; the other is modularized using configuration management methods and object creation methods.

In most cases, it is recommended to use the g.Redis singleton approach to operate redis. Therefore, using configuration files to manage Redis configurations is also recommended. An example configuration in config.yaml is as follows:

Single Instance Configuration

  1. # Redis Configuration Example
  2. redis:
  3. # Single Instance Example 1
  4. default:
  5. address: 127.0.0.1:6379
  6. db: 1
  7. # Single Instance Example 2
  8. cache:
  9. address: 127.0.0.1:6379
  10. db: 1
  11. pass: 123456
  12. idleTimeout: 600

Here, default and cache represent configuration group names. In the program, you can obtain the corresponding redis singleton object using this name. If no group name is passed, the redis.default configuration group is used by default to obtain the corresponding redis client singleton object.

Cluster Configuration

  1. # Redis Configuration Example
  2. redis:
  3. # Cluster Mode Configuration Method
  4. default:
  5. address: 127.0.0.1:6379,127.0.0.1:6370
  6. db: 1

Configuration Item Description

Configuration ItemRequiredDefaultDescription
addressYes-Format: Address:Port
Supports Redis single instance mode and cluster mode configuration, using , to separate multiple addresses. For example:
192.168.1.1:6379, 192.168.1.2:6379
dbNo0Database index
userNo-Authorized user for access
passNo-Authorized password for access
minIdleNo0Minimum number of idle connections allowed
maxIdleNo10Maximum number of idle connections allowed (0 means no limit)
maxActiveNo100Maximum connection limit (0 means no limit)
idleTimeoutNo10Maximum idle time for connections, using a time string such as 30s/1m/1d
maxConnLifetimeNo30Maximum lifetime for connections, using a time string such as 30s/1m/1d
waitTimeoutNo0Timeout for waiting on a connection pool, using a time string such as 30s/1m/1d
dialTimeoutNo0Timeout for TCP connection, using a time string such as 30s/1m/1d
readTimeoutNo0Timeout for TCP Read operation, using a time string such as 30s/1m/1d
writeTimeoutNo0Timeout for TCP Write operation, using a time string such as 30s/1m/1d
masterNameNo-Used in Sentinel mode, set MasterName
tlsNofalseWhether to use TLS authentication
tlsSkipVerifyNofalseWhether to disable server name verification when connecting via TLS
clusterNofalseWhether to force setting as cluster working mode. When address is a single endpoint cluster, the system will automatically determine it as single instance mode, in which case this should be set to true.
protocolNo3Set the RESP protocol version for communication with Redis Server.
sentinelUsernameNoAccount for Sentinel mode
sentinelPasswordNoPassword for Sentinel mode

Usage Example:

config.yaml

  1. # Redis Configuration Example
  2. redis:
  3. # Single Instance Example 1
  4. default:
  5. address: 127.0.0.1:6379
  6. db: 1
  7. pass: "password" # Set password here, remove if not needed
  1. package main
  2. import (
  3. "fmt"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. )
  8. func main() {
  9. var ctx = gctx.New()
  10. _, err := g.Redis().Set(ctx, "key", "value")
  11. if err != nil {
  12. g.Log().Fatal(ctx, err)
  13. }
  14. value, err := g.Redis().Get(ctx, "key")
  15. if err != nil {
  16. g.Log().Fatal(ctx, err)
  17. }
  18. fmt.Println(value.String())
  19. }

After execution, the output is:

Configuration Method (Advanced)

Since GoFrame is a modular framework, apart from using the coupled and convenient g module to automatically parse configuration files and obtain singleton objects, it also supports modular use of the gredis package by capable developers.

gredis provides global group configuration capabilities, with related configuration management methods as follows:

  1. func SetConfig(config Config, name ...string)
  2. func SetConfigByMap(m map[string]interface{}, name ...string) error
  3. func GetConfig(name ...string) (config Config, ok bool)
  4. func RemoveConfig(name ...string)
  5. func ClearConfig()

Usage Example:

  1. package main
  2. import (
  3. "fmt"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/v2/database/gredis"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. )
  9. var (
  10. config = gredis.Config{
  11. Address: "127.0.0.1:6379",
  12. Db: 1,
  13. Pass: "password",
  14. }
  15. group = "cache"
  16. ctx = gctx.New()
  17. )
  18. func main() {
  19. gredis.SetConfig(&config, group)
  20. _, err := g.Redis(group).Set(ctx, "key", "value")
  21. if err != nil {
  22. g.Log().Fatal(ctx, err)
  23. }
  24. value, err := g.Redis(group).Get(ctx, "key")
  25. if err != nil {
  26. g.Log().Fatal(ctx, err)
  27. }
  28. fmt.Println(value.String())
  29. }