Context

GlobalCache interface and its implementations.

class google.cloud.ndb.global_cache.GlobalCache[source]

Bases: object

Abstract base class for a global entity cache.

A global entity cache is shared across contexts, sessions, and possibly even servers. A concrete implementation is available which uses Redis.

Essentially, this class models a simple key/value store where keys and values are arbitrary bytes instances. “Compare and swap”, aka “optimistic transactions” should also be supported.

Concrete implementations can either by synchronous or asynchronous. Asynchronous implementations should return Future instances whose eventual results match the return value described for each method. Because coordinating with the single threaded event model used by NDB can be tricky with remote services, it’s not recommended that casual users write asynchronous implementations, as some specialized knowledge is required.

abstract compare_and_swap(items, expires=None)[source]

Like set() but using an optimistic transaction.

Only keys whose values haven’t changed since a preceding call to watch() will be changed.

Parameters
  • items (Dict[bytes, Union[bytes, None]]) – Mapping of keys to serialized entities.

  • expires (Optional[float]) – Number of seconds until value expires.

abstract delete(keys)[source]

Remove entities from the cache.

Parameters

keys (List[bytes]) – The keys to remove.

abstract get(keys)[source]

Retrieve entities from the cache.

Parameters

keys (List[bytes]) – The keys to get.

Returns

Serialized entities, or None,

for each key.

Return type

List[Union[bytes, None]]]

abstract set(items, expires=None)[source]

Store entities in the cache.

Parameters
  • items (Dict[bytes, Union[bytes, None]]) – Mapping of keys to serialized entities.

  • expires (Optional[float]) – Number of seconds until value expires.

abstract watch(keys)[source]

Begin an optimistic transaction for the given keys.

A future call to compare_and_swap() will only set values for keys whose values haven’t changed since the call to this method.

Parameters

keys (List[bytes]) – The keys to watch.

class google.cloud.ndb.global_cache.RedisCache(redis)[source]

Bases: google.cloud.ndb.global_cache.GlobalCache

Redis implementation of the GlobalCache.

This is a synchronous implementation. The idea is that calls to Redis should be fast enough not to warrant the added complexity of an asynchronous implementation.

Parameters

redis (redis.Redis) – Instance of Redis client to use.

compare_and_swap(items, expires=None)[source]

Implements GlobalCache.compare_and_swap().

delete(keys)[source]

Implements GlobalCache.delete().

classmethod from_environment()[source]

Generate a class:RedisCache from an environment variable.

This class method looks for the REDIS_CACHE_URL environment variable and, if it is set, passes its value to Redis.from_url to construct a Redis instance which is then used to instantiate a RedisCache instance.

Returns

A RedisCache instance or

None, if REDIS_CACHE_URL is not set in the environment.

Return type

Optional[RedisCache]

get(keys)[source]

Implements GlobalCache.get().

set(items, expires=None)[source]

Implements GlobalCache.set().

watch(keys)[source]

Implements GlobalCache.watch().