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

tornado websocket 客户端与服务器端示例

最近在网上找了些websocket的资料看了下,node和tornado等等本身已经实现了websocket的封装,所以使用起来会比较简单,php如果想要写websocket还需要自己跑一整套流程,比较麻烦。...

在128M的VPS上配置mysql+Tornado+Nginx笔记

最近 123systems http://goo.gl/2Q0X2 又推出一年$10的便宜 VPS,128M内存,可以用来学习。在这样的vps 上放一个博客或做反向代理绰绰有余,买下后尝试配一个mysql+Tornado+Nginx 环境。...

SAE+python+Tornado+pyTenjin 的完整示例

python 简单易懂,Tornado 高效易学,pyTenjin 轻巧快速,SAE 安全稳定使用门槛低。现在把他们结合在一起做了一个可运行在SAE 上的完整示例。...

用github 帐号登录之tornado 实现

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

Leave a Comment