首页 > 代码库 > Flask 高效开发实战-flask2

Flask 高效开发实战-flask2

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

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