HTTPcache is a fast, local cache for HTTP requests and responses and wraps the default http.RoundTripper
from Go standard library.
HTTPcache has a few useful features:
- Store and retrieve HTTP responses for any type of request.
- Expire responses after a customisable time duration.
- Decide when to store responses based on status code.
If you want to request a feature then open a GitHub Issue today!
This module can be installed using the command line:
go get -u github.com/alexmerren/httpcache
Here's an example of using the httpcache
module to cache responses:
package main
func main() {
// Create a new cached round tripper that:
// * Only stores responses with status code 200.
// * Refuses to store responses with status code 404.
cache := httpcache.NewCachedRoundTripper(
httpcache.WithAllowedStatusCodes([]int{200}),
httpcache.WithDeniedStatusCodes([]int{404}),
)
// Create HTTP client with cached round tripper.
httpClient := &http.Client{
Transport: cache,
}
// Do first request to populate local database.
httpClient.Get("https://www.google.com")
// Subsequent requests read from database with no outgoing HTTP request.
for _ = range 10 {
response, _ = httpClient.Get("https://www.google.com")
defer response.Body.Close()
responseBody = io.ReadAll(response.body)
fmt.Println(responseBody)
}
}
Any questions can be submitted via GitHub Issues. Feel free to start contributing or asking any questions required!