Skip to content

Commit

Permalink
Fix dynamic excluded provider caching
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
obulat committed Nov 21, 2023
1 parent 3bbeff7 commit f4d623c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions api/api/controllers/search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
NESTING_THRESHOLD = config("POST_PROCESS_NESTING_THRESHOLD", cast=int, default=5)
SOURCE_CACHE_TIMEOUT = 60 * 60 * 4 # 4 hours
FILTER_CACHE_TIMEOUT = 30
FILTERED_PROVIDERS_CACHE_KEY = "filtered_providers"
THUMBNAIL = "thumbnail"
URL = "url"
PROVIDER = "provider"
Expand Down Expand Up @@ -170,19 +171,23 @@ def get_excluded_providers_query() -> Q | None:
Hide data sources from the catalog dynamically.
To exclude a provider, set ``filter_content`` to ``True`` in the
``ContentProvider`` model in Django admin.
The list of ``provider_identifier``s is cached in Redis with
`:1:FILTER_CACHE_KEY` key.
"""

filter_cache_key = "filtered_providers"
filtered_providers = cache.get(key=filter_cache_key)
filtered_providers = cache.get(key=FILTERED_PROVIDERS_CACHE_KEY)
if not filtered_providers:
filtered_providers = models.ContentProvider.objects.filter(
filter_content=True
).values("provider_identifier")
providers = models.ContentProvider.objects.filter(filter_content=True).values(
"provider_identifier"
)
filtered_providers = [p["provider_identifier"] for p in providers]
cache.set(
key=filter_cache_key, timeout=FILTER_CACHE_TIMEOUT, value=filtered_providers
key=FILTERED_PROVIDERS_CACHE_KEY,
timeout=FILTER_CACHE_TIMEOUT,
value=filtered_providers,
)
if provider_list := [f["provider_identifier"] for f in filtered_providers]:
return Q("terms", provider=provider_list)
if filtered_providers:
return Q("terms", provider=filtered_providers)
return None


Expand Down

0 comments on commit f4d623c

Please sign in to comment.