flask-cache

flask 做为可插拔式的微框架,当然少不了对缓存的支持,flask-cache 配置使用都比较方便

1、安装

  推荐使用 pip install flask-cache 

2、配置和使用

from flask import Flask
from flask.ext.cache import Cache
import datetime
app = Flask(__name__)
cache = Cache(app,config={'CACHE_TYPE': 'simple'})

@app.route('/')
def hello():
    return "hello, world!"

@app.route('/t')
@cache.cached(timeout=60*30)
def cached_page():
    time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    return "hello, world, what's your name, thank you!!...  localtime: " + time

if __name__ == '__main__':
    app.run()

  配置cache时 加入 cache = Cache(app,config={'CACHE_TYPE': 'simple'}) 就可以了,更多复杂的配置见 http://pythonhosted.org/Flask-Cache/  需要缓存某个视图和方法时,只需要在其前面加上 @cache 装饰器语法糖 装饰即可

  注意: cache.cached() 方法装饰没有参数的函数, cache.memoize() 方法装修有参数的函数

  

原文地址:https://www.cnblogs.com/lazyboy1/p/5953126.html