本文整理汇总了Python中models.Article.cache_key方法的典型用法代码示例。如果您正苦于以下问题:Python Article.cache_key方法的具体用法?Python Article.cache_key怎么用?Python Article.cache_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Article
的用法示例。
在下文中一共展示了Article.cache_key方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_article
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import cache_key [as 别名]
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: update_article
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import cache_key [as 别名]
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