Files
Evection/apps/core/cache_utils.py
T
ksmithandClaude Sonnet 5.5 2a3e9f632e Make the banner and sidebar counts live on every page
One 5-minute poll (/new-since/) now drives the new-articles banner, the
tab dot and the sidebar counts, across all subscriptions rather than the
selected feed/tag. The banner lives in the base layout with a
server-rendered baseline id and reloads the current view on click; new
articles are never inserted automatically.

Sidebar counts are patched in place by the poll, and immediately after
reading or starring (HX-Trigger: countsChanged). Ingestion, filter rules
and subscription changes now drop the cached counts instead of waiting
out the TTL.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-03 07:39:36 -07:00

44 lines
1.8 KiB
Python

import logging
from django.core.cache import cache
logger = logging.getLogger(__name__)
# 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_feed_subscribers_sidebar_cache(feed_id: int) -> None:
"""Drop the cached unread counts of everyone subscribed to a feed —
called when ingestion adds articles or filter rules change their
read/hidden state, so the sidebar's next refresh shows them at once
instead of after the TTL."""
from apps.feeds.models import Subscription
user_ids = Subscription.objects.filter(feed_id=feed_id).values_list("user_id", flat=True)
try:
cache.delete_many([sidebar_unread_cache_key(user_id) for user_id in user_ids])
except Exception:
logger.warning("Sidebar cache unavailable; nothing to invalidate")
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."""
try:
cache.delete(sidebar_unread_cache_key(user_id))
except Exception:
logger.warning("Sidebar cache unavailable; nothing to invalidate")