1.数值
验证码
用户状态
2.数据记录
1.Caching at the object level
以数据库对象的角度考虑, 应用更普遍
user = User.query.filter_by(id=1).first()
user -> User对象
{
'user_id':1,
'user_name': 'python',
'age': 28,
'introduction': ''
}
2.Caching at the database query level
以数据库查询的角度考虑,应用场景较特殊,一般仅针对较复杂的查询进行使用
就是直接缓存查询的结果
query_result = User.query.join(User.profile).filter_by(id=1).first()
-> sql = "select a.user_id, a.user_name, b.gender, b.birthday from tbl_user as a inner join tbl_profile as b on a.user_id=b.user_id where a.user_id=1;"
# hash算法 md5
query = md5(sql) # 'fwoifhwoiehfiowy23982f92h929y3209hf209fh2'
# redis
setex(query, expiry, json.dumps(query_result))
3.视图的响应结果
@route('/articles')
@cache(exipry=30*60)
def get_articles():
ch = request.args.get('ch')
articles = Article.query.all()
for article in articles:
user = User.query.filter_by(id=article.user_id).first()
comment = Comment.query.filter_by(article_id=article.id).all()
results = {...} # 格式化输出
return results
# redis
# '/artciels?ch=1': json.dumps(results)