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 byNDB
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.
-
abstract
delete
(keys)[source]¶ Remove entities from the cache.
- Parameters
keys (List[bytes]) – The keys to remove.
-
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.
-
abstract
-
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 toRedis.from_url
to construct aRedis
instance which is then used to instantiate aRedisCache
instance.- Returns
- A
RedisCache
instance or None
, ifREDIS_CACHE_URL
is not set in the environment.
- A
- Return type
Optional[RedisCache]
-
get
(keys)[source]¶ Implements
GlobalCache.get()
.
-
set
(items, expires=None)[source]¶ Implements
GlobalCache.set()
.
-
watch
(keys)[source]¶ Implements
GlobalCache.watch()
.