caching - Go webserver - 不要使用时间戳缓存文件

标签 caching go webserver

我在嵌入式系统上运行一个用 go 语言编写的网络服务器。如果有人降级固件版本,index.html 的时间戳可能会倒退。如果 index.html 比以前的版本旧,服务器会发送一个 http 304 响应(未修改),并提供文件的缓存版本。

网络服务器代码使用了 http.FileServer() 和 http.ListenAndServe()。

通过使用 Posix 命令 touch 修改 index.html 的时间戳可以轻松重现该问题

touch -d"23:59" index.html

重新加载页面,然后

touch -d"23:58" index.html

这次重新加载将在 index.html 上给出 304 响应。

有没有办法阻止基于时间戳的缓存?

最佳答案

假设您的文件服务器代码类似于 example in the docs :

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/static"))))

您可以编写一个处理程序来设置适当的缓存 header ,通过剥离 ETag header 并设置 Cache-Control: no-cache, private, max-age=0 来防止缓存(在本地和上游代理中):

var epoch = time.Unix(0, 0).Format(time.RFC1123)

var noCacheHeaders = map[string]string{
    "Expires":         epoch,
    "Cache-Control":   "no-cache, private, max-age=0",
    "Pragma":          "no-cache",
    "X-Accel-Expires": "0",
}

var etagHeaders = []string{
    "ETag",
    "If-Modified-Since",
    "If-Match",
    "If-None-Match",
    "If-Range",
    "If-Unmodified-Since",
}

func NoCache(h http.Handler) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {
        // Delete any ETag headers that may have been set
        for _, v := range etagHeaders {
            if r.Header.Get(v) != "" {
                r.Header.Del(v)
            }
        }

        // Set our NoCache headers
        for k, v := range noCacheHeaders {
            w.Header().Set(k, v)
        }

        h.ServeHTTP(w, r)
    }

    return http.HandlerFunc(fn)
}

像这样使用它:

http.Handle("/static/", NoCache(http.StripPrefix("/static/", http.FileServer(http.Dir("/static")))))

注意:我最初是在 github.com/zenazn/goji/middleware 写这篇文章的,所以你也可以只导入它,但这是一段简单的代码,我想为后代展示一个完整的例子!

关于caching - Go webserver - 不要使用时间戳缓存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880343/

相关文章:

caching - GPU 中的 L1 缓存

Go在docker容器中找不到/usr/share/zoneinfo

html - 为什么这个 http 对话有 10 秒的延迟?

php - 单一缓存前端和后端

java - 为 spring-boot redis 缓存配置配置一个新的序列化器

recursion - Go 中的短变量声明和长变量声明之间的闭包范围区别是什么?

unit-testing - 如何使用相互依赖的接口(interface)方法模拟结构

Java,HttpUrlConnection。 getResponseCode() 的问题

java - 在 LAN 中运行 Jetty Web 服务器

javascript - 使用 ServiceWorker 缓存 iframe 请求