码迷,mamicode.com
首页 > 其他好文 > 详细

Flask 高效开发实战-flask2

时间:2017-06-12 10:41:55      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:开发实战   app   path   sim   htm   html   response   ret   simple   

回调接入点-页面缓存逻辑

from flask import Flask,request,render_template
from werkzeug.contrib.cache import SimpleCache
app = Flask(__name__)

CACHE_TIMEOUT = 300
cache = SimpleCache()
cache.timeout = CACHE_TIMEOUT

@app.before_request
def return_cached():
    if not request.values:
        response = cache.get(request.path)
        if response:
            print(‘从网页获取了cache‘)
            return response
    print(‘将会加载网页‘)

@app.after_request
def cache_response(response):
    if not request.values:
        cache.set(request.path,response,CACHE_TIMEOUT)
    return response

@app.route(‘/get_index‘)
def index():
    return render_template(‘index.html‘)

 使用过滤器

 

Flask 高效开发实战-flask2

标签:开发实战   app   path   sim   htm   html   response   ret   simple   

原文地址:http://www.cnblogs.com/Erick-L/p/6991170.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!