http.FileServer缓存文件并在编辑后提供旧版本 [英] http.FileServer caching files and serving old versions after edit

查看:136
本文介绍了http.FileServer缓存文件并在编辑后提供旧版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在go的核心中有http包的问题。尽管响应正文中的Content-Length是正确的,但看起来文件内容已被缓存。

  package main 

import(
fmt
net / http


func main(){
http.Handle(/,http.FileServer( http.Dir(./ www /)))
err:= http.ListenAndServe(:8080,nil)
if err!= nil {
fmt.Println(err )
}
}

现在假设我们有一个非常简单的html页面:

 <!doctype html> 
< html>
< body>
< p>您好!< / p>
< / body>
< / html>

我执行go程序并访问 http:// localhost:8080 在浏览器中显示:

 您好!

检查回复标题我可以看到以下内容:

 状态码:200 OK 
接受范围:bytes
Content-Length:68
Content-Type:text / html; charset = utf-8
日期:2013年12月20日星期五10:04:03 GMT
上次修改时间:2013年12月20日星期五10:03:32 GMT
< p>
标签包含您好,大家并重新加载页面。我收到以下内容:

 您好!

再次查看响应头文件

 状态码:200 OK 
Accept-Ranges:bytes
Content-Length:77
Content-Type:text / html; charset = utf-8
日期:2013年12月20日星期五10:04:34 GMT
最后修改日期:2013年12月20日星期五10:04:14 GMT
Content-Length
已经改变,并且最后被修改,但不是实际的文件由http.FileServer处理程序提供的内容。即使关闭程序并执行运行src /.../ main.go ,也会发生此问题。我发现迄今为止清除文件的明显缓存版本的唯一方法是重新启动运行该程序的计算机。



我尝试了以下方法:


  • 执行win / ubuntu / osx上的程序10.8.5

  • golang.org/src上的接口,以查看提供的文件是否缓存在磁盘上的任何地方




解决方案

好的,在忽略问题并继续前进几周后,我终于找出问题所在。

为了让我的主计算机相当不习惯,我使用Vagrant来开发使用golang,nodejs和php的应用程序。看来,在Virtual Box共享上运行一个去应用程序与该共享上存储的所有html文件导致此问题。



为了证明这一点,我跨越了一个Vagrant框,将文件从/ vagrant共享目录复制到/ home / vagrant / testing /,然后复制之前列出的所有操作。这使得问题消失。



换句话说,不要使用Virtual Box共享文件夹来托管打算用于http.FileServer方法的文件。

p>

Having issues with the http package in the core of go. It appears that the file contents is cached although the Content-Length in the response body is correct. To demonstrate here is a simplified version of the application I am writing.

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./www/")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println(err)
    }
}

Now suppose we have a very simple html page:

<!doctype html>
<html>
<body>
    <p>Hello there</p>
</body>
</html>

I execute the go program and access http://localhost:8080 in the browser to be presented with:

Hello there

Checking the response headers I can see the following:

Status Code:200 OK
Accept-Ranges:bytes
Content-Length:68
Content-Type:text/html; charset=utf-8
Date:Fri, 20 Dec 2013 10:04:03 GMT
Last-Modified:Fri, 20 Dec 2013 10:03:32 GMT

Now I edit the html file so the <p> tag contains Hello there everyone and reload the page. I get the following:

Hello there

Again looking at the response headers I get

Status Code:200 OK
Accept-Ranges:bytes
Content-Length:77
Content-Type:text/html; charset=utf-8
Date:Fri, 20 Dec 2013 10:04:34 GMT
Last-Modified:Fri, 20 Dec 2013 10:04:14 GMT

So the Content-Length has changed as well as last modified but not the actual file content delivered by the http.FileServer handler. This issue happens even after closing the program down and doing go run src/.../main.go. The only way I have found so far to clear the apparently cached version of the file is to reboot the machine the program is running on.

I have tried the following:

  • Executing program on win / ubuntu / osx 10.8.5
  • Going through the chain of functions / interfaces on golang.org/src to see if the served file is cached on disk anywhere

Any help with this would be very much appreciated.

解决方案

Ok, so after a few weeks of ignoring the problem and moving on I have finally worked out what the issue is.

In order to leave my main computer fairly uncustomised I use Vagrant to develop applications using golang, nodejs and php. It appears that running a go application on a Virtual Box share with all the html files stored on that share causes this issue.

To prove this I span up a Vagrant box and copied the files from the /vagrant shared directory to /home/vagrant/testing/, then replicated all the previously listed actions. This made the problem disappear.

So in other words, don't use a Virtual Box shared folder to host files intended for use in a http.FileServer method.

这篇关于http.FileServer缓存文件并在编辑后提供旧版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆