Skip to content

examples

ludoch edited this page Apr 15, 2026 · 1 revision

Memcache Examples

No examples are available for this runtime.

This page provides code examples in Java for using the low-level Memcache API.

Python code for using Memcache. Memcache is a high-performance, distributed memory object caching system that provides fast access to cached data. To learn more about memcache, read the Memcache Overview.

Synchronous usage

Low-level API example using the synchronous MemcacheService:

View MemcacheSyncCacheServlet.java on GitHub (region: example)

Asynchronous usage

Low-level API example using AsyncMemcacheService:

View MemcacheAsyncCacheServlet.java on GitHub (region: example)

For more information on the low-level API, see the Memcache Javadoc.

The memcache Pattern

Memcache is typically used with the following pattern:

  • The application receives a query from the user or the application. * The application checks whether the data needed to satisfy that query is in memcache.
    • If the data is in memcache, the application uses that data.
    • If the data is not in memcache, the application queries the datastore and stores the results in memcache for future requests.

The pseudocode below represents a typical memcache request:

ndb internally uses memcache to speed up queries. However, if you wish, you can also explicitly add memcache calls to gain more control about the speed-ups.

Caching data

The following example demonstrates several ways to set values in memcache using the Python API.

To learn more about the add(), set_multi(), and set() methods, see the memcache Python API documentation.

Note: The standard Python pickle module is used to auto pickle and depickle data moved into and out of memcache.

Clone this wiki locally