Stay organized with collections Save and categorize content based on your preferences.
This page provides Python code examples 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.
This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the migration guide to learn about your migration options for legacy bundled services. The memcache PatternMemcache is typically used with the following pattern:
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 dataThe 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.
guestbook.py
to use memcache
The Guestbook application queries the Datastore on every request (via ndb, so it already gains some of the memcache speed-ups). You can modify the Guestbook application to use memcache explicitly before resorting to querying the Datastore.
First we'll import the memcache module and create the method that checks memcache before running a query.
Next we'll separate out the querying and creation of the HTML for the page. When we don't hit the cache, we'll call this method to query the Datastore and build the HTML string that we'll store in memcache.
Finally we will update the MainPage
handler to call the get_greetings() method and display some stats about the number of times the cache was hit or missed.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-16 UTC."],[[["This document provides Python code examples for utilizing memcache, a high-performance, distributed memory object caching system for fast data access."],["Memcache follows a pattern where the application first checks if the needed data is in the cache, and if not, it queries the Datastore and stores the results in memcache for future use."],["You can set values in memcache using `add()`, `set_multi()`, and `set()` methods, including options for cache expiration and atomic increments of integer values."],["The document explains how to integrate explicit memcache usage into the Guestbook application, which initially relies solely on the Datastore, to improve performance."],["The guestbook application example shows a `get_greetings()` method that queries memcache for cached greetings before querying the Datastore and displays cache statistics."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4