Private
Public Access
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:
1 parent
b33fe905d2
commit
abf4cef55b
6 files changed
+173
-4
No files matched your search
@@ -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 '<span class="unread-count">1</span>' 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 '<span class="unread-count">1</span>' 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 '<span class="unread-count">1</span>' 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()
|
||||
Reference in new issue
Block a user