Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StatusInTwoHundreds ¶
StatusInTwoHundreds returns true if the responses' status code is between 200 and 300.
func WithTransport ¶
func WithTransport(transport http.RoundTripper) func(*Transport)
WithTransport replaces the http.DefaultTransport RoundTripper.
Types ¶
type Cache ¶
Cache is a key value store.
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport implements the http.RoundTripper interface.
func New ¶
New returns a new cachable Transport.
Example ¶
Generate a new cached http.Client that saves responses with a status code between 200 and 300 in memory.
package main
import (
"fmt"
"log"
"net/http"
"github.com/bake/httpcache"
"github.com/bake/httpcache/memcache"
)
func main() {
cache := memcache.New()
client := httpcache.New(cache,
// httpcache.Verify(httpcache.StatusInTwoHundreds)
httpcache.WithVerifier(func(req *http.Request, res *http.Response) bool {
return res.StatusCode >= 200 && res.StatusCode < 300
}),
).Client()
res, err := client.Get("https://httpbin.org/ip")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
fmt.Println(res.StatusCode)
}
Output: 200
func (*Transport) Client ¶
Client returns a new cached http.Client.
Source Files
¶
- httpcache.go
Click to show internal directories.
Click to hide internal directories.