From b9a8ace6145342782277617f2b8a55860b7c24e7 Mon Sep 17 00:00:00 2001 From: Daniel Glaser <7723396+the78mole@users.noreply.github.com> Date: Fri, 1 Aug 2025 02:36:02 +0200 Subject: [PATCH] feat: Add support for cache password in Redis configuration (#10107) * feat(cache): Add support for cache password in Redis configuration * Add cache password configuration option to documentation * Fix the complaints from CI checks --- docs/docs/start/config.md | 1 + src/backend/InvenTree/InvenTree/cache.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/docs/start/config.md b/docs/docs/start/config.md index e2c2f6d4de..fb6e9680cc 100644 --- a/docs/docs/start/config.md +++ b/docs/docs/start/config.md @@ -307,6 +307,7 @@ The following cache settings are available: | INVENTREE_CACHE_ENABLED | cache.enabled | Enable redis caching | False | | INVENTREE_CACHE_HOST | cache.host | Cache server host | *Not specified* | | INVENTREE_CACHE_PORT | cache.port | Cache server port | 6379 | +| INVENTREE_CACHE_PASSWORD | cache.password | Cache server password | none | | INVENTREE_CACHE_CONNECT_TIMEOUT | cache.connect_timeout | Cache connection timeout (seconds) | 3 | | INVENTREE_CACHE_TIMEOUT | cache.timeout | Cache timeout (seconds) | 3 | | INVENTREE_CACHE_TCP_KEEPALIVE | cache.tcp_keepalive | Cache TCP keepalive | True | diff --git a/src/backend/InvenTree/InvenTree/cache.py b/src/backend/InvenTree/InvenTree/cache.py index a217c94fe1..04f9a42d92 100644 --- a/src/backend/InvenTree/InvenTree/cache.py +++ b/src/backend/InvenTree/InvenTree/cache.py @@ -37,6 +37,11 @@ def cache_port() -> int: return cache_setting('port', '6379', typecast=int) +def cache_password(): + """Return the cache password.""" + return cache_setting('password', None) + + def is_global_cache_enabled() -> bool: """Check if the global cache is enabled. @@ -77,9 +82,17 @@ def get_cache_config(global_cache: bool) -> dict: A dictionary containing the cache configuration options. """ if global_cache: + # Build Redis URL with optional password + password = cache_password() + + if password: + redis_url = f'redis://:{password}@{cache_host()}:{cache_port()}/0' + else: + redis_url = f'redis://{cache_host()}:{cache_port()}/0' + return { 'BACKEND': 'django_redis.cache.RedisCache', - 'LOCATION': f'redis://{cache_host()}:{cache_port()}/0', + 'LOCATION': redis_url, 'OPTIONS': { 'CLIENT_CLASS': 'django_redis.client.DefaultClient', 'SOCKET_CONNECT_TIMEOUT': cache_setting(