This page provides code examples in 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.
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.