flask缓存

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为参数的一个装饰器

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,199评论 19 139
  • 各位简书的读者们: 你们好! 由于我正式从小学毕业,所以要准备开始更...
    快乐米奇阅读 138评论 0 1
  • 一轮圆月东山起, 七彩残阳已西沉。 虔诚冥币筑金屋, 跹舞人间寄先灵。
    喷泉阅读 217评论 0 3
  • (诗:纳苏。) 街边, 那个破旧的绿色邮筒, 被拾荒的老人扔进一封 没有地址的信。 应当送达的是 江南水乡,那个撑...
    纳苏阅读 415评论 2 2