5.7.19. vlcp.utils.redisclient

Note

This document is generated from the source file.

View Source on GitHub

Created on 2016/1/8

author:hubo
class vlcp.utils.redisclient.RedisClient(url=None, db=None, protocol=None)
__init__(url=None, db=None, protocol=None)

Redis client to communicate with Redis server. Several connections are created for different functions.

Parameters:
  • url – connectiom url, e.g. ‘tcp://localhost/’. If not specified, redisclient.url in configuration is used
  • db – default database. If not specified, redisclient.db in configuration is used, which defaults to 0.
  • protocol – use a pre-created protocol instance instead of creating a new instance
execute_command(container, *args)
Execute command on Redis server:
  • For (P)SUBSCRIBE/(P)UNSUBSCRIBE, the command is sent to the subscribe connection. It is recommended to use (p)subscribe/(p)unsubscribe method instead of directly call the command
  • For BLPOP, BRPOP, BRPOPLPUSH, the command is sent to a separated connection. The connection is recycled after command returns.
  • For other commands, the command is sent to the default connection.
get_connection(container)

Get an exclusive connection, useful for blocked commands and transactions.

You must call release or shutdown (not recommanded) to return the connection after use.

Parameters:container – routine container
Returns:RedisClientBase object, with some commands same as RedisClient like execute_command, batch_execute, register_script etc.
make_connobj(container)

Return an object to be used like a connection. Put the connection-like object in module.connections to make RedisClient shutdown on module unloading.

psubscribe(container, *keys)

Subscribe to specified globs

Parameters:
  • container – routine container
  • *keys – subscribed globs
Returns:

list of event matchers for the specified globs

punsubscribe(container, *keys)

Unsubscribe specified globs. Every subscribed glob should be unsubscribed exactly once, even if duplicated subscribed.

Parameters:
  • container – routine container
  • *keys

    subscribed globs

shutdown(container, force=False)

Shutdown all connections. Exclusive connections created by get_connection will shutdown after release()

subscribe(container, *keys)

Subscribe to specified channels

Parameters:
  • container – routine container
  • *keys

    subscribed channels

Returns:

list of event matchers for the specified channels

subscribe_state_matcher(container, connected=True)

Return a matcher to match the subscribe connection status.

Parameters:
  • container – a routine container. NOTICE: this method is not a routine.
  • connected – if True, the matcher matches connection up. If False, the matcher matches connection down.
Returns:

an event matcher.

unsubscribe(container, *keys)

Unsubscribe specified channels. Every subscribed key should be unsubscribed exactly once, even if duplicated subscribed.

Parameters:
  • container – routine container
  • *keys – subscribed channels
class vlcp.utils.redisclient.RedisClientBase(conn=None, parent=None, protocol=None)

Connect to Redis server

__init__(conn=None, parent=None, protocol=None)

Initialize self. See help(type(self)) for accurate signature.

batch_execute(container, *cmds, raise_first_exception=False)

execute a batch of commands on current connection in pipeline mode

context(container, release=True, lockconn=True)

Use with statement to manage the connection

Params release:if True(default), release the connection when leaving with scope
Params lockconn:
 if True(default), do not allow reconnect during with scope; execute commands on a disconnected connection raises Exceptions.
ensure_registerd(container, *scripts)

Ensure that these scripts are cached on the server. Important when using scripts with batch_execute.

Parameters:
  • container – routine container.
  • *scripts – registered script tuples, return value of register_script
eval_registered(container, registerd_script, *args)

eval a registered script. If the script is not cached on the server, it is automatically cached.

execute_command(container, *args)

execute command on current connection

register_script(container, script)

register a script to this connection.

Returns:registered script. This is a tuple (sha1, script). Pass the tuple to eval_registered, ensure_registerd as registerd_script parameter.
release(container)

Release the connection, leave it to be reused later.

shutdown(container, force=False)

Shutdown all connections to Redis server

exception vlcp.utils.redisclient.RedisConnectionDown
exception vlcp.utils.redisclient.RedisConnectionRestarted