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. Theprefix
argument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on theredis.ConnectionPool
constructor.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
MonitoredRedisConnection
to an attribute on theRequestContext
. When Redis commands are executed via this connection object, they will use connections from the providedredis.ConnectionPool
and automatically record diagnostic information.Parameters: connection_pool ( ConnectionPool
) – A connection pool.-
report_runtime_metrics
(batch)[source]¶ Report runtime metrics to the stats system.
Parameters: batch ( Client
) – A metrics client to report statistics to.Return type: None
-
-
class
baseplate.clients.redis.
MonitoredRedisConnection
(context_name, server_span, connection_pool)[source]¶ Redis connection that collects diagnostic information.
This connection acts like
redis.StrictRedis
except that all operations are automatically wrapped with diagnostic collection.The interface is the same as that class except for the
pipeline()
method.-
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.ConnectionPool
orredis.BlockingConnectionPool
from 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 totimeout
seconds or forever ifNone
, if a float is given, it will be rounded up to be an integerRaises: TimedOutError
The queue was empty for the allowed duration of the call.Return type: bytes
- name (
Runtime Metrics¶
In addition to request-level metrics reported through spans, this wrapper reports connection pool statistics periodically via the Process-level metrics system. All metrics are prefixed as follows:
{namespace}.runtime.{hostname}.PID{pid}.clients.{name}
where namespace
is the application’s namespace, hostname
and pid
come from the operating system, and name
is the name given to
add_to_context()
when registering this
context factory.
The following metrics are reported:
pool.size
- The size limit for the connection pool.
pool.in_use
- How many connections have been established and are currently checked out and being used.
New in version 1.5.