You need to enable JavaScript to run this app.
文档中心
缓存数据库 Redis 版

缓存数据库 Redis 版

复制全文
下载 pdf
Go SDK
代码示例
复制全文
下载 pdf
代码示例
本文介绍 Redis Go SDK 的代码示例供您参考。​
前提条件​
请求超时时间设置​
使用 Go SDK 调用 Redis 接口时,默认无超时时间设置。​
您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: <超时时间> * time.Second}) 来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。​
例如,您可以通过如下代码将客户端的请求超时时间设置为 10 秒。​
​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})​
​
创建 Redis 实例​
说明​
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance。​
​
示例 1
示例 2
​
使用如下示例代码创建 1 个启用分片集群的 Redis 实例,配置如下:​
  • 数据库版本号: 5.0。​
  • 可用内存:8 GiB(即 8192 MiB)。​
  • 分片数:2。​
  • 每个分片中的节点数:3。​
  • 在华北 2(北京)地域下使用多可用区部署,其中主节点部署在可用区 A,2 个从节点均在可用区 B。​
  • 绑定至 acl-cq014ioqkm9zt5j**** 和 acl-cq015pp15v8buvl**** 两个白名单。​
  • 绑定 department:game 和 owner:Alice 两个标签。​
  • 计费类型:包年包月,预计使用 3 个月,且不开启自动续费。​
​
package main​
​
import (​
"fmt"​
"os"​
​
"github.com/volcengine/volcengine-go-sdk/service/redis"​
"github.com/volcengine/volcengine-go-sdk/volcengine"​
"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"​
"github.com/volcengine/volcengine-go-sdk/volcengine/session"​
)​
​
func main() {​
var (​
ak string​
sk string​
region string​
config *volcengine.Config​
sess *session.Session​
client *redis.REDIS​
resp *redis.CreateDBInstanceOutput​
err error​
)​
​
ak = "Your AK"​
sk = "Your SK"​
region = "Your Region"​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region)​
​
sess, err = session.NewSession(config)​
if err != nil {​
fmt.Printf("Failed to create session, err: %v\n", err)​
os.Exit(1)​
}​
​
client = redis.New(sess)​
​
// Set the configuration information needed for creating an instance​
regionId := "cn-beijing"​
multiAZ := "enabled"​
aza := "cn-beijing-a"​
azb := "cn-beijing-b"​
azc := "cn-beijing-c"​
instanceName := "go_sdk_shardedcluster_test"​
shardedCluster := int32(1)​
password := "Pwd@12****"​
nodeNumber := int32(2)​
shardNumber := int32(2)​
shardCapacity := int64(8192)​
engineVersion := "5.0"​
vpcId := "vpc-1g15eukkrvojk8ibuxxg3****"​
subnetId := "subnet-2fe941zmjud4w5oxruvfw****"​
allowListId1 := "acl-cq0118bj1dah84v****"​
allowListId2 := "acl-cq011jlt9rkap8n****"​
key1 := "department"​
value1 := "game"​
key2 := "owner"​
value2 := "Alice"​
chargeType := "PrePaid"​
purchaseMonths := int32(3)​
autoRenew := false​
​
// Call the CreateDBInstance API to create an instance​
resp, err = client.CreateDBInstance(&redis.CreateDBInstanceInput{​
RegionId: &regionId,​
MultiAZ: &multiAZ,​
ConfigureNodes: []*redis.ConfigureNodeForCreateDBInstanceInput{​
{​
AZ: &aza,​
},​
{​
AZ: &azb,​
},​
{​
AZ: &azc,​
},​
},​
InstanceName: &instanceName,​
ShardedCluster: &shardedCluster,​
Password: &password,​
NodeNumber: &nodeNumber,​
ShardNumber: &shardNumber,​
ShardCapacity: &shardCapacity,​
EngineVersion: &engineVersion,​
VpcId: &vpcId,​
SubnetId: &subnetId,​
AllowListIds: []*string{​
&allowListId1,​
&allowListId2,​
},​
Tags: []*redis.TagForCreateDBInstanceInput{​
{​
Key: &key1,​
Value: &value1,​
},​
{​
Key: &key2,​
Value: &value2,​
},​
},​
ChargeType: &chargeType,​
PurchaseMonths: &purchaseMonths,​
AutoRenew: &autoRenew,​
})​
​
if err != nil {​
fmt.Printf("Error when calling REDISApi->CreateDBInstance, err: %v\n", err)​
os.Exit(1)​
}​
​
fmt.Println(resp)​
}​
​
​
​
查询 Redis 实例信息​
说明​
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail。​
​
package main​
​
import (​
"fmt"​
"os"​
​
"github.com/volcengine/volcengine-go-sdk/service/redis"​
"github.com/volcengine/volcengine-go-sdk/volcengine"​
"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"​
"github.com/volcengine/volcengine-go-sdk/volcengine/session"​
)​
​
func main() {​
var (​
ak string​
sk string​
region string​
config *volcengine.Config​
sess *session.Session​
client *redis.REDIS​
resp *redis.DescribeDBInstanceDetailOutput​
err error​
)​
​
ak = "Your AK"​
sk = "Your SK"​
region = "Your Region"​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).​
WithRegion(region)​
​
sess, err = session.NewSession(config)​
if err != nil {​
fmt.Printf("Failed to create session, err: %v\n", err)​
os.Exit(1)​
}​
client = redis.New(sess)​
​
// Specify the instance ID need to view​
instanceId := "redis-cn048413rhre*****"​
​
// Call the DescribeDBInstanceDetail API to view the information about the instance​
resp, err = client.DescribeDBInstanceDetail(&redis.DescribeDBInstanceDetailInput{​
InstanceId: &instanceId,​
})​
​
if err != nil {​
fmt.Printf("Error when calling REDISApi->DescribeDBInstanceDetail, err: %v\n", err)​
os.Exit(1)​
}​
​
fmt.Println(resp)​
}​
​
​
最近更新时间:2026.02.27 16:25:20
这个页面对您有帮助吗?
有用
有用
无用
无用