From 451499016efbebe7615a8366114c26e248dd1c07 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 9 Sep 2026 15:53:29 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F(backend)=20increase=20nb=5Fa?= =?UTF-8?q?ccesses=20cache=20TTL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The nb_accesses cache TTL was very short, 30 seconds. That mean that the user will hit the cache for a very short period and the cache is probably not be hit. This is what we can see in the slow queries from the pg_stat_statements table. The query to compute the nb_accesses is executed a little bit less than the number of queries to list or retrieve documents, meaning the cache is not used. --- documentation/env.md | 1 + src/backend/core/api/viewsets.py | 3 +++ src/backend/core/models.py | 4 +++- src/backend/impress/settings.py | 6 ++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/documentation/env.md b/documentation/env.md index 84615543c..18a42cbfb 100644 --- a/documentation/env.md +++ b/documentation/env.md @@ -86,6 +86,7 @@ These are the environment variables you can set for the `impress-backend` contai | DOCSPEC_API_URL | URL to endpoint of DocSpec conversion API | | | DOCUMENT_IMAGE_MAX_SIZE | Maximum size of document in bytes | 10485760 | | DOCUMENT_ALL_ENDPOINT_ENABLED | Enable or not the endpoint /api/v1.0/documents/all/ | true | +| DOCUMENT_NB_ACCESSES_CACHE_TIMEOUT | Time, in seconds, the number of accesses for a document stay in cache. | 600 | | FRONTEND_CSS_URL | To add a external css file to the app | | | FRONTEND_JS_URL | To add a external js file to the app | | | FRONTEND_HOMEPAGE_FEATURE_ENABLED | Frontend feature flag to display the homepage | false | diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 9e1ac3df3..cd1b4fca3 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -1049,6 +1049,9 @@ class DocumentViewSet( defaults={"role": models.RoleChoices.OWNER}, ) + # Invalidate the nb_accesses cache, the value has probably changed after the move. + document.invalidate_nb_accesses_cache() + posthog_capture( PosthogEventName.DOC_MOVED, user, diff --git a/src/backend/core/models.py b/src/backend/core/models.py index 04bcd239b..a18c66a9a 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -1193,7 +1193,9 @@ class Document(MP_Node, BaseModel): document__ancestors_deleted_at__isnull=True, ).count(), ) - cache.set(cache_key, nb_accesses) + cache.set( + cache_key, nb_accesses, settings.DOCUMENT_NB_ACCESSES_CACHE_TIMEOUT + ) return nb_accesses diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index bcd64a92f..4bd543843 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -990,6 +990,12 @@ class Base(Configuration): environ_prefix=None, ) + DOCUMENT_NB_ACCESSES_CACHE_TIMEOUT = values.IntegerValue( + default=600, + environ_name="DOCUMENT_NB_ACCESSES_CACHE_TIMEOUT", + environ_prefix=None, + ) + # Logging # We want to make it easy to log to console but by default we log production # to Sentry and don't want to log to console.