Use the DefaultCachedClient
request,err := http.NewRequest("GET","http://example.com",nil)
DefaultCashedClient.Do(request) //not cached
...
DefaultCachedClient.Do(request) //cached
...
Use the DefaultCachedTransport
request, err := http.NewRequest("GET", "http://example.com", nil)
client := http.Client{
Transport: DefaultCachedTransport,
}
client.Do(request) //not cached
client.Do(request) //cached
Create own CachedTransport
request, err := http.NewRequest("GET", "http://example.com", nil)
cachedTransport := CachedTransport{
Cache: NewMapCache(),
Fallback: http.DefaultTransport,
}
client := http.Client{
Transport: &cachedTransport,
}
client.Do(request) //not cached
client.Do(request) //cached
Create own Cacher
type MyCache struct {
}
func (m *MyCache) Get(req *http.Request) (*http.Response, error) {
panic("implement me")
}
func (m *MyCache) Set(req *http.Request, res *http.Response) error {
panic("implement me")
}
func someFunction() {
request, err := http.NewRequest("GET", "http://example.com", nil)
cachedTransport := CachedTransport{
Cache: &MyCache{},
Fallback: http.DefaultTransport,
}
client := http.Client{
Transport: &cachedTransport,
}
client.Do(request) //(not cached)
client.Do(request) //(cached)
}
type Cacher interface {
Get(req *http.Request) (*http.Response, error)
Set(req *http.Request, res *http.Response) error
}
type MapCache struct {
cache map[string]*http.Response
MapCacheOptions
}
type MapCacheOptions struct {
IgnoreRequestBody bool
DontIncludeAllRequestHeaders bool
}
type FileCache struct {
*MapCache
filePath string
file *os.File
}