Get context cache

Get information about a specific context cache, such as its creation time and expiration time.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

Before trying this sample, follow the C# setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI C# API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


usingGoogle.Cloud.AIPlatform.V1Beta1 ;
usingSystem;
usingSystem.Threading.Tasks;
publicclassGetContextCache
{
publicasyncTask<CachedContent>Get(CachedContentName name)
{
varclient=awaitnewGenAiCacheServiceClientBuilder
{
Endpoint="us-central1-aiplatform.googleapis.com"
}.BuildAsync();
varcachedContent=awaitclient.GetCachedContentAsync(newGetCachedContentRequest
{
CachedContentName=name
});
Console.WriteLine($"Get cache: {cachedContent.Name}");
returncachedContent;
}
}

Go

Before trying this sample, follow the Go setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Go API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import(
"context"
"fmt"
"io"
"cloud.google.com/go/vertexai/genai"
)
// getContextCache shows how to retrieve the metadata of a cached content
// contentName is the ID of the cached content to retrieve
funcgetContextCache(wio.Writer,contentNamestring,projectID,locationstring)error{
// location := "us-central1"
ctx:=context.Background()
client,err:=genai.NewClient (ctx,projectID,location)
iferr!=nil{
returnfmt.Errorf("unable to create client: %w",err)
}
deferclient.Close ()
cachedContent,err:=client.GetCachedContent (ctx,contentName)
iferr!=nil{
returnfmt.Errorf("GetCachedContent: %w",err)
}
fmt.Fprintf(w,"Retrieved cached content %q",cachedContent.Name )
returnnil
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.