Go 1.11 telah mencapai akhir dukungan
dan akan
dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi Go 1.11, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi Go 1.11 yang ada akan terus berjalan dan menerima traffic setelah
tanggal penghentiannya. Sebaiknya Anda
bermigrasi ke Go versi terbaru yang didukung.
Menggunakan Memcache
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara mengonfigurasi dan memantau layanan memcache untuk
aplikasi Anda menggunakan konsol Google Cloud . Panduan ini juga menjelaskan cara menggunakan memcache Go API App Engine untuk menetapkan dan mengambil nilai yang disimpan dalam cache. Untuk mempelajari memcache lebih lanjut,
baca Ringkasan Memcache.
Mengonfigurasi memcache
- Buka halaman Memcache di konsol Google Cloud .
Buka halaman Memcache
Pilih tingkat layanan memcache yang ingin Anda gunakan:
- Bersama (default) - gratis dan menyediakan kapasitas cache berdasarkan upaya
terbaik.
- Khusus - ditagih berdasarkan GB-jam ukuran cache dan menyediakan kapasitas
cache tetap yang ditetapkan secara eksklusif untuk aplikasi Anda.
Pelajari lebih lanjut tentang class layanan yang tersedia di Ringkasan Memcache.
Mengimpor Go API
Untuk mengimpor paket memcache
:
Menyimpan nilai ke cache dan mengambil nilai
Menyimpan nilai ke cache
Gunakan Add()
untuk menulis nilai kunci jika dan hanya jika belum ada nilai untuk kunci tersebut:
item := &memcache.Item{
Key: "[KEY]",
Value: []byte("[VALUE]"),
}
memcache.Add(c, item)
dengan c
adalah appengine.Context
.
Pelajari Add
dan fungsi lainnya untuk menetapkan nilai lebih lanjut di Referensi Memcache API.
Mencari nilai yang disimpan di cache
Gunakan Get()
untuk mendapatkan item untuk kunci tertentu:
memcache.Get(ctx, "[KEY]")
Pelajari Get
dan fungsi lainnya lebih lanjut untuk mencari nilai di Referensi Memcache API.
Contoh
Contoh berikut menunjukkan cara menambahkan, menetapkan, dan mendapatkan nilai Memcache menggunakan Go API.
Asumsikan ctx
adalah appengine.Context
.
Memantau memcache di konsol Google Cloud
- Buka halaman Memcache di konsol Google Cloud .
Buka halaman Memcache
- Lihat laporan berikut:
- Tingkat layanan Memcache: Menunjukkan apakah aplikasi Anda menggunakan tingkat layanan Bersama
atau Khusus.
Jika Anda adalah pemilik project, Anda dapat beralih di antara keduanya. Pelajari tingkat layanan lebih lanjut.
- Rasio hit: Menampilkan persentase permintaan data yang disajikan dari
cache, serta jumlah mentah permintaan data yang disajikan
dari cache.
- Item dalam cache.
- Usia item terlama: Usia item terlama yang disimpan di cache. Perhatikan bahwa usia
item akan di-reset setiap kali item digunakan, baik dibaca maupun ditulis.
- Total ukuran cache.
Anda dapat melakukan salah satu tindakan berikut:
- Kunci baru: Menambahkan kunci baru ke cache.
- Temukan kunci: Mengambil kunci yang ada.
- Kosongkan cache: Menghapus semua key-value pair dari cache.
(Hanya untuk memcache khusus) Lihat daftar Hotkey.
- "Hotkey" adalah kunci yang menerima lebih dari 100 kueri per detik (QPS) di
memcache.
- Daftar ini berisi maksimal 100 hotkey, yang diurutkan berdasarkan QPS tertinggi.
Langkah berikutnya
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-09-04 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)."]]