Tornado 静态文件禁用缓存的方法

在开发环境里,静态文件需要经常改动,特别是css,可以用下面方式禁用缓存。

Tornado 静态文件禁用缓存的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import tornado.web

class NoCacheStaticFileHandler(tornado.web.StaticFileHandler):
    def set_extra_headers(self, path):
        self.set_header("Cache-control", "no-cache")

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
          # ...
          url(r"/static/(.*)", 
          tornado.web.StaticFileHandler if not options.debug else NoCacheStaticFileHandler, {
                "path": '/var/www/static' 
            }, name="static")

下面是我实际中使用的:

1
2
3
4
handlers = [
            # (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': settings['static_path']})
            (r'/static/(.*)', NoCacheStaticFileHandler, {'path': settings['static_path']})
        ]

其实最简单的方式:

1
2
3
4
5
6
7
8
settings = dict(
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            xsrf_cookies=True,
            cookie_secret=setting.COOKIE_SECRET,
            autoescape=None,
            login_url='/oauth/sign-in',
            debug=True,  # here
        )

本文网址: https://pylist.com/topic/124.html 转摘请注明来源

Suggested Topics

域名被冻结后从七牛下载文件的方法

因为域名备案注销,七牛冻结了绑定的域名,备案的问题一时半刻解决不了,想下载保存在七牛上的文件,寻找下载文件的过程还费了好多周折。...

使用pyTenjin 缓存html 页面片段

pyTenjin 号称是世界上最快的模板引擎,支持在 html 文件里嵌入 python 代码,这功能其它模板引擎也有,但最重要的是 pyTenjin 模板引擎只有一个不到70K的单个文件,简单import 一下就可以使用。...

用github 帐号登录之tornado 实现

用github 帐号登录之tornado 实现,主要面向开发者的可以使用这个第三方登录。在gist 上发现的,直接拿来,简单修改一下。...

Leave a Comment