diff --git a/apps/articles/queries.py b/apps/articles/queries.py index 1b8f96e..8782718 100644 --- a/apps/articles/queries.py +++ b/apps/articles/queries.py @@ -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 diff --git a/apps/articles/test_queries.py b/apps/articles/test_queries.py new file mode 100644 index 0000000..5ace671 --- /dev/null +++ b/apps/articles/test_queries.py @@ -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} diff --git a/apps/core/cache_utils.py b/apps/core/cache_utils.py new file mode 100644 index 0000000..c7edf70 --- /dev/null +++ b/apps/core/cache_utils.py @@ -0,0 +1,22 @@ +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)) diff --git a/apps/reader/tests.py b/apps/reader/tests.py index 1ee7337..96a0e66 100644 --- a/apps/reader/tests.py +++ b/apps/reader/tests.py @@ -289,3 +289,57 @@ def test_stream_exposes_the_latest_article_id_and_scope_for_the_banner(client, a assert f"since={article.pk}" in content assert "filter=all" in content + + +@pytest.mark.django_db +def test_sidebar_unread_count_updates_immediately_after_reading_an_article(client, alice): + """unread_counts_by_feed caches its result for a short TTL (C3) — the + reader views that change the current user's own read state must + invalidate it, or the sidebar would show a stale count until the TTL + elapses on every one of these very common actions.""" + feed = Feed.objects.create(feed_url="https://example.com/feed.xml") + Subscription.objects.create(user=alice, feed=feed) + article = Article.objects.create(feed=feed, guid="a-1", title="Hello") + + client.login(username="alice", password="pw-alice-123") + + response = client.get(reverse("reader:stream")) + assert '1' in response.content.decode() + + client.get(reverse("reader:article_detail", args=[article.pk])) + + response = client.get(reverse("reader:stream")) + assert "unread-count" not in response.content.decode() + + +@pytest.mark.django_db +def test_sidebar_unread_count_updates_immediately_after_toggle_read(client, alice): + feed = Feed.objects.create(feed_url="https://example.com/feed.xml") + Subscription.objects.create(user=alice, feed=feed) + article = Article.objects.create(feed=feed, guid="a-1", title="Hello") + UserArticleState.objects.create(user=alice, article=article, is_read=True) + + client.login(username="alice", password="pw-alice-123") + response = client.get(reverse("reader:stream")) + assert "unread-count" not in response.content.decode() + + client.post(reverse("reader:toggle_read", args=[article.pk])) + + response = client.get(reverse("reader:stream")) + assert '1' in response.content.decode() + + +@pytest.mark.django_db +def test_sidebar_unread_count_updates_immediately_after_mark_all_read(client, alice): + feed = Feed.objects.create(feed_url="https://example.com/feed.xml") + Subscription.objects.create(user=alice, feed=feed) + Article.objects.create(feed=feed, guid="a-1", title="Hello") + + client.login(username="alice", password="pw-alice-123") + response = client.get(reverse("reader:stream")) + assert '1' in response.content.decode() + + client.post(reverse("reader:mark_all_read")) + + response = client.get(reverse("reader:stream")) + assert "unread-count" not in response.content.decode() diff --git a/apps/reader/views.py b/apps/reader/views.py index 734d289..2b82ad1 100644 --- a/apps/reader/views.py +++ b/apps/reader/views.py @@ -19,6 +19,7 @@ from apps.articles.models import ( get_system_tag, ) from apps.articles.queries import with_user_state +from apps.core.cache_utils import invalidate_sidebar_unread_cache from apps.feeds.models import Feed FILTER_CHOICES = [ @@ -240,6 +241,7 @@ def mark_all_read(request): for aid in new_article_ids ] ) + invalidate_sidebar_unread_cache(request.user.pk) stream_url = reverse("reader:stream") query = request.GET.urlencode() @@ -254,6 +256,7 @@ def _load_article_and_mark_read(user, article_id): state.is_read = True state.read_at = timezone.now() state.save(update_fields=["is_read", "read_at"]) + invalidate_sidebar_unread_cache(user.pk) article.is_read = state.is_read article.is_starred, article.is_saved_for_later = _article_tag_flags(user, article) @@ -285,6 +288,7 @@ def toggle_read(request, article_id): state.is_read = not state.is_read state.read_at = timezone.now() if state.is_read else None state.save(update_fields=["is_read", "read_at"]) + invalidate_sidebar_unread_cache(request.user.pk) article.is_read = state.is_read article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article) diff --git a/conftest.py b/conftest.py index 443f23f..b12c0d6 100644 --- a/conftest.py +++ b/conftest.py @@ -1,8 +1,22 @@ import pytest +from django.core.cache import cache from apps.accounts.middleware import SetupWizardMiddleware +@pytest.fixture(autouse=True) +def _reset_django_cache(): + """config.settings.test overrides CACHES to LocMemCache, which — like + SetupWizardMiddleware's own cache below — lives in process memory and + survives Django's per-test DB rollback. Without this, a cached + sidebar unread count (apps.core.cache_utils) keyed by a user's pk + could leak into a later, unrelated test that happens to create a user + with the same auto-incrementing pk.""" + cache.clear() + yield + cache.clear() + + @pytest.fixture(autouse=True) def _reset_setup_wizard_cache(): """SetupWizardMiddleware caches "does any User exist" on the class