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

golang 缓存模版的方法

这是官方使用的方法,实例初始化时把所有模版渲染后缓存到 templates,后续使用ExecuteTemplate 方法来使用特定的模版...

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

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

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

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

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

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

一个简单高效的LRU 缓存,golang 实现

LRU(Least recently used,最近最少使用)是根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”。...

用github 帐号登录之tornado 实现

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

Leave a Comment