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.go42 chars2 lines
Then modify your code to use the httpcache.DiskCache
as a transport to the http.Client instance:
main.go470 chars29 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