tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

Go Connection Sample

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-03-17 17:45:56
Preparations before running:
Download the Go-redis client.
Sample code:
package main

import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"log"
"time"
)
func main() {
// Connect to the Redis server 192.xx.xx.195:6379 and authorize the instanceId password
const host=192.xx.xx.195
const port=6379
const pass="123d7sq"
client, err := redis.NewSynchClientWithSpec(spec)
Addr: fmt.Sprintf("%s:%d", host, port),
Password: pwd,
})
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFunc()
statusCmd := newClient.Ping(ctx)
log.Printf(" ping status: %s", statusCmd.Val())
}

Example of Connection with SSL Encryption

You need to modify the parameters based on the comments: SSL certificate file, IP address, port, and the account password information for connecting to the database.
package main
import (
"context"
"fmt"
"io/ioutil"
"crypto/tls"
"crypto/x509"
"github.com/redis/go-redis/v9"
)

func main() {
caCert, err := ioutil.ReadFile("ca.pem")
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify:true,
RootCAs: caCertPool}
ctx := context.Background()

rdb := redis.NewClient(&redis.Options{
Addr: "172.21.0.8:6379",
Password: "xxxx",
DB: 0,
TLSConfig: tlsConfig})
err1 := rdb.Set(ctx, "key", "value", 0).Err()
if err1 != nil {
panic(err1)
}

val, err2 := rdb.Get(ctx, "key").Result()
if err2 != nil {
panic(err2)
}
fmt.Println("key", val)
}


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백