// Create an Itemitem:=&memcache.Item{Key:"lyric",Value:[]byte("Oh, give me a home"),}// Add the item to the memcache, if the key does not already existiferr:=memcache.Add(ctx,item);err==memcache.ErrNotStored{log.Infof(ctx,"item with key %q already exists",item.Key)}elseiferr!=nil{log.Errorf(ctx,"error adding item: %v",err)}// Change the Value of the itemitem.Value=[]byte("Where the buffalo roam")// Set the item, unconditionallyiferr:=memcache.Set(ctx,item);err!=nil{log.Errorf(ctx,"error setting item: %v",err)}// Get the item from the memcacheifitem,err:=memcache.Get(ctx,"lyric");err==memcache.ErrCacheMiss{log.Infof(ctx,"item not in the cache")}elseiferr!=nil{log.Errorf(ctx,"error getting item: %v",err)}else{log.Infof(ctx,"the lyric is %q",item.Value)}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\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)."]]