def handle_bottle_info(id): """ The request is in form of flask.request Try to get data from cache first, if not available there search the DB. """ try: bottle = dumps(redis_get(id)) logging.debug("item: {} was taken from the cache".format(bottle)) except TypeError: # cPickle will error out if it tries to decode None with execute_session() as session: bottle = dumps(sqla_obj_to_dict(Cellar.get_row_by_id(id, session))) logging.debug("item: {} was taken from the db".format(bottle)) _update_total_views(id) return bottle
def build_cache(): """ Rows should be an iterator of SQLA rows """ def is_close_to_eom(): # End of memory return redis.info()["used_memory"] + CACHE_BYTE_MARGIN >= MAX_CACHE_SPACE # Clear the cache first redis.flushdb() rows = get_rows_by_total_views(ARBITRARY_LIMIT) while not is_close_to_eom(): try: to_add = sqla_obj_to_dict(rows.next()) except StopIteration: break else: logging.debug("Add {} to the cache".format(to_add)) redis_set(to_add["id"], to_add) logging.info("Finished adding rows to the cache")