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
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_clear_cache`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_flush`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_get_cache_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_get_datastore_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_get_memcache_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_get_memcache_timeout_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_set_cache_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_set_datastore_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_set_memcache_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_set_memcache_timeout_policy`
- Memcache and Urlfetch Instance Methods
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_add`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_cas`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_decr`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_delete`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_get`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_gets`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_incr`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_replace`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_memcache_set`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_urlfetch`
- Transaction Management Methods
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_call_on_commit`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_in_transaction`
- Static Methods
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_cache_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_datastore_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_memcache_policy`
  - `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_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](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass)
    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
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_cache_policy`).

get_datastore_policy()
:   Returns the current context Datastore policy function.
    This policy function takes a [Key](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass)
    instance argument and returns
    a `bool` indicating if it should use the Datastore.
    May be `None` (meaning use
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_datastore_policy`).

get_memcache_policy()
:   Returns the current memcache policy function.
    This policy function takes a [Key](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass)
    instance argument and returns
    a `bool` indicating if it should be cached.
    May be `None` (meaning use
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_memcache_policy`).

get_memcache_timeout_policy()
:   Returns the current memcache timeout policy function.
    This policy function takes a [Key](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass)
    instance argument and returns
    the desired memcache timeout in seconds (or returns
    zero to use the default timeout).
    May be `None`, which uses
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/contextclass#Context_default_memcache_timeout_policy`.

set_cache_policy(func)

:   Sets the cache policy function. **Arguments**

    func
    :   This function takes a
        [Key](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass) 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](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass) instance argument and
        returns a `bool` indicating whether the entity
        with this key should be stored in the persistent Datastore.
        May be `None`.

set_memcache_policy(func)

:   Sets the memcache policy function. **Arguments**

    func
    :   This function takes a
        [Key](https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass) 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
        `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/keyclass` 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


The full list of methods is found in the [Memcache](https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache) API reference. The list below highlights some of the more
commonly used methods:

memcache_add(key, value, time, namespace)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#add`.

memcache_cas(key, value, time, namespace)
:   Async auto-batching memcache `Client`
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#cas` (compare-and-swap).

memcache_decr(key, delta, initial_value, namespace)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#decr`.

memcache_delete(key, seconds, namespace)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#delete`.

memcache_get(key, namespace, use_cache)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#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
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#gets`.

memcache_incr(key, delta, initial_value, namespace)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#incr`.

memcache_replace(key, value, time, namespace)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#replace`.

memcache_set(key, value, time, namespace, use_cache)
:   Async auto-batching memcache
    `https://docs.cloud.google.com/appengine/docs/standard/python3/reference/services/bundled/google/appengine/api/memcache/Client#set`.

urlfetch(url, payload, method, headers, allow_truncated, follow_redirects, validate_certificate, deadline, callback)
:   Async auto-batching urlfetch
    `https://docs.cloud.google.com/appengine/docs/standard/services/urlfetch/issue-requests.`

## 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 the `transaction()` 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()
:   Returns `True` if this Context represents
    a transaction, `False` otherwise.

## Static Methods

default_cache_policy(key)
:   Checks for a
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/modelclass#Model__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
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/modelclass#Model__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
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/modelclass#Model__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
    `https://docs.cloud.google.com/appengine/docs/standard/services/ndb/modelclass#Model__memcache_timeout`
    class variable on the entity's model class; if there is one, return it.
    Otherwise, return `None`.