Flask:缓存静态文件(.js,.css) [英] Flask: Caching static files (.js, .css)

查看:562
本文介绍了Flask:缓存静态文件(.js,.css)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的找不到任何资源。那么我怎么能比静态文件(即.css,.js)分离缓存视图/函数呢?
我希望缓存我的静态对象一个星期,另一方面我需要缓存函数/视图只有几分钟。



当我做下面是来自flask.ext.cache的缓存

 。缓存
cache = Cache(config = {'CACHE_TYPE':'simple '))
cache.init_app(app)

@ cache.cached(timeout = 500)
def index():

return render_template 'index.html')

然后将所有视图,对象的缓存时间设置为相同的值, 500.怎么去解决这个问题?

解决方案

我不会从我的python应用程序服务器静态文件,但试图委托网络服务器(nginx,apache ...)。
然后,您可以设置时间通过标题控制浏览器缓存多久。


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?

解决方案

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.

这篇关于Flask:缓存静态文件(.js,.css)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆