Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 439
Emmet B Avatar asked Apr 15 '26 19:04

Emmet B


1 Answers

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.

like image 132
chaos Avatar answered Apr 17 '26 10:04

chaos