Only call get_audience once in add_status

This is by far the most expensive part of this task, so this should
double the speed in the increment_unread case.

Related: #2720
This commit is contained in:
Wesley Aptekar-Cassels 2023-04-05 21:03:07 -04:00
parent 78607a0c3e
commit 7a93b5c315

View file

@ -38,13 +38,14 @@ class ActivityStream(RedisStore):
def add_status(self, status, increment_unread=False):
"""add a status to users' feeds"""
audience = self.get_audience(status)
# the pipeline contains all the add-to-stream activities
pipeline = self.add_object_to_stores(
status, self.get_stores_for_object(status), execute=False
status, self.get_stores_for_users(audience), execute=False
)
if increment_unread:
for user_id in self.get_audience(status):
for user_id in audience:
# add to the unread status count
pipeline.incr(self.unread_id(user_id))
# add to the unread status count for status type
@ -149,6 +150,10 @@ class ActivityStream(RedisStore):
trace.get_current_span().set_attribute("stream_id", self.key)
return [user.id for user in self._get_audience(status)]
def get_stores_for_users(self, user_ids):
"""convert a list of user ids into redis store ids"""
return [self.stream_id(user_id) for user_id in user_ids]
def get_stores_for_object(self, obj):
"""the stores that an object belongs in"""
return [self.stream_id(user_id) for user_id in self.get_audience(obj)]