美文网首页
flask缓存

flask缓存

作者: 樊海鹏 | 来源:发表于2017-06-23 23:49 被阅读0次
from werkzeug.contrib.cache import SimpleCache

CACHE_TIMEOUT = 300

cache = SimpleCache()

class cached(object):

    def __init__(self, timeout=None):
        self.timeout = timeout or CACHE_TIMEOUT

    def __call__(self, f):
        def decorator(*args, **kwargs):
            response = cache.get(request.path)
            if response is None:
                response = f(*args, **kwargs)
                cache.set(request.path, response, self.timeout)
            return response
        return decorator

@app.route("/")
@cached()
def index():
    return render_template("index.html")

类的实例可以callable,且是以func为参数的一个装饰器

相关文章

网友评论

      本文标题:flask缓存

      本文链接:https://www.haomeiwen.com/subject/agyhcxtx.html