redis 缓存目标

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值