flask-cache

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

一、安装flask

  推荐使用 pip install flask-cache 缓存

二、配置和使用app

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() 方法装修有参数的函数函数

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息