asyncredis package

Submodules

asyncredis.connection module

class asyncredis.connection.RedisConnection(**options)

Bases: object

async read_message(n: int = - 1) bytes

Read from the open IO stream.

Parameters

n (int, optional) – The number of bytes to read, defaults to -1

Returns

The bytes read from the IO stream

Return type

bytes

async send_message(blob: bytes)

Send a blob of bytes to the Redis server.

Parameters

blob (bytes) – The bytes you want to send

async socket_close()

Close the TCP socket connection, by closing the transport, and closing the writer.

async socket_connect()

Open a TCP socket connection to the Redis server.

asyncredis.encoders module

asyncredis.helpers module

async asyncredis.helpers.connect(uri: str) asyncredis.redis.Redis

A helper function to parse and prepare a connection from a connection uri string.

Parameters

uri (str) – Your connection string

Returns

A Redis client instance to let you interact with your Redis server

Return type

Redis

asyncredis.parser module

class asyncredis.parser.RedisParser(encoding: str = 'utf-8', errors: str = 'strict')

Bases: object

parse_message(message: bytes, *, decode: bool = True) Union[str, bytes]

Parse a message that’s been received from the Redis server.

Parameters
  • message (bytes) – The received message in bytes

  • decode (bool, optional) – Whether or not the function should handle decoding the bytes after parsing, defaults to True

Raises

ValueError – When Incomplete data has been passed to the parser

Returns

The parsed value

Return type

Union[str, bytes]

asyncredis.redis module

class asyncredis.redis.Redis(connection: asyncredis.connection.RedisConnection, socket_read_size: int = 65536)

Bases: object

A class representing a client interacting with a Redis server.

async close()

Ask the Redis server to close our connection, process any remaining responses, and cleanup.

command(command: str, *args) bytes

Generate the appropriate bytes to send across for a command

Parameters

command (str) – Command Name

Returns

The encoded bytes (utf-8)

Return type

bytes

async copy(existing_key: str, new_key: str) bool

Copy the value of a key-value pair, into another pair.

Parameters
  • existing_key (str) – The key you want to copy from

  • new_key (str) – The name of the new key you want to copy to

Returns

Whether the COPY was actually performed. Could return False

Return type

bool

async delete(*keys) int

Delete some keys.

Parameters

keys – A consume-rest arg. You would pass it as so: .delete(“mykey”, “mykey2”, “mykey3”)

Returns

The amount of keys that ended up being deleted

Return type

int

async dump(key: str) Optional[bytes]

Serialize the value stored at key in a Redis-specific format and return it to the user.

Parameters

key (bytes) – The associated key for the value you want to serialize

Returns

The serialized value. Could return None if the key was not found, or if something went wrong

Return type

Optional[bytes]

async execute_command(data: bytes) bytes

Executes a bytes-encoded command by sending it to the Redis server.

Parameters

data (bytes) – The bytes-encoded version of the command string

Returns

The bytes read from the IO stream after sending the message across

Return type

bytes

async exists(key: str) bool

Check whether a key exists.

Parameters

key (str) – The key to check

Returns

Whether or not the key exists. True - Exists. False - Doesn’t exist

Return type

bool

async existsmany(*keys) int

Checks whether the provided keys exist and returns a number representing how many keys actually existed.

Parameters

keys – A consume-rest arg. You would pass it as so: .existsmany(“mykey”, “mykey2”, “mykey3”)

Returns

The amount of keys that existed compared to the ones you provided

Return type

int

classmethod from_url(connection_uri: str)

Create a RedisConnection instance from a connection uri.

Parameters

connection_uri (str) – Your redis connection uri

Returns

A Redis client instance

Return type

Redis

async get(key: str) Optional[str]

Get a value from the Redis server with the associated key.

Parameters

key (str) – The associated key

Returns

The value that was found, or None, if no value was found

Return type

Optional[str]

async prepare()

“Prepares” the Redis client for appropriate connections. Tries to connect to the socket 5 times.

async set(key: str, value: str, *, timeout: Optional[int] = None) Optional[str]

Set a key-value pair.

Parameters
  • key (str) – The key you want to set

  • value (str) – The assosciated value you want to set

  • timeout (Optional[int], optional) – How many seconds before the key-value pair expires, defaults to None

Returns

The value returned by the Redis server. We parse the response, but we let you handle it for flexibility

Return type

Optional[str]

Module contents