Redis built into a python package
npx @tessl/cli install tessl/pypi-redislite@6.2.0Redislite is a self-contained Python library that provides enhanced Redis-py bindings with a built-in Redis server that is automatically installed, configured, and managed. It offers seamless integration for Python applications requiring Redis functionality without external Redis server setup, supporting all Redis features including replication and clustering.
pip install redisliteimport redisliteMost commonly used:
from redislite import Redis, StrictRedisSubmodule imports:
import redislite.patch
import redislite.debug
import redislite.configurationfrom redislite import Redis
# Create a Redis instance with embedded server
redis_connection = Redis('/tmp/redis.db')
# Use it like any Redis connection
redis_connection.set('key', 'value')
value = redis_connection.get('key')
print(value) # b'value'
# The server is automatically started and will be cleaned up when the instance is deletedRedislite is built around several key components:
Redis and StrictRedis classes that extend redis-py with embedded server managementThe library enables easy creation of either single shared servers or multiple independent servers, maintains full compatibility with existing redis-py code, and uses secure default configurations accessible only by the creating user.
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.
class Redis(RedisMixin, redis.Redis):
def __init__(self, dbfilename=None, serverconfig=None, host=None, port=None, **kwargs):
"""Enhanced Redis client with embedded server management"""
class StrictRedis(RedisMixin, redis.StrictRedis):
def __init__(self, dbfilename=None, serverconfig=None, host=None, port=None, **kwargs):
"""Enhanced StrictRedis client with embedded server management"""Configuration generation and management for Redis server instances, including default settings and custom configuration options.
def config(**kwargs):
"""Generate a redis configuration file based on the passed arguments"""
def settings(**kwargs):
"""Get config settings based on the defaults and the arguments passed"""
DEFAULT_REDIS_SETTINGS = {
'activerehashing': 'yes',
'appendonly': 'no',
'databases': '16',
# ... additional default settings
}Monkey-patch functionality to replace redis-py classes with redislite classes, enabling existing code to use embedded Redis servers without modification.
def patch_redis(dbfile=None):
"""Patch all the redis classes provided by redislite"""
def unpatch_redis():
"""Unpatch all the redis classes provided by redislite"""
def patch_redis_Redis(dbfile=None):
"""Patch the redis module to replace redis.Redis() class with redislite.Redis()"""
def patch_redis_StrictRedis(dbfile=None):
"""Patch the redis module to replace redis.StrictRedis() class with redislite.StrictRedis()"""Debug utilities for troubleshooting redislite installations and understanding the embedded Redis server configuration.
def debug_info():
"""Return a multi-line string with the debug information"""
def debug_info_list():
"""Return a list with the debug information"""
def print_debug_info():
"""Display information about the redislite build, and redis-server on stdout"""class RedisLiteException(Exception):
"""Redislite Client Error exception class"""
class RedisLiteServerStartError(Exception):
"""Redislite redis-server start error"""
# Module attributes
__version__: str # The version of the redislite module
__git_version__: str # Version number derived from git revisions
__git_origin__: str # Git origin of the source repository
__git_branch__: str # Git branch the module was built from
__git_hash__: str # Git hash value for the code used to build this module
__source_url__: str # GitHub web URL for the source code
__redis_executable__: str # Full path to the embedded redis-server executable
__redis_server_version__: str # Version of the embedded redis-server
__redis_server_info__: dict # Redis server information dictionary