cache http responses on file system in go

To cache HTTP responses on the file system in Go, you can use the httpcache package. This package provides an implementation of http.RoundTripper (used by http.Client) that caches responses on disk.

To use the package, first install it via go get:

main.go
go get -u golang.org/x/net/http/httpcache
42 chars
2 lines

Then modify your code to use the httpcache.DiskCache as a transport to the http.Client instance:

main.go
import (
    "net/http"
    "golang.org/x/net/http/httpcache"
)

...

cache := httpcache.NewDiskCache("/tmp/cache")
client := &http.Client{
    Transport: &httpcache.Transport{
        Cache: cache,
        Transport: &http.Transport{},
    },
}

req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
if err != nil {
    // handle error
}

resp, err := client.Do(req)
if err != nil {
    // handle error
}

defer resp.Body.Close()

// use the response
470 chars
29 lines

In this example, httpcache.NewDiskCache creates a new disk cache instance with the specified directory ("/tmp/cache"). The cache directory will be created if it doesn't exist.

The http.Client instance is then created with the httpcache.Transport as its transport. The Cache attribute is set to the disk cache instance, and the Transport attribute is set to the default http.Transport instance.

Now, when making an HTTP request, if the response is already cached on disk, it will be returned from the cache instead of making a new request to the server. If the response is not in the cache or the cache has expired, a new request will be made to the server and the response will be cached.

gistlibby LogSnag