Go 1.11 ha raggiunto la fine del supporto
e verrà
ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment delle applicazioni Go 1.11, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment dei runtime legacy. Le tue applicazioni Go
1.11 continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti
consigliamo di
eseguire la migrazione all'ultima versione supportata di Go.
Utilizzo di memcache
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina descrive come configurare e monitorare il servizio memcache per la tua applicazione utilizzando la console Google Cloud . Descrive inoltre come utilizzare l'API Go memcache di App Engine
per impostare e recuperare i valori memorizzati nella cache. Per saperne di più su Memcache,
leggi la panoramica di Memcache.
Configurazione di Memcache
- Vai alla pagina Memcache nella console Google Cloud .
Vai alla pagina Memcache
Seleziona il livello di servizio memcache che vuoi utilizzare:
- Condiviso (impostazione predefinita): gratuito e fornisce capacità della cache al meglio delle possibilità.
- Dedicato: fatturato in base alle GB/ora di dimensione della cache e fornisce una capacità della cache fissa assegnata esclusivamente alla tua applicazione.
Scopri di più sulle classi di servizio disponibili nella panoramica di Memcache.
Importazione dell'API Go
Per importare il pacchetto memcache
:
Memorizzazione nella cache e recupero dei valori
Memorizzazione nella cache di un valore
Utilizza Add()
per scrivere un valore per una chiave se e solo se non esiste già un valore
per la chiave:
item := &memcache.Item{
Key: "[KEY]",
Value: []byte("[VALUE]"),
}
memcache.Add(c, item)
dove c
è un appengine.Context
.
Scopri di più su Add
e altre funzioni per impostare i valori nell'API Memcache
Reference.
Ricerca di valori memorizzati nella cache
Utilizza Get()
per ottenere l'elemento per una determinata chiave:
memcache.Get(ctx, "[KEY]")
Scopri di più su Get
e altre funzioni per la ricerca di valori nel riferimento API
Memcache.
Esempio
L'esempio riportato di seguito mostra come aggiungere, impostare e ottenere valori Memcache
utilizzando l'API Go.
Supponi che ctx
sia un appengine.Context
.
Monitoraggio di Memcache nella console Google Cloud
- Vai alla pagina Memcache nella console Google Cloud .
Vai alla pagina Memcache
- Esamina i seguenti report:
- Livello di servizio memcache: indica se la tua applicazione utilizza il livello di servizio condiviso o dedicato.
Se sei il proprietario del progetto, puoi passare da una modalità all'altra. Scopri
di più sui livelli di servizio.
- Rapporto hit: mostra la percentuale di richieste di dati gestite dalla cache, nonché il numero grezzo di richieste di dati gestite dalla cache.
- Elementi nella cache.
- Età elemento più vecchio: l'età dell'elemento memorizzato nella cache meno recente. Tieni presente che l'età
di un elemento viene reimpostata ogni volta che viene utilizzato, letto o scritto.
- Dimensione cache totale.
Puoi eseguire una delle seguenti azioni:
- Nuova chiave: aggiungi una nuova chiave alla cache.
- Trova una chiave: recupera una chiave esistente.
- Svuota cache: rimuovi tutte le coppie chiave-valore dalla cache.
(Solo memcache dedicato) Esamina l'elenco dei tasti di scelta rapida.
- Le "hot key" sono chiavi che ricevono più di 100 query al secondo (QPS) nella
memcache.
- Questo elenco include fino a 100 tasti di scelta rapida, ordinati in base al valore QPS più alto.
Passaggi successivi
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-09-04 UTC.
[[["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-04 UTC."],[[["\u003cp\u003eThis guide outlines how to configure and monitor the memcache service for applications using the Google Cloud console.\u003c/p\u003e\n"],["\u003cp\u003eThe document details how to utilize the App Engine memcache Go API to store and retrieve cached data through \u003ccode\u003eAdd()\u003c/code\u003e and \u003ccode\u003eGet()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eMemcache offers two service levels: Shared, which is free with best-effort capacity, and Dedicated, which offers fixed cache capacity and is billed by GB-hour.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Cloud console's Memcache page provides reports on service level, hit ratio, item count, item age, total cache size, and hot keys (for dedicated memcache).\u003c/p\u003e\n"],["\u003cp\u003eThe guide provides instructions on how to import the memcache package and provides a code example of adding, setting, and getting memcache values.\u003c/p\u003e\n"]]],[],null,["# Using Memcache\n\nThis page describes how to configure and monitor the memcache service for your\napplication using the Google Cloud console. It also describes how to use the App Engine memcache Go API\nto set and retrieve cached values. To learn more about memcache,\nread the [Memcache Overview](/appengine/docs/legacy/standard/go111/memcache).\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| go\n| /services/access). If you are updating to the App Engine Go 1.12+ runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/go-differences) to learn about your migration options for legacy bundled services.\n\nConfiguring memcache\n--------------------\n\n1. Go to the Memcache page in the Google Cloud console. \n [Go to the Memcache page](https://console.cloud.google.com/appengine/memcache)\n2. Select the memcache service level you want to use:\n\n - **Shared** (default) - free and provides cache capacity on a best-effort basis.\n - **Dedicated** - billed by the GB-hour of cache size and provides a fixed cache capacity assigned exclusively to your application.\n\n Learn more about available service classes in [Memcache Overview](#service_levels).\n\nImporting the Go API\n--------------------\n\nTo import the `memcache` package: \n\n import \"google.golang.org/appengine/memcache\"\n\nCaching and retrieving values\n-----------------------------\n\n### Caching a value\n\nUse [`Add()`](/appengine/docs/legacy/standard/go111/reference/latest/memcache#google_golang_org_appengine_memcache_Add) to write a value for a key if and only if no value\nalready exists for the key: \n\n item := &memcache.Item{\n Key: \"[KEY]\",\n Value: []byte(\"[VALUE]\"),\n }\n memcache.Add(c, item)\n\nwhere `c` is an `appengine.Context`.\n\nLearn more about `Add` and other functions for setting values in [Memcache API\nReference](/appengine/docs/legacy/standard/go111/memcache/reference).\n\n### Looking up cached values\n\nUse [`Get()`](/appengine/docs/legacy/standard/go111/reference/latest/memcache#google_golang_org_appengine_memcache_Item_Get) to get the item for a given key: \n\n memcache.Get(ctx, \"[KEY]\")\n\nLearn more about `Get` and other functions for looking up values in [Memcache\nAPI Reference](/appengine/docs/legacy/standard/go111/memcache/reference).\n\n### Example\n\nThe following example demonstrates how to add, set, and get Memcache values\nusing the Go API.\n\nAssume that `ctx` is an `appengine.Context`. \n\n // Create an Item\n item := &memcache.Item{\n \tKey: \"lyric\",\n \tValue: []byte(\"Oh, give me a home\"),\n }\n // Add the item to the memcache, if the key does not already exist\n if err := memcache.Add(ctx, item); err == memcache.ErrNotStored {\n \tlog.Infof(ctx, \"item with key %q already exists\", item.Key)\n } else if err != nil {\n \tlog.Errorf(ctx, \"error adding item: %v\", err)\n }\n\n // Change the Value of the item\n item.Value = []byte(\"Where the buffalo roam\")\n // Set the item, unconditionally\n if err := memcache.Set(ctx, item); err != nil {\n \tlog.Errorf(ctx, \"error setting item: %v\", err)\n }\n\n // Get the item from the memcache\n if item, err := memcache.Get(ctx, \"lyric\"); err == memcache.ErrCacheMiss {\n \tlog.Infof(ctx, \"item not in the cache\")\n } else if err != nil {\n \tlog.Errorf(ctx, \"error getting item: %v\", err)\n } else {\n \tlog.Infof(ctx, \"the lyric is %q\", item.Value)\n }\n\nMonitoring memcache in the Google Cloud console\n-----------------------------------------------\n\n1. Go to the Memcache page in the Google Cloud console. \n [Go to the Memcache page](https://console.cloud.google.com/appengine/memcache) \n2. Look at the following reports:\n - **Memcache service level** : Shows if your application is using the Shared or Dedicated service level. If you are an owner of the project, you can switch between the two. Learn more about the [service levels](./#service_levels).\n - **Hit ratio**: Shows the percentage of data requests that were served from the cache, as well as the raw number of data requests that were served from the cache.\n - **Items in the cache**.\n - **Oldest item age**: The age of the oldest cached item. Note that the age of an item is reset every time it is used, either read or written.\n - **Total cache size**.\n3. You can take any of the following actions:\n\n - **New key**: Add a new key to the cache.\n - **Find a key**: Retrieve an existing key.\n - **Flush cache**: Remove all the key-value pairs from the cache.\n4. (Dedicated memcache only) Look through the list of **Hot keys**.\n\n - \"Hot keys\" are keys that receive more than 100 queries per second (QPS) in the memcache.\n - This list includes up to 100 hot keys, sorted by highest QPS.\n\nWhat's next\n-----------\n\n- Learn more about memcache in the [Memcache Overview](/appengine/docs/legacy/standard/go111/memcache).\n- Refer to the [Memcache API Reference](/appengine/docs/legacy/standard/go111/memcache/reference)."]]