Google App Engine

Memcache PHP API Overview

Python |Java |PHP |Go
High performance scalable web applications often use a distributed in-memory data cache in front of or in place of robust persistent storage for some tasks. App Engine includes a memory cache service for this purpose.

  1. PHP memcache implementation
  2. When to use a memory cache
  3. How cached data expires
  4. Statistics
  5. Limits
  6. Configuring memcache

PHP memcache implementation

App Engine includes implementations of the standard Memcache and Memcached APIs, which invoke the App Engine memcache service "under the hood". Some functions are callable ("stubbed") but do nothing, as they aren't needed in the context of an App Engine app. As such, calls to the following functions will be ignored:

Stubbed functions in the Memcache API

  • memcache_add_server()
  • memcache_close()
  • memcache_connect()
  • memcache_pconnect()
  • memcache_set_compress_threshold()
  • addServer()
  • close()
  • connect()
  • pconnect()
  • setCompressThreshold()

Stubbed functions in the Memcached API

  • addServer()
  • addServers()
  • getAllKeys()
  • getServerByKey()
  • getServerList()
  • getStats()
  • getVersion()
  • isPersistent()
  • isPristine()
  • quit()
  • resetServerList()
  • setSaslAuthData()

An example usage of the Memcache PHP API in App Engine:

function getData($key) {
  $memcache = new Memcache;
  $data = $memcache->get($key);
  if ($data === false) {
    $data = doSlowQuery($key);
    $memcache->set($key, $data);
  return $data;

An example usage of the Memcached PHP API in App Engine:

$memcache = new Memcached;
$memcache->setMulti(['image' => $image, 'data' => $data], 300);

When to use a memory cache

One use of a memory cache is to speed up common datastore queries. If many requests make the same query with the same parameters, and changes to the results do not need to appear on the web site right away, the app can cache the results in the memcache. Subsequent requests can check the memcache, and only perform the datastore query if the results are absent or expired. Session data, user preferences, and any other queries performed on most pages of a site are good candidates for caching.

Memcache may be useful for other temporary values. However, when considering whether to store a value solely in the memcache and not backed by other persistent storage, be sure that your application behaves acceptably when the value is suddenly not available. Values can expire from the memcache at any time, and may be expired prior to the expiration deadline set for the value. For example, if the sudden absence of a user's session data would cause the session to malfunction, that data should probably be stored in the datastore in addition to the memcache.

How cached data expires

By default, values stored in memcache are retained as long as possible. Values may be evicted from the cache when a new value is added to the cache if the cache is low on memory. When values are evicted due to memory pressure, the least recently used values are evicted first.

The app can provide an expiration time when a value is stored, as either a number of seconds relative to when the value is added, or as an absolute Unix epoch time in the future (a number of seconds from midnight January 1, 1970). The value will be evicted no later than this time, though it may be evicted for other reasons.

Under rare circumstances, values may also disappear from the cache prior to expiration for reasons other than memory pressure. While memcache is resilient to server failures, memcache values are not saved to disk, so a service failure may cause values to become unavailable.

In general, an application should not expect a cached value to always be available.

You can erase an application's entire cache via the API or via the Admin Console (under Memcache Viewer).


Memcache maintains statistics about the amount of data cached for an application, the cache hit rate, and the age of cache items. You can view these statistics using the API or in the Administration Console, under Memcache Viewer.


The following limits apply to the use of the memcache service:

  • The maximum size of a cached data value is 1 MB minus the size of the key minus an implementation-dependent overhead which is approximately 96 bytes.
  • A key cannot be larger than 250 bytes. In the PHP runtime, if you try to set memcache with a larger key the call will raise an exception. (Other runtimes behave differently.)
  • The "multi" batch operations can have any number of elements. The total size of the call and the total size of the data fetched must not exceed 32 megabytes.

Configuring memcache

The memcache service provides best-effort cache space by default. Apps with billing enabled may opt to use dedicated memcache which provides a fixed cache size assigned exclusively to your app. The service is configured via memcache settings on the Admin Console.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.