Skip to content

Commit

Permalink
Add cache parameter to does_match for selective caching control [SDBE…
Browse files Browse the repository at this point in the history
…LGA-538] (#2695)

* Add cache parameter to does_match for selective caching control [SDBELGA-538]

* refactore logic
  • Loading branch information
devketanpro authored Sep 13, 2024
1 parent 81462f7 commit 63b0b61
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions apps/content_filters/content_filter/content_filter_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,34 +193,36 @@ def _get_elastic_query(self, doc, matching=True):
expressions_list.append({"bool": filter_conditions})
return {"bool": expressions}

def does_match(self, content_filter, article, filters=None):
def does_match(self, content_filter, article, filters=None, cache=True):
if not content_filter:
return True # a non-existing filter matches every thing
cache_id = _cache_id("filter-match", content_filter.get("_id") or content_filter.get("name"), article)
if not hasattr(flask.g, cache_id):
setattr(flask.g, cache_id, self._does_match(content_filter, article, filters))
if not cache:
return self._does_match(content_filter, article, filters, cache)
elif not hasattr(flask.g, cache_id):
setattr(flask.g, cache_id, self._does_match(content_filter, article, filters, cache))
return getattr(flask.g, cache_id)

def _does_match(self, content_filter, article, filters):
def _does_match(self, content_filter, article, filters, cache=True):
for index, expression in enumerate(content_filter.get("content_filter", [])):
if not expression.get("expression"):
raise SuperdeskApiError.badRequestError(
_("Filter statement {index} does not have a filter condition").format(index=index + 1)
)
if "fc" in expression.get("expression", {}):
if not self._does_filter_condition_match(content_filter, article, filters, expression):
if not self._does_filter_condition_match(content_filter, article, filters, expression, cache):
continue
if "pf" in expression.get("expression", {}):
if not self._does_content_filter_match(content_filter, article, filters, expression):
if not self._does_content_filter_match(content_filter, article, filters, expression, cache):
continue
return True
return False

def _does_filter_condition_match(self, content_filter, article, filters, expression) -> bool:
def _does_filter_condition_match(self, content_filter, article, filters, expression, cache=True) -> bool:
filter_condition_service = get_resource_service("filter_conditions")
for f in expression["expression"]["fc"]:
cache_id = _cache_id("filter-condition-match", f, article)
if not hasattr(flask.g, cache_id):
if not cache or not hasattr(flask.g, cache_id):
fc = (
filters.get("filter_conditions", {}).get(f, {}).get("fc")
if filters
Expand All @@ -230,18 +232,22 @@ def _does_filter_condition_match(self, content_filter, article, filters, express
logger.error("Missing filter condition %s in content filter %s", f, content_filter.get("name"))
return False
filter_condition = FilterCondition.parse(fc)
if not cache:
return filter_condition.does_match(article)
setattr(flask.g, cache_id, filter_condition.does_match(article))
if not getattr(flask.g, cache_id):
return False
return True

def _does_content_filter_match(self, content_filter, article, filters, expression) -> bool:
def _does_content_filter_match(self, content_filter, article, filters, expression, cache=True) -> bool:
for f in expression["expression"]["pf"]:
cache_id = _cache_id("content-filter-match", f, article)
if not hasattr(flask.g, cache_id):
if not cache or not hasattr(flask.g, cache_id):
current_filter = (
filters.get("content_filters", {}).get(f, {}).get("cf") if filters else self.get_cached_by_id(f)
)
if not cache:
return self.does_match(current_filter, article, filters=filters)
setattr(flask.g, cache_id, self.does_match(current_filter, article, filters=filters))
if not getattr(flask.g, cache_id):
return False
Expand Down

0 comments on commit 63b0b61

Please sign in to comment.