From 8e727fe7314553cd5a3f0406d93beac79425a3df Mon Sep 17 00:00:00 2001 From: Alex Dusenbery Date: Fri, 29 Sep 2023 10:55:51 -0400 Subject: [PATCH] fix: cache debugging in discovery client --- .../apps/api_client/discovery_cache.py | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/enterprise_catalog/apps/api_client/discovery_cache.py b/enterprise_catalog/apps/api_client/discovery_cache.py index e12adfaa6..59d57d556 100644 --- a/enterprise_catalog/apps/api_client/discovery_cache.py +++ b/enterprise_catalog/apps/api_client/discovery_cache.py @@ -14,6 +14,8 @@ LOGGER = getLogger(__name__) +_CACHE_MISS = object() + class CatalogQueryMetadata: """ @@ -52,16 +54,24 @@ def _get_catalog_query_metadata(self, catalog_query): Empty dictionary if no data found in cache or from API. """ cache_key = DISCOVERY_CATALOG_QUERY_CACHE_KEY_TPL.format(id=self.catalog_query.id) - catalog_query_data = cache.get(cache_key) - if not catalog_query_data: + catalog_query_data = cache.get(cache_key, _CACHE_MISS) + if catalog_query_data is not _CACHE_MISS: + LOGGER.info('Cache HIT for CatalogQuery id=%s', self.catalog_query.id) + else: client = DiscoveryApiClient() catalog_query_data = client.get_metadata_by_query(catalog_query) if not catalog_query_data: catalog_query_data = [] - cache.set(cache_key, catalog_query_data, settings.DISCOVERY_CATALOG_QUERY_CACHE_TIMEOUT) - LOGGER.info( - 'CatalogQueryDetails: CACHED CatalogQuery metadata with id %s for %s sec', - self.catalog_query.id, - settings.DISCOVERY_CATALOG_QUERY_CACHE_TIMEOUT, - ) + # cache.add() will not attempt to update the cache if the key specified is already present + # this is fine here, because we know we just encountered a cache miss on our key. + # add() returns a boolean letting us know if it stored anything in the cache + cache_add_success = cache.add(cache_key, catalog_query_data, settings.DISCOVERY_CATALOG_QUERY_CACHE_TIMEOUT) + if cache_add_success: + LOGGER.info( + 'CatalogQueryDetails: CACHED CatalogQuery metadata with id %s for %s sec', + self.catalog_query.id, + settings.DISCOVERY_CATALOG_QUERY_CACHE_TIMEOUT, + ) + else: + LOGGER.info('Cache ADD FAIL for CatalogQuery id=%s', self.catalog_query.id) return catalog_query_data