Skip to content

Commit

Permalink
WIP redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
daddyy committed Oct 8, 2024
1 parent 6315b7b commit 2f95777
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions engine/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2f95777

Please sign in to comment.