From bc510575b00ece58f0a4120657f0acbb9b3454ea Mon Sep 17 00:00:00 2001 From: SebastienReuiller Date: Tue, 18 Jun 2024 16:48:15 +0200 Subject: [PATCH] =?UTF-8?q?fix(Session):=20Stockage=20des=20sessions=20c?= =?UTF-8?q?=C3=B4t=C3=A9=20serveur=20(#1270)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/settings/base.py | 67 +++++++++++++++++++++---------------- docker-compose.yml | 5 +++ lemarche/www/siaes/tests.py | 2 +- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index bc337e40a..e5c305b23 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -388,6 +388,37 @@ 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", "") +REDIS_CACHE_ENABLED = env.bool("REDIS_CACHE_ENABLED", False) + +if REDIS_CACHE_ENABLED: + # 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 +438,19 @@ SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", False) -SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies" +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" + 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 +687,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 +727,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: . 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)