Notice: Over the next few months, we're reorganizing the App Engine documentation site to make it easier to find content and better align with the rest of Google Cloud products. The same content will be available, but the navigation will now match the rest of the Cloud products.

Using Memcache

Stay organized with collections Save and categorize content based on your preferences.

This page describes how to configure and monitor the memcache service for your application using the Google Cloud console. It also describes how to use the App Engine memcache Go API to set and retrieve cached values. To learn more about memcache, read the Memcache Overview.

Configuring memcache

  1. Go to the Memcache page in the Google Cloud console.
    Go to the Memcache page
  2. Select the memcache service level you want to use:

    • Shared (default) - free and provides cache capacity on a best-effort basis.
    • Dedicated - billed by the GB-hour of cache size and provides a fixed cache capacity assigned exclusively to your application.

    Learn more about available service classes in Memcache Overview.

Importing the Go API

To import the memcache package:

import ""

Caching and retrieving values

Caching a value

Use Add() to write a value for a key if and only if no value already exists for the key:

item := &memcache.Item{
        Key:   "[KEY]",
        Value: []byte("[VALUE]"),
memcache.Add(c, item)

where c is an appengine.Context.

Learn more about Add and other functions for setting values in Memcache API Reference.

Looking up cached values

Use Get() to get the item for a given key:

memcache.Get(ctx, "[KEY]")

Learn more about Get and other functions for looking up values in Memcache API Reference.


The following example demonstrates how to add, set, and get Memcache values using the Go API.

Assume that ctx is an appengine.Context.

// Create an Item
item := &memcache.Item{
	Key:   "lyric",
	Value: []byte("Oh, give me a home"),
// Add the item to the memcache, if the key does not already exist
if err := memcache.Add(ctx, item); err == memcache.ErrNotStored {
	log.Infof(ctx, "item with key %q already exists", item.Key)
} else if err != nil {
	log.Errorf(ctx, "error adding item: %v", err)

// Change the Value of the item
item.Value = []byte("Where the buffalo roam")
// Set the item, unconditionally
if err := memcache.Set(ctx, item); err != nil {
	log.Errorf(ctx, "error setting item: %v", err)

// Get the item from the memcache
if item, err := memcache.Get(ctx, "lyric"); err == memcache.ErrCacheMiss {
	log.Infof(ctx, "item not in the cache")
} else if err != nil {
	log.Errorf(ctx, "error getting item: %v", err)
} else {
	log.Infof(ctx, "the lyric is %q", item.Value)

Monitoring memcache in the Google Cloud console

  1. Go to the Memcache page in the Google Cloud console.
    Go to the Memcache page
  2. Look at the following reports:
    • 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.
    • 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.
    • Items in the cache.
    • 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.
    • Total cache size.
  3. You can take any of the following actions:

    • New key: Add a new key to the cache.
    • Find a key: Retrieve an existing key.
    • Flush cache: Remove all the key-value pairs from the cache.
  4. (Dedicated memcache only) Look through the list of Hot keys.

    • "Hot keys" are keys that receive more than 100 queries per second (QPS) in the memcache.
    • This list includes up to 100 hot keys, sorted by highest QPS.

What's next