From 2ef1de5563a82b059f9ee503daacee55bbeb33bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Tue, 18 Jun 2024 13:04:20 +0200 Subject: [PATCH 1/3] change session storage from signed_cookies to cached_db --- config/settings/base.py | 66 ++++++++++++++++++++++++----------------- docker-compose.yml | 5 ++++ 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index bc337e40a..f0a028eae 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -388,6 +388,36 @@ HUBSPOT_API_KEY = env.str("HUBSPOT_API_KEY", "set-it") HUBSPOT_IS_ACTIVATED = env.bool("HUBSPOT_IS_ACTIVATED", False) +# Caching +# https://docs.djangoproject.com/en/4.0/topics/cache/ +# ------------------------------------------------------------------------------ + +# Redis database to use with async (must be different for each environement) +# 1 <= REDIS_DB <= 100 (number of dbs available on CleverCloud) +REDIS_DB = env.int("REDIS_DB", 1) +# Complete URL (containing the instance password) +REDIS_URL = env.str("REDIS_URL", "localhost") +REDIS_PORT = env.int("REDIS_PORT", 6379) +REDIS_PASSWORD = env.str("REDIS_PASSWORD", "") + +if env.bool("REDIS_CACHE_ENABLED", False): + # use Redis cache backend (also needed for session storage perf) + CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.redis.RedisCache", + "LOCATION": f"redis://:{REDIS_PASSWORD}@{REDIS_URL}:{REDIS_PORT}", + } + } +else: + # Simple DB caching, we need it for Select2 (don't ask me why...) + CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.db.DatabaseCache", + "LOCATION": "django_cache", + } + } + +SELECT2_CACHE_BACKEND = "default" # Security # ------------------------------------------------------------------------------ @@ -407,13 +437,19 @@ SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", False) -SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies" +if env.bool("REDIS_CACHE_ENABLED", False): + # Session reads use the cache, or the database if the data has been evicted from the cache. + # https://docs.djangoproject.com/en/5.0/topics/http/sessions/#using-database-backed-sessions + SESSION_ENGINE = "django.contrib.sessions.backends.cached_db" + SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_SECURE = True -SESSION_EXPIRE_AT_BROWSER_CLOSE = True +SESSION_COOKIE_AGE = env.int("SESSION_COOKIE_AGE", 604800) # one week + +SESSION_EXPIRE_AT_BROWSER_CLOSE = False X_FRAME_OPTIONS = "DENY" @@ -650,17 +686,6 @@ # Async Configuration Options: Huey # Workers are run in prod via `CC_WORKER_COMMAND = django-admin run_huey`. # ------------------------------------------------------------------------------ - -# Redis server URL: -# Provided by the Redis addon (itou-redis) -# Redis database to use with async (must be different for each environement) -# 1 <= REDIS_DB <= 100 (number of dbs available on CleverCloud) -REDIS_DB = env.int("REDIS_DB", 1) -# Complete URL (containing the instance password) -REDIS_URL = env.str("REDIS_URL", "localhost") -REDIS_PORT = env.int("REDIS_PORT", 6379) -REDIS_PASSWORD = env.str("REDIS_PASSWORD", "") - CONNECTION_MODES_HUEY = { # immediate mode "direct": {"immediate": True}, @@ -701,21 +726,6 @@ } -# Caching -# https://docs.djangoproject.com/en/4.0/topics/cache/ -# ------------------------------------------------------------------------------ - -# Simple DB caching, we need it for Select2 (don't ask me why...) -CACHES = { - "default": { - "BACKEND": "django.core.cache.backends.db.DatabaseCache", - "LOCATION": "django_cache", - } -} - -SELECT2_CACHE_BACKEND = "default" - - # Logging # https://docs.djangoproject.com/en/dev/topics/logging # ------------------------------------------------------------------------------ diff --git a/docker-compose.yml b/docker-compose.yml index f16cee179..b0943d8f9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,11 @@ services: - env.docker.local ports: - "${POSTGRESQL_PORT:-5432}:5432" + redis: + image: redis:7-alpine + restart: unless-stopped + ports: + - 6379:6379 app: build: context: . From a2cae4948a11cf4622ee506437c27ca93bf8d3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Tue, 18 Jun 2024 13:04:29 +0200 Subject: [PATCH 2/3] increase num queries for session in db --- lemarche/www/siaes/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lemarche/www/siaes/tests.py b/lemarche/www/siaes/tests.py index a0c815ce0..15793bae0 100644 --- a/lemarche/www/siaes/tests.py +++ b/lemarche/www/siaes/tests.py @@ -58,7 +58,7 @@ def setUpTestData(cls): def test_search_num_queries(self): url = reverse("siae:search_results") - with self.assertNumQueries(8): + with self.assertNumQueries(12): response = self.client.get(url) siaes = list(response.context["siaes"]) self.assertEqual(len(siaes), 20) From 2982043f2c153971ae590434775d8e68f74a4e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Tue, 18 Jun 2024 16:29:53 +0200 Subject: [PATCH 3/3] use local var instead read env everytime --- config/settings/base.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index f0a028eae..e5c305b23 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -399,8 +399,9 @@ REDIS_URL = env.str("REDIS_URL", "localhost") REDIS_PORT = env.int("REDIS_PORT", 6379) REDIS_PASSWORD = env.str("REDIS_PASSWORD", "") +REDIS_CACHE_ENABLED = env.bool("REDIS_CACHE_ENABLED", False) -if env.bool("REDIS_CACHE_ENABLED", False): +if REDIS_CACHE_ENABLED: # use Redis cache backend (also needed for session storage perf) CACHES = { "default": { @@ -437,7 +438,7 @@ SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", False) -if env.bool("REDIS_CACHE_ENABLED", False): +if REDIS_CACHE_ENABLED: # Session reads use the cache, or the database if the data has been evicted from the cache. # https://docs.djangoproject.com/en/5.0/topics/http/sessions/#using-database-backed-sessions SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"