baseplate.clients.redis¶
Redis is an in-memory data structure store used where speed is necessary but complexity is beyond simple key-value operations. (If you’re just doing caching, prefer memcached). Redis-py is a Python client library for Redis.
Example¶
To integrate redis-py with your application, add the appropriate client declaration to your context configuration:
baseplate.configure_context(
app_config,
{
...
"foo": RedisClient(),
...
}
)
configure it in your application’s configuration file:
[app:main]
...
# required: what redis instance to connect to
foo.url = redis://localhost:6379/0
# optional: the maximum size of the connection pool
foo.max_connections = 99
# optional: how long to wait for a connection to establish
foo.socket_connect_timeout = 3 seconds
# optional: how long to wait for a command to execute
foo.socket_timeout = 200 milliseconds
...
and then use the attached Redis-like object in
request:
def my_method(request):
request.foo.ping()
Configuration¶
-
class
baseplate.clients.redis.RedisClient(**kwargs)[source]¶ Configure a Redis client.
This is meant to be used with
baseplate.Baseplate.configure_context().See
pool_from_config()for available configuration settings.
-
baseplate.clients.redis.pool_from_config(app_config, prefix='redis.', **kwargs)[source]¶ Make a ConnectionPool from a configuration dictionary.
The keys useful to
pool_from_config()should be prefixed, e.g.redis.url,redis.max_connections, etc. Theprefixargument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on theredis.ConnectionPoolconstructor.Supported keys:
url(required): a URL likeredis://localhost/0.max_connections: an integer maximum number of connections in the poolsocket_connect_timeout: how long to wait for sockets to connect. e.g.200 milliseconds(Timespan())
socket_timeout: how long to wait for socket operations, e.g.200 milliseconds(Timespan())
Return type: ConnectionPool
Classes¶
-
class
baseplate.clients.redis.RedisContextFactory(connection_pool)[source]¶ Redis client context factory.
This factory will attach a
MonitoredRedisConnectionto an attribute on theRequestContext. When Redis commands are executed via this connection object, they will use connections from the providedredis.ConnectionPooland automatically record diagnostic information.Parameters: connection_pool ( ConnectionPool) – A connection pool.
-
class
baseplate.clients.redis.MonitoredRedisConnection(context_name, server_span, connection_pool)[source]¶ Redis connection that collects diagnostic information.
This connection acts like
redis.StrictRedisexcept that all operations are automatically wrapped with diagnostic collection.The interface is the same as that class except for the
pipeline()method.Note
Locks and PubSub are currently unsupported.
-
execute_command(*args, **kwargs)[source]¶ Execute a command and return a parsed response
Return type: Any
-
-
class
baseplate.clients.redis.MessageQueue(name, client)[source]¶ A Redis-backed variant of
MessageQueue.Parameters: - name (
str) – can be any string. - client (
ConnectionPool) – should be aredis.ConnectionPoolorredis.BlockingConnectionPoolfrom which a client connection can be created from (preferably generated from thepool_from_config()helper).
-
get(timeout=None)[source]¶ Read a message from the queue.
Parameters: timeout ( Optional[float]) – If the queue is empty, the call will block up totimeoutseconds or forever ifNone, if a float is given, it will be rounded up to be an integerRaises: TimedOutErrorThe queue was empty for the allowed duration of the call.Return type: bytes
- name (