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. The prefix argument specifies the prefix used to filter keys. Each key is mapped to a corresponding keyword argument on the redis.ConnectionPool constructor.

Supported keys:

  • url (required): a URL like redis://localhost/0.
  • max_connections: an integer maximum number of connections in the pool
  • socket_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:BlockingConnectionPool

Classes

class baseplate.clients.redis.RedisContextFactory(connection_pool)[source]

Redis client context factory.

This factory will attach a MonitoredRedisConnection to an attribute on the RequestContext. When Redis commands are executed via this connection object, they will use connections from the provided redis.ConnectionPool and automatically record diagnostic information.

Parameters:connection_pool (ConnectionPool) – A connection pool.
make_object_for_context(name, span)[source]

Return an object that can be added to the context object.

Parameters:
  • name (str) – The name assigned to this object on the context.
  • span (Span) – The current span this object is being made for.
Return type:

MonitoredRedisConnection

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.

Note

Locks and PubSub are currently unsupported.

execute_command(command, *args, **kwargs)[source]

Execute a command and return a parsed response

Return type:Any
pipeline(name, transaction=True, shard_hint=None)[source]

Create a pipeline.

This returns an object on which you can call the standard Redis commands. Execution will be deferred until execute is called. This is useful for saving round trips.

Parameters:
  • name (str) – The name to attach to diagnostics for this pipeline.
  • transaction (bool) – Whether or not the commands in the pipeline are wrapped with a transaction and executed atomically.
Return type:

MonitoredRedisPipeline

transaction(*args, **kwargs)[source]

Not currently implemented.

Return type:Any
lock(*args, **kwargs)[source]

Not currently implemented.

Return type:Any
pubsub(*args, **kwargs)[source]

Not currently implemented.

Return type:Any
class baseplate.clients.redis.MessageQueue(name, client)[source]

A Redis-backed variant of MessageQueue.

Parameters:
get(timeout=None)[source]

Read a message from the queue.

Parameters:timeout (Optional[float]) – If the queue is empty, the call will block up to timeout seconds or forever if None, if a float is given, it will be rounded up to be an integer
Raises:TimedOutError The queue was empty for the allowed duration of the call.
Return type:bytes
put(message, timeout=None)[source]

Add a message to the queue.

Parameters:message (bytes) – will be typecast to a string upon storage and will come out of the queue as a string regardless of what type they are when passed into this method.
Return type:None

Not implemented for Redis variant.

Return type:None
close()[source]

Close queue when finished.

Will delete the queue from the Redis server (Note, can still enqueue and dequeue as the actions will recreate the queue)

Return type:None