From 9c55539eb30cb5b2e5defb3455d81b340be9cb15 Mon Sep 17 00:00:00 2001 From: Maxim Kolyubyakin Date: Tue, 29 Mar 2022 09:27:17 +0200 Subject: [PATCH] LITE-22733 Added lock for cache update in RQLFilterBackend --- dj_rql/drf/backend.py | 14 ++++++++++---- requirements/dev.txt | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/dj_rql/drf/backend.py b/dj_rql/drf/backend.py index 6e673b0..ddea49a 100644 --- a/dj_rql/drf/backend.py +++ b/dj_rql/drf/backend.py @@ -1,12 +1,16 @@ # -# Copyright © 2021 Ingram Micro Inc. All rights reserved. +# Copyright © 2022 Ingram Micro Inc. All rights reserved. # +from threading import Lock from dj_rql.drf._utils import get_query from rest_framework.filters import BaseFilterBackend +lock = Lock() + + class _FilterClassCache: CACHE = {} @@ -46,10 +50,12 @@ def filter_queryset(self, request, queryset, view): cache_key = str(queryset.query) + query query_cache = self._get_or_init_cache(filter_class, view) - filters_result = query_cache.get(cache_key) - if not filters_result: + try: + filters_result = query_cache[cache_key] + except KeyError: filters_result = filter_instance.apply_filters(query, request, view) - query_cache[cache_key] = filters_result + with lock: + query_cache[cache_key] = filters_result else: filters_result = filter_instance.apply_filters(query, request, view) diff --git a/requirements/dev.txt b/requirements/dev.txt index fa5a9a5..17520ec 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,2 +1,2 @@ -lib-rql +lib-rql>=1.1.1 Django>=2.2.19