1.flask-cache 缓存插件
安装:pip install flask-cache
使用方式:
cache = Cache( config= { "CACHE_TYPE" : "redis" } )
cache. init_app( app程序实例)
@cache. cached ( timeout= 超时时间秒数, key_prefix= '可以设置缓存key的前缀' )
def 视图函数名称( ) :
耗时操作
'''
注意:如果使用的flask-cache插件是0.13.1版本,则需要将jinja2ext.py源码中
第33行代码修改为:from flask_cache import make_template_fragment_key。
'''
cache_config = {
"CACHE_TYPE" : "redis" ,
"CACHE_REDIS_HOST" : "10.12.155.62" ,
"CACHE_REDIS_PORT" : 6379 ,
"CACHE_REDIS_DB" : 3 ,
"CACHE_REDIS_PASSWORD" : '' ,
}
app = Flask( __name__)
app. config[ "SECRECY_KEY" ] = "abcdef"
app. config. from_object( cache_config)
app. register_blueprint( blue)
c. init_app( app, cache_config)
c = Cache( )
@blue. route ( "/cache/" )
@c. cached ( timeout= 30 , key_prefix= "前缀" )
def cache_view ( ) :
return "视图函数返回的结果"
示例代码:
from cache_app import create_app
from flask_script import Manager
app = creat_app( )
manaegr = Manager( app)
if __name__ == "__main__" :
manager. run( )
from cache_app. views import c, blue
cache_config = {
'CACHE_TYPE' : 'redis' ,
'CACHE_REDIS_HOST' : 'lcoalhost' ,
'CACHE_REDIS_PORT' : 6379 ,
'CACHE_REDIS_DB' : 3 ,
'CACHE_REDIS_PASSWORD' : 123
}
def create_app ( ) :
app = Flask( __name__)
app. config. from_object( cache_config)
c. init_app( app, cache_config)
app. register_blueprint( blue)
return app
from flask import Blueprint
from flask_cache import Cache
blue = Blueprint( "myblue" , __name__)
c = Cache( )
@blue. route ( "/cache/" )
@c. cached ( timeout= 30 , key_prefix= "python" )
def my_view ( ) :
print ( "模拟耗时操作......" )
time. sleep( 3 )
return "<h3 style='color:green'>恭喜,你成功了!!!</h3>"
2. 请求钩子
Flask 的请求钩子是通过装饰器的形式实现。
2.1 brefore_first_request
注册一个函数,在处理第一个请求之前运行
2.2 before_request
注册一个函数,在每次请求之前运行
2.3 after_request
正常请求(没有发生异常,或异常被处理了)之后执行
被装饰的请求钩子函数接收一个响应对象的参数,最终一定要返回该响应对象
2.4 teardown_request
注册一个函数(该函数接收一个异常对象的参数),即使有未处理的异常抛出,也在每
次请求之后运行(关闭调试模式)
2.5 使用技巧
在请求钩子函数和视图函数之间共享数据一般使用上下文全局变量g
例如:before_request处理程序可以从数据中加载已登录用户,并将其保存到g.user
中。随后调用视图函数时,视图函数再使用g.user获取用户
示例代码:
from gou_app import create_app
from flask_script import Manager
app = create_app( )
manager = Manager( app)
if __name__ == "__main__" :
manager. run( )
from gou_app. middleware import load_middleware
from gou_app. views import blue
def create_app ( ) :
app = Flask( __name__)
load_middleware( app)
app. register_blueprint( blue)
return app
import random
from flask import request, g
def load_middleware ( app) :
@app. before_first_request
def a ( ) :
print ( "第一次请求 before_first_request......" )
@app. before_request
def b ( ) :
ip = request. remote_addr
print ( ip + "发送了请求,before_request" )
number = random. randint( 1 , 10 )
if number >= 8 :
g. book = "<<九阴真经>>"
return "<h3 style='color:green'>恭喜,已中奖,您的IP是:" + ip + ": 获取的随机数是:" + str ( number) + "</h3>"
g. book = "<<九阳真经>>"
@app. after_request
def c ( response) :
data = g. book
print ( "after_request......,g.book=" , data)
return response
@app. teardown_requet
def d ( ) :
print ( "treardown_request......,e=" , e)
from flask import Blueprint, g
blue = Blueprint( "myblue" , __name__)
@blue. route ( "/index/" )
def index_view ( ) :
data = g. book
try :
5 / 0
except :
print ( "异常已经被处理......" )
return "<h3>拿到武林绝学了:<span style='color:red'>" + data + "</span></h3>"
3. 四大内置对象(request,session,g,config)
1. request
2. .session
3. g: 一个请求中的全局对象,只要在同一个请求内,都可以通过g对象传递数据
4. config:
针对程序实例的配置。
在模板中直接使用config内置对象
在函数中通过current_app.config访问config 对象
遍历config 内容:
for k,v in config对象.items():
print(k,"========>",v)
示例代码:
from inner_obj_app import create_app
from flask_script import Manager
app = create_app( )
manager = Manager( )
if __name__ == "__main__" :
manager. run( )
from flask import Flask
from inner_obj_app. views import blue
def create_app ( ) :
app = Flask( __name__)
app. register_blueprint( blue)
app. config[ "country" ] = "China"
app. config[ "color" ] = "RED"
app. config[ "food" ] = "Noodles"
return app
from flask import Blueprint, render_template, current_app
@blue. route ( "/go/" )
def go_config ( ) :
return render_template( "config_demo.html" )
@blue. route ( "/show/" )
def show_config_view ( ) :
html = ""
for k, v in current_app. config. items( ) :
html += k
html += "------>"
html += str ( v)
html += "<br/>"
return html
config_demo.html
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 模板中演示config内置对象</ title>
</ head>
< body>
{% for k,v in config.items() %}
{{ k }} =========> {{ v }} < br/>
{% endfor %}
</ body>
</ html>