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

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

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

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

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

Leave a Comment