️(backend) increase nb_accesses cache TTL

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.
This commit is contained in:
Manuel Raynaud
2026-09-11 12:55:20 +02:00
parent 6baf20aaeb
commit 451499016e
4 changed files with 13 additions and 1 deletions
+1
View File
@@ -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 |
+3
View File
@@ -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,
+3 -1
View File
@@ -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
+6
View File
@@ -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.