Module: redis_connector.py
Class and Functions
RedisConnector
class RedisConnector:
"""
RedisConnector facilitates connecting to a Redis data store.
It handles setting and retrieving key-value pairs.
"""
def __init__(self, username: str, password: str, host='localhost', port=6379, db=0):
"""
Initializes the connection to Redis.
Parameters:
username (str): Username for Redis authentication.
password (str): Password for Redis authentication.
host (str): Redis server host.
port (int): Redis server port.
db (int): Redis database number.
"""
def set_value(self, key, value):
"""
Sets a value in Redis under the specified key.
Parameters:
key (str): The key under which the value is stored.
value (str): The value to store.
"""
def get_value(self, key):
"""
Retrieves a value from Redis by key.
Parameters:
key (str): The key whose value to retrieve.
Returns:
str: The value stored under the key, or None if the key does not exist.
"""
def push_to_list(self, list_name, *values):
"""
Push values to the end of a list stored in Redis.
Parameters:
list_name (str): The name of the list.
values (str): One or more values to push to the list.
"""
def get_from_list(self, list_name, start=0, end=-1):
"""
Retrieve values from a list stored in Redis.
Parameters:
list_name (str): The name of the list.
start (int): Optional. The index of the first element to retrieve (default is 0).
end (int): Optional. The index of the last element to retrieve (default is -1, meaning all elements).
Returns:
list: A list of values within the specified range.
"""
def add_to_set(self, set_name, *values):
"""
Add values to a set stored in Redis.
Parameters:
set_name (str): The name of the set.
values (str): One or more values to add to the set.
"""
def get_from_set(self, set_name):
"""
Get all members of a set stored in Redis.
Parameters:
set_name (str): The name of the set.
Returns:
set: A set containing all members of the set.
"""
def set_hash_field(self, hash_name, key, value):
"""
Set the value of a field in a hash stored in Redis.
Parameters:
hash_name (str): The name of the hash.
key (str): The field key.
value (str): The value to set for the field.
"""
def get_hash_field(self, hash_name, key):
"""
Get the value associated with a field in a hash stored in Redis.
Parameters:
hash_name (str): The name of the hash.
key (str): The field key.
Returns:
str: The value associated with the field.
"""
def publish(self, channel, message):
"""
Publish a message to a channel in Redis Pub/Sub.
Parameters:
channel (str): The channel to which the message will be published.
message (str): The message to publish.
"""
def subscribe(self, channel):
"""
Subscribe to a channel in Redis Pub/Sub.
Parameters:
channel (str): The channel to subscribe to.
Returns:
redis.client.PubSub: A subscriber object.
"""