From db2ffb0f875fc9a7829a47451bad6d76f978cbbd Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Tue, 29 Apr 2014 11:58:18 -0700 Subject: [PATCH] Checking if stories are shared through redis before asking mongo. --- apps/reader/views.py | 19 ++++++++++++------- apps/social/models.py | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/apps/reader/views.py b/apps/reader/views.py index dba49e758..222d0e4d7 100644 --- a/apps/reader/views.py +++ b/apps/reader/views.py @@ -581,10 +581,12 @@ def load_single_feed(request, feed_id): story_feed_id=feed.pk, story_hash__in=story_hashes)\ .only('story_hash', 'starred_date', 'user_tags') - shared_stories = MSharedStory.objects(user_id=user.pk, - story_feed_id=feed_id, - story_hash__in=story_hashes)\ - .only('story_hash', 'shared_date', 'comments') + shared_story_hashes = MSharedStory.check_shared_story_hashes(user.pk, story_hashes) + shared_stories = [] + if shared_story_hashes: + shared_stories = MSharedStory.objects(user_id=user.pk, + story_hash__in=shared_story_hashes)\ + .only('story_hash', 'shared_date', 'comments') starred_stories = dict([(story.story_hash, dict(starred_date=story.starred_date, user_tags=story.user_tags)) for story in starred_stories]) @@ -781,9 +783,12 @@ def load_starred_stories(request): unsub_feed_ids = list(set(story_feed_ids).difference(set(usersub_ids))) unsub_feeds = Feed.objects.filter(pk__in=unsub_feed_ids) unsub_feeds = dict((feed.pk, feed.canonical(include_favicon=False)) for feed in unsub_feeds) - shared_stories = MSharedStory.objects(user_id=user.pk, - story_hash__in=story_hashes)\ - .only('story_hash', 'shared_date', 'comments') + shared_story_hashes = MSharedStory.check_shared_story_hashes(user.pk, story_hashes) + shared_stories = [] + if shared_story_hashes: + shared_stories = MSharedStory.objects(user_id=user.pk, + story_hash__in=shared_story_hashes)\ + .only('story_hash', 'shared_date', 'comments') shared_stories = dict([(story.story_hash, dict(shared_date=story.shared_date, comments=story.comments)) for story in shared_stories]) diff --git a/apps/social/models.py b/apps/social/models.py index 3b5acccd4..219371ad6 100644 --- a/apps/social/models.py +++ b/apps/social/models.py @@ -1399,7 +1399,7 @@ class MSharedStory(mongo.Document): meta = { 'collection': 'shared_stories', 'indexes': [('user_id', '-shared_date'), ('user_id', 'story_feed_id'), - 'shared_date', 'story_guid', 'story_feed_id'], + 'shared_date', 'story_guid', 'story_feed_id', 'story_hash'], 'index_drop_dups': True, 'ordering': ['-shared_date'], 'allow_inheritance': False, @@ -1676,7 +1676,21 @@ class MSharedStory(mongo.Document): shared_story.publish_update_to_subscribers() return shared - + + @staticmethod + def check_shared_story_hashes(user_id, story_hashes, r=None): + if not r: + r = redis.Redis(connection_pool=settings.REDIS_POOL) + pipeline = r.pipeline() + + for story_hash in story_hashes: + feed_id, guid_hash = MStory.split_story_hash(story_hash) + share_key = "S:%s:%s" % (feed_id, guid_hash) + pipeline.sismember(share_key, user_id) + shared_hashes = pipeline.execute() + + return [story_hash for s, story_hash in enumerate(story_hashes) if shared_hashes[s]] + @classmethod def sync_all_redis(cls, drop=False): r = redis.Redis(connection_pool=settings.REDIS_POOL)