Skip to main content
 首页 » 编程设计

python之Flask:缓存静态文件(.js、.css)

2024年10月25日15lyhabc

我真的找不到这方面的任何资源。那么我怎样才能将 View /函数的缓存与静态文件(即.css、.js)分开呢? 我想将我的静态对象缓存一周,另一方面我只需要缓存函数/ View 几分钟。

当我跟随

from flask.ext.cache import Cache 
cache = Cache(config={'CACHE_TYPE': 'simple'}) 
cache.init_app(app) 
 
@cache.cached(timeout=500) 
def index(): 
 
    return render_template('index.html') 

然后所有 View ,对象的缓存时间设置为相同的值,500。如何去做?

请您参考如下方法:

我不会为我的 python 应用程序提供静态文件,而是尝试将其委​​托给 Web 服务器(nginx、apache...)。 然后您可以通过 header 设置过期时间,控制浏览器缓存它们的时间。