Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Session): Stockage des sessions côté serveur #1270

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
SebastienReuiller marked this conversation as resolved.
Show resolved Hide resolved
# 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
# ------------------------------------------------------------------------------
Expand All @@ -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"

Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
# ------------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: .
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading