Skip to main content
Version: 2.8.x(Latest)

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

# Redis Configuration Example
redis:
# Single Instance Example 1
default:
address: 127.0.0.1:6379
db: 1

# Single Instance Example 2
cache:
address: 127.0.0.1:6379
db: 1
pass: 123456
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

# Redis Configuration Example
redis:
# Cluster Mode Configuration Method
default:
address: 127.0.0.1:6379,127.0.0.1:6370
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

# Redis Configuration Example
redis:
# Single Instance Example 1
default:
address: 127.0.0.1:6379
db: 1
pass: "password" # Set password here, remove if not needed
package main

import (
"fmt"

_ "github.com/gogf/gf/contrib/nosql/redis/v2"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)

func main() {
var ctx = gctx.New()
_, err := g.Redis().Set(ctx, "key", "value")
if err != nil {
g.Log().Fatal(ctx, err)
}
value, err := g.Redis().Get(ctx, "key")
if err != nil {
g.Log().Fatal(ctx, err)
}
fmt.Println(value.String())
}

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:

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

Usage Example:

package main

import (
"fmt"

_ "github.com/gogf/gf/contrib/nosql/redis/v2"

"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)

var (
config = gredis.Config{
Address: "127.0.0.1:6379",
Db: 1,
Pass: "password",
}
group = "cache"
ctx = gctx.New()
)

func main() {
gredis.SetConfig(&config, group)

_, err := g.Redis(group).Set(ctx, "key", "value")
if err != nil {
g.Log().Fatal(ctx, err)
}
value, err := g.Redis(group).Get(ctx, "key")
if err != nil {
g.Log().Fatal(ctx, err)
}
fmt.Println(value.String())
}