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

在GAE 上正确使用缓存优化程序

缓存在应用中经常会用到,为了避免一些需要长时间才能得到的结果多次重复获取。GAE 是一个分布式平台,数据操作和网络访问都需要很长的时间,更应该在这样的操作里添加缓存。...

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

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

python 标准库读写CSV 文件

CSV 是(Comma Separated Values 逗号分隔值)的英文缩写,通常都是纯文本文件。这里介绍使用python 标准库读写csv 文件的方法。...

Leave a Comment