From 2f95777894be2145f1a6ea40390ca9bfa2f25499 Mon Sep 17 00:00:00 2001 From: Pavel Fiala Date: Tue, 8 Oct 2024 11:24:03 +0200 Subject: [PATCH] WIP redis cache --- engine/settings/base.py | 48 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/engine/settings/base.py b/engine/settings/base.py index 328388656..89b9fba30 100644 --- a/engine/settings/base.py +++ b/engine/settings/base.py @@ -248,6 +248,7 @@ class DatabaseTypes: "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": REDIS_URI, + "KEY_PREFIX": REDIS_KEY_PREFIX, "OPTIONS": { "DB": REDIS_DATABASE, "PARSER_CLASS": "redis.connection._HiredisParser", @@ -263,31 +264,42 @@ class DatabaseTypes: }, } -# comma separated list, sentinel with port (sentinel:26379, sentinel2:26379) REDIS_SENTINELS = os.getenv("REDIS_SENTINELS", None) -REDIS_SENTINEL_MASTER_NAME = os.getenv("REDIS_SENTINEL_MASTER_NAME", None) -REDIS_SENTINEL_USERNAME = os.getenv("REDIS_SENTINEL_USERNAME", None) -REDIS_SENTINEL_PASSWORD = os.getenv("REDIS_SENTINEL_PASSWORD", None) -REDIS_SENTINEL_KWARGS = {} -if REDIS_SENTINEL_PASSWORD: - REDIS_SENTINEL_KWARGS["password"] = REDIS_SENTINEL_PASSWORD -if REDIS_SENTINEL_USERNAME: - REDIS_SENTINEL_KWARGS["username"] = REDIS_SENTINEL_USERNAME if REDIS_SENTINELS: + REDIS_SENTINEL_MASTER_NAME = os.getenv("REDIS_SENTINEL_MASTER_NAME", None) + REDIS_SENTINEL_USERNAME = os.getenv("REDIS_SENTINEL_USERNAME", None) + REDIS_SENTINEL_PASSWORD = os.getenv("REDIS_SENTINEL_PASSWORD", None) + REDIS_SENTINEL_KWARGS = {"password": REDIS_SENTINEL_PASSWORD, "username": REDIS_SENTINEL_USERNAME} + DJANGO_SENTINELS = [tuple(sentinel.split(":")) for sentinel in REDIS_SENTINELS.split(",")] DJANGO_SENTINELS = [(host, int(port)) for host, port in DJANGO_SENTINELS] - CACHES["default"]["OPTIONS"]["SENTINELS"] = DJANGO_SENTINELS - - if REDIS_KEY_PREFIX: - CACHES["default"]["KEY_PREFIX"] = REDIS_KEY_PREFIX - if REDIS_SENTINEL_MASTER_NAME: - CACHES["default"]["OPTIONS"]["LOCATION"] = "redis://%s/%s" % (REDIS_SENTINEL_MASTER_NAME, REDIS_DATABASE) + DJANGO_REDIS_CONNECTION_FACTORY = 'django_redis.pool.SentinelConnectionFactory' - if REDIS_SENTINEL_KWARGS: - CACHES["default"]["OPTIONS"]["SENTINEL_KWARGS"] = REDIS_SENTINEL_KWARGS + CACHES["sentinel"] = { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": f"redis://{REDIS_USERNAME}@{REDIS_SENTINEL_MASTER_NAME}/{REDIS_DATABASE}", + "KEY_PREFIX": REDIS_KEY_PREFIX, + "OPTIONS": { + "PARSER_CLASS": "redis.connection._HiredisParser", + "CLIENT_CLASS": "django_redis.client.SentinelClient", + "CONNECTION_POOL_CLASS": "redis.sentinel.SentinelConnectionPool", + "SENTINELS": DJANGO_SENTINELS, + "MASTER_NAME": REDIS_SENTINEL_MASTER_NAME, + "PASSWORD": REDIS_PASSWORD, + "SENTINEL_KWARGS": REDIS_SENTINEL_KWARGS, + "CONNECTION_POOL_CLASS_KWARGS": REDIS_SSL_CONFIG + | { + "max_connections": 50, + "timeout": 20, + }, + "MAX_CONNECTIONS": 1000, + "PICKLE_VERSION": -1, + }, + } +print(CACHES) # Application definition @@ -550,6 +562,8 @@ class BrokerTypes: else: raise ValueError(f"Invalid BROKER_TYPE env variable: {BROKER_TYPE}") +CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True + CELERY_IGNORE_RESULT = True CELERY_ACKS_LATE = True