Skip to content

Commit

Permalink
Merge pull request #47 from cloudblue/bugfix/LITE-22733
Browse files Browse the repository at this point in the history
LITE-22733 Added lock for cache update in RQLFilterBackend
  • Loading branch information
maxipavlovic authored Mar 29, 2022
2 parents 00f5703 + 9c55539 commit 283d376
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions dj_rql/drf/backend.py
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
lib-rql
lib-rql>=1.1.1
Django>=2.2.19

0 comments on commit 283d376

Please sign in to comment.