Flask 高效開發實戰-flask2
阿新 • • 發佈:2017-06-12
request before log val sim value timeout out strong
回調接入點-頁面緩存邏輯
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