Flask: Caching static files (.js, .css)

2019-07-24 02:42发布

I really could not find any resource on this. So how can I seperate caching of views/functions than static files (i.e. .css,.js)? I want to cache my static objects for a week, on the other hand I need to cache functions/views for only a few minutes.

When I do following

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')

then all views, objects' cache time is set to the same value, 500. How to go about it?

1条回答
爷、活的狠高调
2楼-- · 2019-07-24 02:54

I would not server the static files from my python application but try to delegate that to the web server (nginx, apache... ). Then you could set the time to expire through headers controlling how long should the browser cache them.

查看更多
登录 后发表回答