httpcache

package module
v0.0.0-...-775d0ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2019 License: MIT Imports: 8 Imported by: 0

README

httpcache

GoDoc Go Report Card

As simple as it gets HTTP cache. It is heavily inspired by gregjones/httpcache, but it ignores the response headers. If you need a cache that respects these, please take a look at gregjones/httpcache and lox/httpcache.

Usage

cache := memcache.New()
httpClient := httpcache.New(cache).Client()

Per default every response is cached, even POST with a status code outside the range [200-300[. You can specify if a response should get saved by providing a function using the Verify option.

cache := memcache.New()
client := httpcache.New(cache,
  httpcache.WithVerifier(func(req *http.Request, res *http.Response) bool {
    return res.StatusCode >= 200 && res.StatusCode < 300
  }),
).Client()

A common example, only cache GET requests that don't fail, could look like the following.

client := httpcache.New(memcache.New(),
  httpcache.WithVerifier(httpcache.StatusInTwoHundreds),
  httpcache.WithVerifier(httpcache.RequestMethod(http.MethodGet)),
).Client()

Cache

Currently there are two cache methods: in memory and on disk using diskv. Caches have to implement the Cache interface, which is basically a key-value-store with two functions Get and Set.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StatusInTwoHundreds

func StatusInTwoHundreds(req *http.Request, res *http.Response) bool

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.

func WithVerifier

func WithVerifier(v Verifier) func(*Transport)

WithVerifier adds a verifier that tests if a http.Response should be cached, i.e. the status code is OK or the body contains relevant information.

Types

type Cache

type Cache interface {
	Get(key string) ([]byte, error)
	Set(key string, dump []byte) error
}

Cache is a key value store.

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport implements the http.RoundTripper interface.

func New

func New(c Cache, options ...func(*Transport)) *Transport

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

func (t *Transport) Client() *http.Client

Client returns a new cached http.Client.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction, returning a Response for the provided Request.

type Verifier

type Verifier func(*http.Request, *http.Response) bool

Verifier determine if a given request-response-pair should get cached.

func RequestMethod

func RequestMethod(method string) Verifier

RequestMethod returns true if a given request method is used.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL