Context
Context for currently running tasks and transactions.
-
class
google.cloud.ndb.context.
Context
[source]
Bases: google.cloud.ndb.context._Context
User management of cache and other policy.
Create new instance of _ContextTuple(client, eventloop, stub, batches, commit_batches, transaction, cache, global_cache, on_commit_callbacks, legacy_data)
-
call_on_commit
(callback)[source]
Call a callback 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 the 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.)
- Parameters
callback (Callable) – The callback function.
-
clear_cache
()[source]
Clears the in-memory cache.
This does not affect global cache.
-
flush
()[source]
Force any pending batch operations to go ahead and run.
-
get_cache_policy
()[source]
Return the current context cache policy function.
- Returns
- A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should be cached. May be None
.
- Return type
Callable
-
get_datastore_policy
()[source]
Return the current context datastore policy function.
- Returns
- A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should use the datastore. May be None
.
- Return type
Callable
-
get_global_cache_policy
()[source]
Return the current global cache policy function.
- Returns
- A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should be cached. May be None
.
- Return type
Callable
-
get_global_cache_timeout_policy
()[source]
Return the current policy function global cache timeout (expiration).
- Returns
- A function that accepts a
Key
instance as a single
positional argument and returns an int
indicating the
timeout, in seconds, for the key. 0
implies the default
timeout. May be None
.
- Return type
Callable
-
get_memcache_policy
()
Return the current global cache policy function.
- Returns
- A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should be cached. May be None
.
- Return type
Callable
-
get_memcache_timeout_policy
()
Return the current policy function global cache timeout (expiration).
- Returns
- A function that accepts a
Key
instance as a single
positional argument and returns an int
indicating the
timeout, in seconds, for the key. 0
implies the default
timeout. May be None
.
- Return type
Callable
-
in_transaction
()[source]
Get whether a transaction is currently active.
- Returns
True
if currently in a transaction, otherwiseFalse
.
- Return type
bool
-
memcache_add
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_cas
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_decr
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_delete
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_get
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_gets
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_incr
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_replace
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
memcache_set
(*args, **kwargs)[source]
Direct pass-through to memcache client.
-
set_cache_policy
(policy)[source]
Set the context cache policy function.
- Parameters
policy (Callable) – A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should be cached. May be None
.
-
set_datastore_policy
(policy)[source]
Set the context datastore policy function.
- Parameters
policy (Callable) – A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should use the datastore. May be None
.
-
set_global_cache_policy
(policy)[source]
Set the global cache policy function.
- Parameters
policy (Callable) – A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should be cached. May be None
.
-
set_global_cache_timeout_policy
(policy)[source]
Set the policy function for global cache timeout (expiration).
- Parameters
policy (Callable) – A function that accepts a
Key
instance as a single
positional argument and returns an int
indicating the
timeout, in seconds, for the key. 0
implies the default
timeout. May be None
.
-
set_memcache_policy
(policy)
Set the global cache policy function.
- Parameters
policy (Callable) – A function that accepts a
Key
instance as a single
positional argument and returns a bool
indicating if it
should be cached. May be None
.
-
set_memcache_timeout_policy
(policy)
Set the policy function for global cache timeout (expiration).
- Parameters
policy (Callable) – A function that accepts a
Key
instance as a single
positional argument and returns an int
indicating the
timeout, in seconds, for the key. 0
implies the default
timeout. May be None
.
-
urlfetch
(*args, **kwargs)[source]
Fetch a resource using HTTP.
-
google.cloud.ndb.context.
get_context
(raise_context_error=True)[source]
Get the current context.
This function should be called within a context established by
google.cloud.ndb.client.Client.context()
.
- Parameters
raise_context_error (bool) – If set to True
, will raise an
exception if called outside of a context. Set this to False
in order to have it just return None
if called outside of a
context. Default: True
- Returns
The current context.
- Return type
Context
- Raises
ContextError – If called outside of a context
established by google.cloud.ndb.client.Client.context()
and
raise_context_error
is True
.