A Connection instance will take care of the
connection and will automatically reconnect, using a new transport when the
connection drops. This connection class also acts as a proxy to at
RedisProtocol instance; any Redis
command of the protocol can be called directly at the connection.

importtrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():# Create Redis connectionconnection=yieldFrom(trollius_redis.Connection.create(host=u'localhost',port=6379))# Set a keyyieldFrom(connection.set(u'my_key',u'my_value'))# When finished, close the connection.connection.close()if__name__=='__main__':loop=trollius.get_event_loop()loop.run_until_complete(example())

Requests will automatically be distributed among all connections in a
Pool. If a connection is blocking because of
–for instance– a blocking rpop, another connection will be used for new
commands.

Note

This is the recommended way to connect to the Redis server.

importtrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():# Create Redis connectionconnection=yieldFrom(trollius_redis.Pool.create(host=u'localhost',port=6379,poolsize=10))# Set a keyyieldFrom(connection.set(u'my_key',u'my_value'))# When finished, close the connection pool.connection.close()

A transaction can be started by calling multi. This returns a Transaction instance which is in fact just a proxy to the
RedisProtocol, except that every Redis
method of the protocol now became a coroutine that returns a future. The
results of these futures can be retrieved after the transaction is commited
with exec.

The redis protocol only knows about bytes, but normally you want to use strings
in your Python code. trollius_redis is helpful and installs an encoder that
does this conversion automatically, using the UTF-8 codec. However, sometimes
you want to access raw bytes. This is possible by passing a
BytesEncoder instance to the
connection, pool or protocol.

importtrolliusfromtrolliusimportFromimporttrollius_redisfromtrollius_redis.encodersimportBytesEncoder@trollius.coroutinedefexample():# Create Redis connectionconnection=yieldFrom(trollius_redis.Connection.create(host=u'localhost',port=6379,encoder=BytesEncoder()))# Set a keyyieldFrom(connection.set(b'my_key',b'my_value'))# When finished, close the connection.connection.close()

Redis has a few nice scanning utilities to discover keys in the database. They
are rather low-level, but trollius_redis exposes a simple
Cursor class that allows you to iterate over
all the keys matching a certain pattern. Each call of the
fetchone() coroutine will return the next
match. You don’t have have to worry about accessing the server every x pages.

The most low level way of accessing the redis server through this library is
probably by creating a connection with the RedisProtocol yourself. You can do
it as follows:

importtrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():loop=trollius.get_event_loop()# Create Redis connectiontransport,protocol=yieldFrom(loop.create_connection(trollius_redis.RedisProtocol,u'localhost',6379))# Set a keyyieldFrom(protocol.set(u'my_key',u'my_value'))# Get a keyresult=yieldFrom(protocol.get(u'my_key'))print(result)if__name__=='__main__':trollius.get_event_loop().run_until_complete(example())

Note

It is not recommended to use the Protocol class directly, because the
low-level Redis implementation could change. Prefer the
Connection or Pool class as demonstrated above if possible.