Skip to content

Commit

Permalink
fix: cache debugging in discovery client
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Sep 29, 2023
1 parent 53eaa93 commit 8e727fe
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions enterprise_catalog/apps/api_client/discovery_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

LOGGER = getLogger(__name__)

_CACHE_MISS = object()


class CatalogQueryMetadata:
"""
Expand Down Expand Up @@ -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

0 comments on commit 8e727fe

Please sign in to comment.