Checking if stories are shared through redis before asking mongo.

This commit is contained in:
Samuel Clay 2014-04-29 11:58:18 -07:00
parent af333e5382
commit db2ffb0f87
2 changed files with 28 additions and 9 deletions

View file

@ -581,10 +581,12 @@ def load_single_feed(request, feed_id):
story_feed_id=feed.pk, story_feed_id=feed.pk,
story_hash__in=story_hashes)\ story_hash__in=story_hashes)\
.only('story_hash', 'starred_date', 'user_tags') .only('story_hash', 'starred_date', 'user_tags')
shared_stories = MSharedStory.objects(user_id=user.pk, shared_story_hashes = MSharedStory.check_shared_story_hashes(user.pk, story_hashes)
story_feed_id=feed_id, shared_stories = []
story_hash__in=story_hashes)\ if shared_story_hashes:
.only('story_hash', 'shared_date', 'comments') 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, starred_stories = dict([(story.story_hash, dict(starred_date=story.starred_date,
user_tags=story.user_tags)) user_tags=story.user_tags))
for story in starred_stories]) 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_feed_ids = list(set(story_feed_ids).difference(set(usersub_ids)))
unsub_feeds = Feed.objects.filter(pk__in=unsub_feed_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) unsub_feeds = dict((feed.pk, feed.canonical(include_favicon=False)) for feed in unsub_feeds)
shared_stories = MSharedStory.objects(user_id=user.pk, shared_story_hashes = MSharedStory.check_shared_story_hashes(user.pk, story_hashes)
story_hash__in=story_hashes)\ shared_stories = []
.only('story_hash', 'shared_date', 'comments') 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, shared_stories = dict([(story.story_hash, dict(shared_date=story.shared_date,
comments=story.comments)) comments=story.comments))
for story in shared_stories]) for story in shared_stories])

View file

@ -1399,7 +1399,7 @@ class MSharedStory(mongo.Document):
meta = { meta = {
'collection': 'shared_stories', 'collection': 'shared_stories',
'indexes': [('user_id', '-shared_date'), ('user_id', 'story_feed_id'), '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, 'index_drop_dups': True,
'ordering': ['-shared_date'], 'ordering': ['-shared_date'],
'allow_inheritance': False, 'allow_inheritance': False,
@ -1676,7 +1676,21 @@ class MSharedStory(mongo.Document):
shared_story.publish_update_to_subscribers() shared_story.publish_update_to_subscribers()
return shared 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 @classmethod
def sync_all_redis(cls, drop=False): def sync_all_redis(cls, drop=False):
r = redis.Redis(connection_pool=settings.REDIS_POOL) r = redis.Redis(connection_pool=settings.REDIS_POOL)