Cache sidebar unread counts for a short TTL (C3)

apps.core.context_processors.sidebar runs on every authenticated page
render and called unread_counts_by_feed, a join/group-by over the
user's whole article corpus, every single time — cost grew with the
archive's size regardless of which page was being viewed.

unread_counts_by_feed now caches its {feed_id: count} result per user
for 30 seconds (apps.core.cache_utils). The reader views that change
the current user's own read state (toggle_read, mark_all_read, viewing
an article) invalidate it immediately so the sidebar never visibly lags
behind the action that just caused it; other paths (rule-driven state
changes during background ingestion) aren't explicitly invalidated and
simply self-heal within the TTL, which is an acceptable tradeoff
already made elsewhere in this app (the stream's own "new articles"
banner polls on the same 30s cadence).

Also adds a global cache.clear() autouse fixture (conftest.py) — the
test CACHES backend (LocMemCache) lives in process memory and survives
Django's per-test DB rollback, so a cached count keyed by a user's pk
could otherwise leak into an unrelated later test reusing that pk.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 16:09:28 -06:00
1 parent b33fe905d2
commit abf4cef55b
6 files changed
+173 -4

No files matched your search

+19 -4
View File
@@ -1,5 +1,8 @@
from django.core.cache import cache
from django.db.models import Count
from apps.core.cache_utils import SIDEBAR_UNREAD_CACHE_TTL_SECONDS, sidebar_unread_cache_key
from .models import SAVED_TAG_NAME, STARRED_TAG_NAME, Article, ArticleTag, UserArticleState
@@ -44,9 +47,19 @@ def with_user_state(articles, user):
def unread_counts_by_feed(user):
"""{feed_id: unread_count} for every feed the user subscribes to, in one
query — used by the sidebar, so it must stay cheap regardless of how
many feeds/articles exist."""
"""{feed_id: unread_count} for every feed the user subscribes to — used
by the sidebar's context processor, which runs on every authenticated
page render, so the underlying join/group-by over the user's whole
article corpus is cached for a short TTL rather than recomputed on
every single page view (C3). apps.core.cache_utils.
invalidate_sidebar_unread_cache is called wherever a request changes
the current user's own read state, so this is rarely visibly stale.
"""
cache_key = sidebar_unread_cache_key(user.pk)
counts = cache.get(cache_key)
if counts is not None:
return counts
rows = (
Article.objects.filter(feed__subscriptions__user=user)
.exclude(user_states__user=user, user_states__is_read=True)
@@ -54,4 +67,6 @@ def unread_counts_by_feed(user):
.values("feed_id")
.annotate(unread=Count("id"))
)
return {row["feed_id"]: row["unread"] for row in rows}
counts = {row["feed_id"]: row["unread"] for row in rows}
cache.set(cache_key, counts, SIDEBAR_UNREAD_CACHE_TTL_SECONDS)
return counts
+60
View File
@@ -0,0 +1,60 @@
import pytest
from django.contrib.auth import get_user_model
from apps.core.cache_utils import invalidate_sidebar_unread_cache
from apps.feeds.models import Feed, Subscription
from .models import Article
from .queries import unread_counts_by_feed
User = get_user_model()
@pytest.fixture
def alice(db):
return User.objects.create_user("alice", password="pw-alice-123")
@pytest.fixture
def feed(db):
return Feed.objects.create(feed_url="https://example.com/feed.xml")
@pytest.mark.django_db
def test_unread_counts_by_feed_is_cached_across_calls(alice, feed):
"""unread_counts_by_feed (C3) caches its result — a second call right
after a new unread article appears must still see the stale cached
value until invalidated or the TTL elapses, proving it isn't
recomputing the query every time."""
Subscription.objects.create(user=alice, feed=feed)
Article.objects.create(feed=feed, guid="a-1")
first = unread_counts_by_feed(alice)
assert first == {feed.pk: 1}
Article.objects.create(feed=feed, guid="a-2")
second = unread_counts_by_feed(alice)
assert second == {feed.pk: 1}, "expected the cached count, not a fresh recount"
@pytest.mark.django_db
def test_invalidate_sidebar_unread_cache_forces_a_recount(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
Article.objects.create(feed=feed, guid="a-1")
unread_counts_by_feed(alice)
Article.objects.create(feed=feed, guid="a-2")
invalidate_sidebar_unread_cache(alice.pk)
assert unread_counts_by_feed(alice) == {feed.pk: 2}
@pytest.mark.django_db
def test_unread_counts_by_feed_cache_is_per_user(alice, feed):
bob = User.objects.create_user("bob", password="pw-bob-123")
Subscription.objects.create(user=alice, feed=feed)
Subscription.objects.create(user=bob, feed=feed)
Article.objects.create(feed=feed, guid="a-1")
assert unread_counts_by_feed(alice) == {feed.pk: 1}
assert unread_counts_by_feed(bob) == {feed.pk: 1}