Skip to content

Commit

Permalink
Merge pull request #6508 from OCHA-DAP/feature/HDX-10494-truthy-cache…
Browse files Browse the repository at this point in the history
…-wrapper

PR HDX-10494 implement function wrapper that only caches truthy values
  • Loading branch information
danmihaila authored Jan 24, 2025
2 parents f6f0450 + 5346c40 commit 2d75784
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ckanext-hdx_package/ckanext/hdx_package/helpers/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import ckan.plugins.toolkit as tk
import ckanext.hdx_theme.helpers.country_list_hardcoded as focus_countries
from ckanext.hdx_theme.helpers.caching import dogpile_standard_config, dogpile_config_filter, dogpile_requests_region, \
HDXRedisInvalidationStrategy
HDXRedisInvalidationStrategy, cache_only_if_truthy_wrapper

log = logging.getLogger(__name__)
config = tk.config
Expand Down Expand Up @@ -193,7 +193,7 @@ def invalidate_cached_resource_id_apihighways():
cached_resource_id_apihighways.invalidate()


@dogpile_requests_region.cache_on_arguments()
@cache_only_if_truthy_wrapper(dogpile_requests_region)
def cached_approved_tags_list():
log.info('Creating cache for approved tags list')
tags = tk.get_action('hdx_retrieve_approved_tags')({'user': '127.0.0.1'}, {})
Expand Down
31 changes: 31 additions & 0 deletions ckanext-hdx_theme/ckanext/hdx_theme/helpers/caching.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
from functools import wraps

import requests
import time
# import datetime
# import dateutil.parser

from dogpile.cache import make_region
from dogpile.cache.api import NO_VALUE
from dogpile.cache.region import RegionInvalidationStrategy, CacheRegion

from redis import StrictRedis
Expand Down Expand Up @@ -149,3 +152,31 @@ def cached_make_rest_api_request(url):
response.raise_for_status()

return response.json()

def cache_only_if_truthy_wrapper(region: CacheRegion, *args, **kwargs):
# original_decorator = region.cache_on_arguments(*args, **kwargs)

def decorator(f):
@wraps(f)
def wrapper(*fn_args, **fn_kwargs):
key_generator = region.function_key_generator('only_truthy_values', f)
key = key_generator(*fn_args, *fn_kwargs)

cached_value = region.get(key)
if cached_value is not NO_VALUE:
return cached_value
# Call the original function
result = f(*fn_args, **fn_kwargs)

# Cache the result only if it's truthy
if result:
log.info(f'Caching result for key "{key}"')
region.set(key, result)
else:
log.warning(f'Not caching result for key "{key}" because returned value was: {result}')

return result

return wrapper

return decorator

0 comments on commit 2d75784

Please sign in to comment.