SingleRedis¶
- class deepfos.lib.redis.SingleRedis(url=None, host=None, port=None, db=None, password=None, **kwargs)¶
- class Redis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, socket_connect_timeout=None, socket_keepalive=None, socket_keepalive_options=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', encoding_errors='strict', charset=None, errors=None, decode_responses=False, retry_on_timeout=False, retry_on_error=None, ssl=False, ssl_keyfile=None, ssl_certfile=None, ssl_cert_reqs='required', ssl_ca_certs=None, ssl_ca_path=None, ssl_ca_data=None, ssl_check_hostname=False, ssl_password=None, ssl_validate_ocsp=False, ssl_validate_ocsp_stapled=False, ssl_ocsp_context=None, ssl_ocsp_expected_cert=None, max_connections=None, single_connection_client=False, health_check_interval=0, client_name=None, username=None, retry=None, redis_connect_func=None, credential_provider=None)¶
Implementation of the Redis protocol.
This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol.
Pipelines derive from this, implementing how the commands are sent and received to the Redis server. Based on configuration, an instance will either use a ConnectionPool, or Connection object to talk to redis.
It is not safe to pass PubSub or Pipeline objects between threads.
- client()¶
- close()¶
- execute_command(*args, **options)¶
Execute a command and return a parsed response
- classmethod from_url(url, **kwargs)¶
Return a Redis client object configured from the given URL
For example:
redis://[[username]:[password]]@localhost:6379/0 rediss://[[username]:[password]]@localhost:6379/0 unix://[username@]/path/to/socket.sock?db=0[&password=password]
Three URL schemes are supported:
redis:// creates a TCP socket connection. See more at: <https://www.iana.org/assignments/uri-schemes/prov/redis>
rediss:// creates a SSL wrapped TCP socket connection. See more at: <https://www.iana.org/assignments/uri-schemes/prov/rediss>
unix://: creates a Unix Domain Socket connection.
The username, password, hostname, path and all querystring values are passed through urllib.parse.unquote in order to replace any percent-encoded values with their corresponding characters.
There are several ways to specify a database number. The first value found will be used:
A
dbquerystring option, e.g. redis://localhost?db=0If using the redis:// or rediss:// schemes, the path argument of the url, e.g. redis://localhost/0
A
dbkeyword argument to this function.
If none of these options are specified, the default db=0 is used.
All querystring options are cast to their appropriate Python types. Boolean arguments can be specified with string values “True”/”False” or “Yes”/”No”. Values that cannot be properly cast cause a
ValueErrorto be raised. Once parsed, the querystring arguments and keyword arguments are passed to theConnectionPool’s class initializer. In the case of conflicting arguments, querystring arguments always win.
- get_connection_kwargs()¶
Get the connection’s key-word arguments
- get_encoder()¶
Get the connection pool’s encoder
- load_external_module(funcname, func)¶
This function can be used to add externally defined redis modules, and their namespaces to the redis client.
funcname - A string containing the name of the function to create func - The function, being added to this class.
ex: Assume that one has a custom redis module named foomod that creates command named ‘foo.dothing’ and ‘foo.anotherthing’ in redis. To load function functions into this namespace:
from redis import Redis from foomodule import F r = Redis() r.load_external_module(“foo”, F) r.foo().dothing(‘your’, ‘arguments’)
For a concrete example see the reimport of the redisjson module in tests/test_connection.py::test_loading_external_modules
- lock(name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, lock_class=None, thread_local=True)¶
Return a new Lock object using key
namethat mimics the behavior of threading.Lock.If specified,
timeoutindicates a maximum life for the lock. By default, it will remain locked until release() is called.sleepindicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.blockingindicates whether callingacquireshould block until the lock has been acquired or to fail immediately, causingacquireto return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing ablockingargument toacquire.blocking_timeoutindicates the maximum amount of time in seconds to spend trying to acquire the lock. A value ofNoneindicates continue trying forever.blocking_timeoutcan be specified as a float or integer, both representing the number of seconds to wait.lock_classforces the specified lock implementation. Note that as of redis-py 3.0, the only lock class we implement isLock(which is a Lua-based lock). So, it’s unlikely you’ll need this parameter, unless you have created your own custom lock class.thread_localindicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline:- time: 0, thread-1 acquires my-lock, with a timeout of 5 seconds.
thread-1 sets the token to “abc”
- time: 1, thread-2 blocks trying to acquire my-lock using the
Lock instance.
- time: 5, thread-1 has not yet completed. redis expires the lock
key.
- time: 5, thread-2 acquired my-lock now that it’s available.
thread-2 sets the token to “xyz”
- time: 6, thread-1 finishes its work and calls release(). if the
token is not stored in thread local storage, then thread-1 would see the token value as “xyz” and would be able to successfully release the thread-2’s lock.
In some use cases it’s necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn’t disabled in this case, the worker thread won’t see the token set by the thread that acquired the lock. Our assumption is that these cases aren’t common and as such default to using thread local storage.
- monitor()¶
- parse_response(connection, command_name, **options)¶
Parses a response from the Redis server
- pipeline(transaction=True, shard_hint=None)¶
Return a new pipeline object that can queue multiple commands for later execution.
transactionindicates whether all commands should be executed atomically. Apart from making a group of operations atomic, pipelines are useful for reducing the back-and-forth overhead between the client and server.
- pubsub(**kwargs)¶
Return a Publish/Subscribe object. With this object, you can subscribe to channels and listen for messages that get published to them.
- set_response_callback(command, callback)¶
Set a custom Response Callback
- transaction(func, *watches, **kwargs)¶
Convenience method for executing the callable func as a transaction while watching all keys specified in watches. The ‘func’ callable should expect a single argument which is a Pipeline object.