Stay organized with collections Save and categorize content based on your preferences.
This page describes how to configure and monitor the memcache service for your application using the Google Cloud console. It also describes how to use the App Engine memcache Python API to set and retrieve cached values and use the compare-and-set feature to handle concurrent write requests to the same memcache key. To learn more about memcache, read the Memcache Overview.
This API is supported for first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes. 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. Configuring memcacheSelect the memcache service level you want to use:
Learn more about available service classes in Memcache Overview.
Use add()
to add a key's value if and only if it doesn't already exist, with an optional expiration time:
memcache.add(key="[KEY]", value="[VALUE]", time=[EXPIRATION_TIME])
For example, to add the value raining
to the key weather_USA_98105
with an expiration time of one hour from when the value is written:
memcache.add(key="weather_USA_98105", value="raining", time=3600)
Learn more about add()
and other methods for setting values in the memcache Python API documentation.
See other examples of caching values in Memcache Examples.
Looking up cached valuesUse get()
to look up the value of a single key:
memcache.get(key="[KEY]")
For example, to get the value of the key weather_USA_98105
:
memcache.get(key="weather_USA_98105")
Learn more about get()
and other methods for looking up values in the memcache Python API documentation.
You can take any of the following actions:
(Dedicated memcache only) Look through the list of Hot keys.
To use the compare and set feature to handle writes from multiple requests to the same memcache key:
Client
object.gets()
or get_multi()
with the for_cas
parameter set to True
.cas()
or cas_multi()
.The following snippet shows one way to use the compare and set feature:
def bump_counter(key):
client = memcache.Client()
while True: # Retry loop
counter = client.gets(key)
if counter is None: raise KeyError('Uninitialized counter')
if client.cas(key, counter+1):
break
The retry loop is necessary because without the loop this code doesn't actually avoid race conditions, it just detects them! The memcache service guarantees that when used in the pattern shown here (that is, using gets()
and cas()
), if two (or more) different client instances happen to be involved in a race condition, only the first one to execute the cas()
operation succeeds (return True
), while the second one (and subsequent ones) fails (return False
).
Another refinement you should add to this sample code is to set a limit on the number of retries, to avoid an infinite loop in worst-case scenarios where there is a lot of contention for the same counter. An example of when such contention could occur is if there are more requests trying to update the counter than the memcache service can process in real time.
Learn more about compare and set in the Memcache Overview.
What's nextExcept 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 guide explains how to configure and monitor the memcache service through the Google Cloud console, including switching between Shared and Dedicated service levels."],["You can cache and retrieve values using the memcache Python API methods like `add()` for caching and `get()` for retrieval, with the option to set expiration times."],["The memcache monitoring in the Google Cloud console offers insights into the service level, hit ratio, cache items, item age, total size, and dedicated memcache allows to see \"Hot keys\"."],["Concurrent write requests to the same memcache key can be handled using the compare-and-set (CAS) feature, implemented with a retry loop using `gets()` and `cas()`."],["This API is supported for first-generation runtimes and can be used when upgrading to the corresponding second-generation runtimes."]]],[]]
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