Redis built into a python package
—
Enhanced Redis client classes that automatically manage embedded Redis server instances. These classes provide all the functionality of redis-py Redis classes with additional embedded server management capabilities.
Enhanced version of the redis.Redis() class that uses an embedded redis-server by default.
class Redis(RedisMixin, redis.Redis):
"""
Enhanced Redis client with embedded server management.
Parameters:
dbfilename (str, optional): Path to the redis rdb file to back the redis instance
serverconfig (dict, optional): Dictionary containing redis server settings
host (str, optional): Hostname or IP address of redis server to connect to
port (int, optional): Port number of redis server to connect to
**kwargs: All other keyword arguments supported by redis.Redis()
Attributes:
db (str): The fully qualified filename associated with the redis dbfilename
pid (int): Pid of the running embedded redis server (read only)
redis_log (str): The contents of the redis-server log file
start_timeout (float): Number of seconds to wait for server start
"""
def redis_log_tail(self, lines=1, width=80):
"""
The redis log output.
Parameters:
lines (int, optional): Number of lines from the end of logfile to return
width (int, optional): Expected average width of a log file line
Returns:
list: List of strings containing the lines from the logfile
"""Usage Examples:
from redislite import Redis
# Create with default temporary database
redis_conn = Redis()
# Create with specific database file
redis_conn = Redis('/tmp/my_redis.db')
# Create with custom server configuration
redis_conn = Redis(
'/tmp/redis.db',
serverconfig={
'port': '8002',
'databases': '32'
}
)
# Use like any Redis connection
redis_conn.set('key', 'value')
value = redis_conn.get('key')
# Access server information
print(f"Server PID: {redis_conn.pid}")
print(f"Database file: {redis_conn.db}")
# View server logs
recent_logs = redis_conn.redis_log_tail(lines=10)
all_logs = redis_conn.redis_logEnhanced version of the redis.StrictRedis() class that uses an embedded redis-server by default.
class StrictRedis(RedisMixin, redis.StrictRedis):
"""
Enhanced StrictRedis client with embedded server management.
Parameters:
dbfilename (str, optional): Path to the redis rdb file to back the redis instance
serverconfig (dict, optional): Dictionary containing redis server settings
host (str, optional): Hostname or IP address of redis server to connect to
port (int, optional): Port number of redis server to connect to
**kwargs: All other keyword arguments supported by redis.StrictRedis()
Attributes:
db (str): The fully qualified filename associated with the redis dbfilename
pid (int): Pid of the running embedded redis server (read only)
redis_log (str): The contents of the redis-server log file
start_timeout (float): Number of seconds to wait for server start
"""Usage Examples:
from redislite import StrictRedis
# Create StrictRedis instance
redis_conn = StrictRedis('/tmp/strict_redis.db')
# Use like any StrictRedis connection
redis_conn.set('counter', 0)
redis_conn.incr('counter')
value = redis_conn.get('counter') # Returns b'1'The core mixin class that provides embedded server management functionality to both Redis and StrictRedis classes.
class RedisMixin:
"""
Extended version of the redis.Redis class with code to start/stop the
embedded redis server based on the passed arguments.
Attributes:
redis_dir (str): Directory for redis instance files
pidfile (str): Path to redis pid file
socket_file (str): Path to redis unix socket file
start_timeout (int): Timeout for server start (default: 10)
running (bool): Whether redis server is running
dbfilename (str): Database filename (default: 'redis.db')
dbdir (str): Database directory
"""
@property
def redis_log(self):
"""
Redis server log content as a string.
Returns:
str: Log contents
"""
@property
def db(self):
"""
Return the connection string to allow connecting to the same redis server.
Returns:
str: Connection path
"""
@property
def pid(self):
"""
Get the current redis-server process id.
Returns:
int: The process id of the redis-server process or 0 if not running
"""
def redis_log_tail(self, lines=1, width=80):
"""
The redis log output.
Parameters:
lines (int, optional): Number of lines from end of logfile (0 = all lines)
width (int, optional): Expected average width of log file line
Returns:
list: List of strings containing the lines from the logfile
"""Create multiple independent Redis servers or share servers via database files.
Usage Examples:
import redislite
# Create multiple independent servers
servers = {}
for i in range(3):
servers[i] = redislite.Redis() # Each gets its own server
servers[i].set('server_id', i)
# Create multiple clients sharing the same server
shared_db = '/tmp/shared.db'
client1 = redislite.Redis(shared_db)
client2 = redislite.Redis(shared_db) # Shares server with client1
client1.set('shared_key', 'value')
print(client2.get('shared_key')) # b'value'Set up Redis replication using server configuration.
Usage Examples:
import redislite
# Create master server on port 8002
master = redislite.Redis(serverconfig={'port': '8002'})
# Create slave server that replicates from master
slave = redislite.Redis(serverconfig={'slaveof': '127.0.0.1 8002'})
# Set data on master
master.set('replicated_key', 'replicated_value')
# Data is available on slave
print(slave.get('replicated_key')) # b'replicated_value'class RedisLiteException(Exception):
"""Redislite Client Error exception class"""
class RedisLiteServerStartError(Exception):
"""Redislite redis-server start error"""Usage Examples:
from redislite import Redis, RedisLiteServerStartError
try:
redis_conn = Redis()
redis_conn.ping()
except RedisLiteServerStartError as e:
print(f"Failed to start Redis server: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-redislite