Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
digest: Clear the cache once we move to a new realm / cutoff value.
Alex Vandivera8a1f10f3cf1
diff --git a/zerver/lib/digest.py b/zerver/lib/digest.pyindex d229e990c06..fcb27a25d16 100644--- a/zerver/lib/digest.py+++ b/zerver/lib/digest.py@@ -150,6 +150,23 @@ def _enqueue_emails_for_realm(realm: Realm, cutoff: datetime.datetime) -> None:)+last_realm_id: Optional[int] = None+last_cutoff: Optional[float] = None+++def maybe_clear_recent_topics_cache(realm_id: int, cutoff: float) -> None:+ # As an optimization, we clear the digest caches when we switch to+ # a new realm or cutoff value. Since these values are part of the+ # cache key, this is not necessary for correctness -- it merely+ # helps reduce the memory footprint of the cache.+ global last_realm_id, last_cutoff+ if last_realm_id != realm_id or last_cutoff != cutoff:+ logger.info("Flushing stream cache: %s", get_recent_topics.cache_info())+ get_recent_topics.cache_clear()+ last_realm_id = realm_id+ last_cutoff = cutoff++# We cache both by stream-id and cutoff, which ensures the per-stream# cache also does not contain data from old digests@functools.lru_cache(maxsize=500)@@ -321,6 +338,8 @@ def bulk_get_digest_context(# Convert from epoch seconds to a datetime object.cutoff_date = datetime.datetime.fromtimestamp(int(cutoff), tz=datetime.timezone.utc)+ maybe_clear_recent_topics_cache(realm.id, cutoff)+stream_id_map = get_slim_stream_id_map(realm)recently_created_streams = get_recently_created_streams(realm, cutoff_date)diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.pyindex 4f43cf26cf6..c424a6b3939 100644--- a/zerver/tests/test_digest.py+++ b/zerver/tests/test_digest.py@@ -104,11 +104,11 @@ class TestDigestEmailMessages(ZulipTestCase):self.assertEqual(get_recent_topics.cache_info().hits, 7)self.assertEqual(get_recent_topics.cache_info().currsize, 5)- # If we use a different cutoff, it makes new cache entries.+ # If we use a different cutoff, it clears the cache.with self.assert_database_query_count(12):bulk_handle_digest_email([cordelia.id, prospero.id], cutoff + 1)- self.assertEqual(get_recent_topics.cache_info().hits, 8)- self.assertEqual(get_recent_topics.cache_info().currsize, 9)+ self.assertEqual(get_recent_topics.cache_info().hits, 1)+ self.assertEqual(get_recent_topics.cache_info().currsize, 4)def test_bulk_handle_digest_email_skips_deactivated_users(self) -> None:"""