Ensure caching is both lifecycle-correct and identity-safe.
Example pattern (lifecycle + identity check):
# lifecycle reset at correct boundary
if not internal_subrun:
reset_search_cache() # run-scoped invalidation
# key must cover all query dimensions
cache_key = (core_topic, count, from_date)
# identity-safe refetch
def refetch_datum(item, datum_key):
event_id = str(getattr(item, "metadata", {}).get("event_id") or "").strip()
slug = extract_slug(item.url) # parse from URL
payload = http_request_lookup(event_id or slug)
if event_id:
# If we used slug fallback, still enforce ID match
chosen = pick_matching_event(payload, expected_event_id=event_id)
if chosen is None:
raise ValueError("Event identity mismatch; degrade safely")
return payload
This prevents stale cross-run reuse (wrong cache lifetime) and prevents cross-entity contamination (wrong identity during refetch), both of which lead to incorrect cached results.