Redis built into a python package
—
Configuration generation and management for Redis server instances, including default settings and custom configuration options.
Generate Redis server configuration files based on default settings and custom parameters.
def config(**kwargs):
"""
Generate a redis configuration file based on the passed arguments.
Parameters:
**kwargs: Configuration settings where key is the setting name and value
is the setting value. If value is list, setting will be repeated
with each value. If value is None, setting will be removed from
defaults if it exists.
Returns:
str: Redis server configuration
"""
def settings(**kwargs):
"""
Get config settings based on the defaults and the arguments passed.
Parameters:
**kwargs: Redis server arguments, the keyword is the setting, the value
is the value. If value is list, setting will be set multiple times.
If value is None, setting will be removed from defaults.
Returns:
dict: Dictionary containing redis server settings with key being the
setting name and values being the settings.
"""
def config_line(setting, value):
"""
Generate a single configuration line based on the setting and value.
Parameters:
setting (str): The configuration setting
value (str): The value for the configuration setting
Returns:
str: The configuration line based on the setting and value
"""Usage Examples:
from redislite.configuration import config, settings, config_line
# Generate basic configuration
basic_config = config()
print(basic_config)
# Generate configuration with custom settings
custom_config = config(
port='8080',
databases='32',
loglevel='debug'
)
print(custom_config)
# Get settings dictionary
config_dict = settings(
port='6379',
databases='16',
appendonly='yes'
)
print(config_dict)
# Generate individual configuration line
line = config_line('port', '6379')
print(line) # "port 6379"Comprehensive default configuration settings for Redis server instances.
DEFAULT_REDIS_SETTINGS = {
'activerehashing': 'yes',
'aof-rewrite-incremental-fsync': 'yes',
'appendonly': 'no',
'appendfilename': 'appendonly.aof',
'appendfsync': 'everysec',
'aof-load-truncated': 'yes',
'auto-aof-rewrite-percentage': '100',
'auto-aof-rewrite-min-size': '64mb',
'bind': None,
'daemonize': 'yes',
'databases': '16',
'dbdir': './',
'dbfilename': 'redis.db',
'hash-max-ziplist-entries': '512',
'hash-max-ziplist-value': '64',
'hll-sparse-max-bytes': '3000',
'hz': '10',
'list-max-ziplist-entries': '512',
'list-max-ziplist-value': '64',
'loglevel': 'notice',
'logfile': 'redis.log',
'lua-time-limit': '5000',
'pidfile': '/var/run/redislite/redis.pid',
'port': '0',
'save': ['900 1', '300 100', '60 200', '15 1000'],
'stop-writes-on-bgsave-error': 'yes',
'tcp-backlog': '511',
'tcp-keepalive': '0',
'rdbcompression': 'yes',
'rdbchecksum': 'yes',
'slave-serve-stale-data': 'yes',
'slave-read-only': 'yes',
'repl-disable-tcp-nodelay': 'no',
'slave-priority': '100',
'no-appendfsync-on-rewrite': 'no',
'slowlog-log-slower-than': '10000',
'slowlog-max-len': '128',
'latency-monitor-threshold': '0',
'notify-keyspace-events': '""',
'set-max-intset-entries': '512',
'timeout': '0',
'unixsocket': '/var/run/redislite/redis.socket',
'unixsocketperm': '700',
'zset-max-ziplist-entries': '128',
'zset-max-ziplist-value': '64',
}Usage Examples:
from redislite.configuration import DEFAULT_REDIS_SETTINGS, settings
# View default settings
print(DEFAULT_REDIS_SETTINGS['databases']) # '16'
print(DEFAULT_REDIS_SETTINGS['port']) # '0' (Unix socket only)
# Override defaults
custom_settings = settings(
port='6379', # Enable TCP port
databases='32', # More databases
loglevel='debug' # Verbose logging
)
# Remove a default setting
no_save_settings = settings(
save=None # Disable automatic saves
)Configure Redis server instances with complex settings including replication, persistence, and performance tuning.
Usage Examples:
from redislite import Redis
# Configure master-slave replication
master_config = {
'port': '8002',
'bind': '127.0.0.1',
'databases': '16'
}
slave_config = {
'slaveof': '127.0.0.1 8002',
'slave-read-only': 'yes'
}
master = Redis(serverconfig=master_config)
slave = Redis(serverconfig=slave_config)
# Configure persistence options
persistent_config = {
'appendonly': 'yes',
'appendfsync': 'always',
'save': ['60 1000', '300 100', '900 1'] # Custom save points
}
persistent_redis = Redis(serverconfig=persistent_config)
# Configure performance settings
performance_config = {
'databases': '32',
'hash-max-ziplist-entries': '1024',
'hash-max-ziplist-value': '128',
'tcp-keepalive': '60'
}
performance_redis = Redis(serverconfig=performance_config)
# Disable specific features
minimal_config = {
'appendonly': 'no',
'save': None, # Disable RDB snapshots
'slowlog-max-len': '0' # Disable slow log
}
minimal_redis = Redis(serverconfig=minimal_config)Generate complete Redis configuration files for external use.
Usage Examples:
from redislite.configuration import config
# Generate configuration file content
config_content = config(
port='6379',
bind='127.0.0.1',
databases='16',
appendonly='yes',
appendfilename='myapp.aof',
save=['900 1', '300 10', '60 10000'],
maxmemory='256mb',
maxmemory_policy='allkeys-lru'
)
# Write to file
with open('/tmp/redis.conf', 'w') as f:
f.write(config_content)
# Use with external Redis server
# redis-server /tmp/redis.confInstall with Tessl CLI
npx tessl i tessl/pypi-redislite