Пример #1
0
def get_article(id, user=None):
    """Gets a single article (formatted by json_data) from redis or postgresql
    And performs some pre-filters for the current user
    Args:
        id (int): id
        user (User): the current user
    Returns:
        None if the article is not existed or a dict of article data
    """
    id = int(id)
    key = Article.cache_key(id)
    redis_client = get_client(key)

    article = None
    if not redis_client.exists(key):
        article = Article.find_by_pk(id)
        if article:
            data = article.json_data()
            redis_client.set(key, json.dumps(data))
            article = data
    else:
        if redis_client.type(key) == 'hash':
            print redis_client
        article = json.loads(redis_client.get(key))

    article['user_vote'] = Article.check_vote(user, article['id'])

    return article
Пример #2
0
def update_article(id, article=None):
    """Updates the cache of article :id
    Args:
        id (int): article id
        article (Article): if not provided, an article will be queried from postgresql
    Returns:
        Article
    """
    id = int(id)
    if not article:
        article = Article.find_by_pk(id)

    if article:
        key = Article.cache_key(id)
        redis_client = get_client(key)
        data = article.json_data()
        redis_client.set(key, json.dumps(data))

    return article