from django.core.cache import cache # Short enough that a missed invalidation point self-heals within one # page load's worth of staleness; long enough to actually absorb repeat # authenticated-page renders (apps.core.context_processors.sidebar runs # on every one) without recomputing unread_counts_by_feed's join/group-by # over the user's whole article corpus each time (C3). SIDEBAR_UNREAD_CACHE_TTL_SECONDS = 30 def sidebar_unread_cache_key(user_id: int) -> str: return f"sidebar-unread:{user_id}" def invalidate_sidebar_unread_cache(user_id: int) -> None: """Call this wherever a request changes the current user's own read/unread state (toggle_read, mark_all_read, viewing an article) so the sidebar reflects it on the very next render instead of waiting out the TTL above. Rule-driven state changes during background feed ingestion aren't invalidated this way — those self-heal within the TTL, same as a missed call site here would.""" cache.delete(sidebar_unread_cache_key(user_id))