Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

NDB Context class

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 themigration guide to learn about your migration options for legacy bundled services.

An application can use aContext to control its caching policy.Context also offers convenient asynchronous APIs for Memcache and URL Fetch integrated into NDB's asynchronous facilities; in particular, the Memcache API supports auto-batching.

Cache Management Instance Methods

clear_cache()
Clears the in-context cache.
flush()
Flush all auto-batcher queues (sending their work to the servers). The flush happens asynchronously.

Returns aFuture. To wait for the flushing to finish, call this object'swait() method.

get_cache_policy()
Returns the cache policy function. This policy function takes aKey instance argument and returns abool indicating whether the entity with this key should be cached in the in-context cache. May beNone (meaning use default_cache_policy).
get_datastore_policy()
Returns the current context Datastore policy function. This policy function takes aKey instance argument and returns abool indicating if it should use the Datastore. May beNone (meaning use default_datastore_policy).
get_memcache_policy()
Returns the current memcache policy function. This policy function takes aKey instance argument and returns abool indicating if it should be cached. May beNone (meaning use default_memcache_policy).
get_memcache_timeout_policy()
Returns the current memcache timeout policy function. This policy function takes aKey instance argument and returns the desired memcache timeout in seconds (or returns zero to use the default timeout). May beNone, which uses default_memcache_timeout_policy.
set_cache_policy(func)
Sets the cache policy function.

Arguments

func
This function takes aKey instance argument and returns abool indicating whether the entity with this key should be cached in the in-context cache. May beNone.
set_datastore_policy(func)
Sets the Datastore policy function.

Arguments

func
This function takes aKey instance argument and returns abool indicating whether the entity with this key should be stored in the persistent Datastore. May beNone.
set_memcache_policy(func)
Sets the memcache policy function.

Arguments

func
This function takes aKey instance argument and returns abool indicating whether the entity with this key should be cached in the in-context cache. May beNone.
set_memcache_timeout_policy(func)
Sets the memcache timeout policy function.

Arguments

func
This function takes aKey instance argument and returns the desired memcache timeout in seconds (or returns zero to use the default timeout). May beNone.

Memcache and Urlfetch Instance Methods

The full list of methods is found in the Memcache API reference. The list below highlights some of the more commonly used methods:

memcache_add(key,value,time,namespace)
Async auto-batching memcache add().
memcache_cas(key,value,time,namespace)
Async auto-batching memcacheClient cas() (compare-and-swap).
memcache_decr(key,delta,initial_value,namespace)
Async auto-batching memcachedecr().
memcache_delete(key,seconds,namespace)
Async auto-batching memcachedelete().
memcache_get(key,namespace,use_cache)
Async auto-batching memcacheget().

Returns aFuture whose return value is the value retrieved from memcache orNone.

memcache_gets(key,namespace,use_cache)
Async auto-batching memcachegets().
memcache_incr(key,delta,initial_value,namespace)
Async auto-batching memcacheincr().
memcache_replace(key,value,time,namespace)
Async auto-batching memcachereplace().
memcache_set(key,value,time,namespace,use_cache)
Async auto-batching memcacheset().
urlfetch(url,payload,method,headers,allow_truncated,follow_redirects,validate_certificate,deadline,callback)
Async auto-batching urlfetch fetch().

Transaction Management Methods

call_on_commit(callback)
Queues a callback to call upon successful commit of a transaction.

If not in a transaction, the callback is called immediately.

In a transaction, multiple callbacks may be registered and will be called once the transaction commits in the order in which they were registered. If the transaction fails, the callbacks will not be called.

If a callback raises an exception, it bubbles up normally. This means: If the callback is called immediately, any exception it raises will bubble up immediately. If the call is postponed until commit, remaining callbacks will be skipped and the exception will bubble up through thetransaction() call. (However, the transaction is already committed at that point.)

Arguments

callback
Callback function. This callback function takes no parameters and returns nothing.
in_transaction()
ReturnsTrue if this Context represents a transaction,False otherwise.

Static Methods

default_cache_policy(key)
Checks for a_use_cache class variable on the entity's model class; if there is one, return it. Otherwise, returnNone.
default_datastore_policy(key)
Checks for a_use_datastore class variable on the entity's model class; if there is one, return it. Otherwise, returnNone.
default_memcache_policy(key)
Checks for a_use_memcache class variable on the entity's model class; if there is one, return it. Otherwise, returnNone.
default_memcache_timeout_policy(key)
Checks for a_memcache_timeout class variable on the entity's model class; if there is one, return it. Otherwise, returnNone.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.