Il seguente esempio mostra come creare un'istanza di un client, caricare un'autorità di certificazione per la crittografia in transito e configurare un pool di connessioni.
Vai
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}
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-05 UTC."],[],[],null,["# In-transit encryption client library code sample\n\nThis code sample shows a [go-redis client library](https://github.com/redis/go-redis)\nconfiguration for connecting to a Memorystore for Redis instance that has [in-transit encryption](/memorystore/docs/redis/about-in-transit-encryption)\nenabled.\n\nConnect to an instance\n----------------------\n\nThe following sample provides an example of how to instantiate a client,\nload an in-transit encryption Certificate Authority, and how to set up a\nconnection pool. \n\n### Go\n\n import (\n \t\"context\"\n \t\"crypto/tls\"\n \t\"crypto/x509\"\n \t\"errors\"\n \t\"fmt\"\n \t\"io\"\n \t\"time\"\n\n \tmemorystore \"cloud.google.com/go/redis/apiv1\"\n \tredispb \"cloud.google.com/go/redis/apiv1/redispb\"\n \t\"github.com/go-redis/redis/v8\"\n )\n\n // ConnectToDatabase demonstrates how to use go-redis library to connect to a\n // Memorystore Redis instance.\n func ConnectToDatabase(w io.Writer, projectID, location, instanceID string) error {\n\n \t// Instantiate a Redis administrative client\n \tctx := context.Background()\n \tadminClient, err := memorystore.NewCloudRedisClient(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer adminClient.https://cloud.google.com/go/docs/reference/cloud.google.com/go/redis/latest/apiv1.html#cloud_google_com_go_redis_apiv1_CloudRedisClient_Close()\n\n \treq := &redispb.GetInstanceRequest{\n \t\tName: fmt.Sprintf(\"projects/%s/locations/%s/instances/%s\", projectID, location, instanceID),\n \t}\n\n \tinstance, err := adminClient.GetInstance(ctx, req)\n \tif err != nil {\n \t\treturn err\n \t}\n\n \tfmt.Fprintln(w, instance)\n\n \t// Load CA cert\n \tcaCerts := instance.GetServerCaCerts()\n \tif len(caCerts) == 0 {\n \t\treturn errors.New(\"memorystore: no server CA certs for instance\")\n \t}\n\n \tcaCertPool := x509.NewCertPool()\n \tcaCertPool.AppendCertsFromPEM([]byte(caCerts[0].Cert))\n\n \t// Setup Redis Connection pool\n \tclient := redis.NewClient(&redis.Options{\n \t\tAddr: fmt.Sprintf(\"%s:%d\", instance.Host, instance.Port),\n \t\tPassword: \"PASSWORD\",\n \t\tPoolSize: 1,\n \t\tMinIdleConns: 1,\n \t\tPoolTimeout: 0,\n \t\tIdleTimeout: 20 * time.Second,\n \t\tDialTimeout: 2 * time.Second,\n \t\tTLSConfig: &tls.Config{\n \t\t\tRootCAs: caCertPool,\n \t\t},\n \t})\n\n \tp, err := client.Ping(ctx).Result()\n \tif err != nil {\n \t\treturn err\n \t}\n \tfmt.Fprintf(w, \"Response:\\n%s\", p)\n\n \treturn nil\n }\n\nWhat's next\n-----------\n\n- Learn more about [in-transit encryption](/memorystore/docs/redis/about-in-transit-encryption).\n- Learn how to [enable in-transit encryption](/memorystore/docs/redis/manage-in-transit-encryption) on an instance."]]