English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# NDB Context class

#### Experimental!

NDB is an experimental, innovative, and rapidly changing new feature for App Engine. Unfortunately, being on the bleeding edge means that we may make backwards-incompatible changes to NDB. We will inform the community when this feature is no longer experimental.

An application can use a Context 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()`
  - `flush()`
  - `get_cache_policy()`
  - `get_datastore_policy()`
  - `set_memcache_policy()`
  - `set_memcache_timeout_policy()`
- Memcache and Urlfetch Instance Methods
  - `get_memcache_policy()`
  - `get_memcache_timeout_policy()`
  - `memcache_add()`
  - `memcache_cas()`
  - `memcache_decr()`
  - `memcache_delete()`
  - `memcache_get()`
  - `memcache_gets()`
  - `memcache_incr()`
  - `memcache_replace()`
  - `memcache_set()`
  - `set_cache_policy()`
  - `set_datastore_policy()`
  - `urlfetch()`
- Static Methods
  - `default_cache_policy()`
  - `default_datastore_policy()`
  - `default_memcache_policy()`
  - `default_memcache_timeout_policy()`

## 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 a `Future`. To wait for the flushing to finish, call this object's `wait()` method.

get_cache_policy()
  Returns the cache policy function. This policy function takes a Key instance argument and returns a `bool` indicating whether the entity with this key should be cached in the in-context cache. May be `None` (meaning use `default_cache_policy`).
get_datastore_policy()
  Returns the current context Datastore policy function. This policy function takes a Key instance argument and returns a `bool` indicating if it should use the Datastore. May be `None` (meaning use `default_datastore_policy`).
set_memcache_policy(func)
  Sets the memcache policy function.

  Arguments

  func
    This function takes a Key instance argument and returns a `bool` indicating whether the entity with this key should be cached in the in-context cache. May be `None`.
set_memcache_timeout_policy(func)
  Sets the memcache timeout policy function.

  Arguments

  func
    This function takes a `Key` instance argument and returns the desired memcache timeout in seconds (or returns zero to use the default timeout). May be `None`.

## Memcache and Urlfetch Instance Methods

get_memcache_policy()
  Returns the current memcache policy function. This policy function takes a Key instance argument and returns a `bool` indicating if it should be cached. May be `None` (meaning use `default_memcache_policy`).
get_getmemcache_timeout_policy()
  Returns the current memcache timeout policy function. This policy function takes a Key instance argument and returns the desired memcache timeout in seconds (or returns zero to use the default timeout). May be `None`, which uses `default_memcache_timeout_policy`.
memcache_add(key, value, time, namespace)
  Async auto-batching memcache `add()`.
memcache_cas(key, value, time, namespace)
  Async auto-batching memcache `Client` `cas()` (compare-and-swap).
memcache_decr(key, delta, initial_value, namespace)
  Async auto-batching memcache `decr()`.
memcache_delete(key, seconds, namespace)
  Async auto-batching memcache `delete()`.
memcache_get(key, namespace, use_cache)
  Async auto-batching memcache `get()`.

  Returns a `Future` whose return value is the value retrieved from memcache or `None`.

memcache_gets(key, namespace, use_cache)
  Async auto-batching memcache `gets()`.
memcache_incr(key, delta, initial_value, namespace)
  Async auto-batching memcache `incr()`.
memcache_replace(key, value, time, namespace)
  Async auto-batching memcache `replace()`.
memcache_set(key, value, time, namespace, use_cache)
  Async auto-batching memcache `set()`.
set_cache_policy(func)
  Sets the cache policy function.

  Arguments

  func
    This function takes a Key instance argument and returns a `bool` indicating whether the entity with this key should be cached in the in-context cache. May be `None`.
set_datastore_policy(func)
  Sets the Datastore policy function.

  Arguments

  func
    This function takes a Key instance argument and returns a `bool` indicating whether the entity with this key should be stored in the persistent Datastore. May be `None`.
urlfetch(url, payload, method, headers, allow_truncated, follow_redirects, validate_certificate, deadline, callback)
  Async auto-batching urlfetch `fetch().`

## 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, return `None`.
default_datastore_policy(key)
  Checks for a `_use_datastore` class variable on the entity's model class; if there is one, return it. Otherwise, return `None`.
default_memcache_policy(key)
  Checks for a `_use_memcache` class variable on the entity's model class; if there is one, return it. Otherwise, return `None`.
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, return `None`.