def _get_from_cache(cls, subreddit_id): cache_key = Subreddit.make_cache_key(subreddit_id) logger.info( f"Getting subreddit listing from cache at key {cache_key}.") ids = list_cache.get(cache_key, start=0) ids_cache = Thread.make_cache_keys(map(str, ids)) logger.info( f"Getting thread listing from cache with keys {ids[:5]}...") items = cache.get_many(ids_cache) # For the items not found in cache, fetch from database items_found = [] items_not_found_ids = [] for i in range(len(ids)): if items[i]: items_found.append(items[i]) else: items_not_found_ids.append(ids[i]) logger.info(f"Did not find {len(ids_not_found)} in cache.") if items_not_found_ids: logger.info("Fetching missing items from database.") items_found_in_db = self._get_from_db_w_ids(items_not_found_ids) logger.info( f"Found {len(items_found_in_db)}/{len(ids_not_found)} missing cache items in database." ) items_found.extend(items_found_in_db) return items_found
def handle_update(self, ch, method, props, body): logger.info("Vote updated; atomically updating scores.") try: body = json.loads(body)['body'] post_id = body['voted_thread_id'] user_id = body['voter_id'] direction = body['direction'] except json.JSONDecodeError as ex: logger.info("Error while decoding json:", str(ex)) thread_key = Thread.make_cache_key(post_id) return cache.incr_field(thread_key, 'score', direction * -1 * 2)
def _get_from_cache(cls, user_id): # TODO: retrieve back ups from database query = select([subscriptions.c.subreddit_id])\ .where(subscriptions.c.subscriber_id == user_id) sr_ids = db.engine.execute(query) sr_ids = Subreddit.make_cache_keys(map(lambda pair: pair[0], sr_ids)) logger.info(f'Subreddit followed: {sr_ids}') sr_listing_ids = list_cache.get_many(sr_ids) sr_listing_ids = Thread.make_cache_keys(chain(*sr_listing_ids)) logger.info(f"Fetched cached listing ids {sr_listing_ids[:5]}...") items = cache.get_many(sr_listing_ids) logger.info(f"Fetched {len(items)} cached items.") return items
def update_vote(thread=None, direction=None): vote = Vote.get(current_user.id, thread) thread = Thread.get_or_404(thread) if not vote: raise InvalidUsage.resource_not_found() try: thread.update_vote(vote) vote.update(direction, commit=False) thread.save() except Exception as ex: db.session.rollback() raise InvalidUsage.duplicate() return dict(message='Vote updated.', status=204), 204
def handle_delete(self, ch, method, properties, body): logger.info(f"Handling comment.delete. {body}") try: body = json.loads(body)['body'] thread_id = body['thread_id'] thread_cache_key = Thread.make_cache_key(thread_id) cache.incr_field(thread_cache_key, 'num_comments', -1) # Acknowledgement ch.basic_ack(delivery_tag=method.delivery_tag) logger.info("Finished updating number of comments in cache.") except json.JSONDecodeError as ex: logger.warning("Error while decoding json:", str(ex))
def create_vote(thread=None, direction=None): if not direction or direction not in [-1, 1]: raise InvalidUsage.validation_error() thread = Thread.get_or_404(thread) try: vote = Vote(voter_id=current_user.id, voted_thread_id=thread.id, direction=direction) vote.save(commit=False) thread.add_vote(vote) thread.save(commit=True) except exc.IntegrityError as ex: db.session.rollback() raise InvalidUsage.duplicate() vote_data = vote_schema.dump(vote) return dict(message='Vote created', status=201), 201