Skip to page content Skip to main navigation
English | Site Directory

e.g. "templates" or "datastore"

# Overview

The Memcache service provides your application with a high performance in-memory key-value cache that is accessible by multiple instances of your application. Memcache is useful for data that does not need the persistence and transactional features of the datastore, such as temporary data or data copied from the datastore to the cache for high speed access. The Memcache API has similar features to and is compatible with memcached by Danga Interactive.

The Memcache API enables you to increase the performance of your application and reduce the load on your datastore by:

- Significantly reducing the number of datastore queries.
- Reducing the datastore quota usage for very popular pages.
- Caching the results of expensive queries and operations.
- Making possible the use of transient counters.

By using the Memcache API you can create a coherent cache for data in your application. The cache is available to all instances of your application and data is only evicted through memory pressure (i.e. there is too much data in the cache) or the caching policy set by the developer. The caching policy can be set on each key-value pair stored in the cache. You can clear the entire cache or set a cache expiration time on each piece of data.

    from google.appengine.api import memcache

    # Add a value if it doesn't exist in the cache, with a cache expiration of 1 hour.
    memcache.add(key="weather_USA_98105", value="raining", time=3600)

    # Set several values, overwriting any existing values for these keys.
    memcache.set_multi({ "USA_98105": "raining",
                         "USA_94105": "foggy",
                         "USA_94043": "sunny" },
                         key_prefix="weather_", time=3600)

    # Atomically increment an integer value.
    memcache.set(key="counter", 0)
    memcache.incr("counter")
    memcache.incr("counter")
    memcache.incr("counter")