Skip to main content

Redis Connections

Overview

This section focuses on the case of redis creating a single node.

Preparing

  1. Complete golang installation
  2. Prepare for yourself a redis server, for example we use 127.0.0.1:6379.

redis configuration description

RedisConf related introduction.
RedisConf struct {
Host string
Type string `json:",default=node,options=node|cluster"`
Pass string `json:",optional"`
Tls bool `json:",optional"`
}

Host: redis service address :port, if redis cluster is ip1:port1,ip2:port2,ip3:port3. .(redis is not supported)
Type: node single nodes redis, cluster reced
Pass: password
Tls: Turn on tls:

Initialize redis configuration, we recommend loading using conf.MustLoad for configurations, referenceconfiguration profiles

conf := redis.RedisConf{
Host: "127.0.0.1:6379",
Type: "node",
Pass: "123456",
Tls: false,
}

Create redis instance

Create a redis link using MustNewRedis.

conf := redis.RedisConf{
Host: "127.0.0.1:6379",
Type: "node",
Pass: "123456",
Tls: false,
}

rds := redis.MustNewRedis(conf)

This way we have finished creating an instance of rds.

redis Usage

String values are associated to key and extracted from redis.

    ctx := context.Background()
err := rds.SetCtx(ctx, "key", "hello world")
if err != nil {
logc.Error(ctx, err)
}

v, err := rds.GetCtx(ctx, "key")
if err != nil {
logc.Error(ctx, err)
}
fmt.Println(v)

Full examples below

package main

import (
"context"
"fmt"
"time"

"github.com/zeromicro/go-zero/core/logc"
"github.com/zeromicro/go-zero/core/stores/redis"
)

func main() {
conf := redis.RedisConf{
Host: "127.0.0.1:6379",
Type: "node",
Pass: "",
Tls: false,
NonBlock: false,
PingTimeout: time.Second,
}
rds := redis.MustNewRedis(conf)
ctx := context.Background()
err := rds.SetCtx(ctx, "key", "hello world")
if err != nil {
logc.Error(ctx, err)
}

v, err := rds.GetCtx(ctx, "key")
if err != nil {
logc.Error(ctx, err)
}
fmt.Println(v)
}