O exemplo a seguir mostra como instanciar um cliente,
carregar uma autoridade certificadora de criptografia em trânsito e configurar um
pool de conexões.
Go
import("context""crypto/tls""crypto/x509""errors""fmt""io""time"memorystore"cloud.google.com/go/redis/apiv1"redispb"cloud.google.com/go/redis/apiv1/redispb""github.com/go-redis/redis/v8")// ConnectToDatabase demonstrates how to use go-redis library to connect to a// Memorystore Redis instance.funcConnectToDatabase(wio.Writer,projectID,location,instanceIDstring)error{// Instantiate a Redis administrative clientctx:=context.Background()adminClient,err:=memorystore.NewCloudRedisClient(ctx)iferr!=nil{returnerr}deferadminClient.Close()req:=&redispb.GetInstanceRequest{Name:fmt.Sprintf("projects/%s/locations/%s/instances/%s",projectID,location,instanceID),}instance,err:=adminClient.GetInstance(ctx,req)iferr!=nil{returnerr}fmt.Fprintln(w,instance)// Load CA certcaCerts:=instance.GetServerCaCerts()iflen(caCerts)==0{returnerrors.New("memorystore: no server CA certs for instance")}caCertPool:=x509.NewCertPool()caCertPool.AppendCertsFromPEM([]byte(caCerts[0].Cert))// Setup Redis Connection poolclient:=redis.NewClient(&redis.Options{Addr:fmt.Sprintf("%s:%d",instance.Host,instance.Port),Password:"PASSWORD",PoolSize:1,MinIdleConns:1,PoolTimeout:0,IdleTimeout:20*time.Second,DialTimeout:2*time.Second,TLSConfig:&tls.Config{RootCAs:caCertPool,},})p,err:=client.Ping(ctx).Result()iferr!=nil{returnerr}fmt.Fprintf(w,"Response:\n%s",p)returnnil}
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2024-12-06 UTC."],[],[]]