httpcache

package module
v0.0.0-...-b58218f Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 9 Imported by: 1

README

HTTPcache

Go Report Card Go Version Build Passing

HTTPcache is a fast, local cache for HTTP requests and responses and wraps the default http.RoundTripper from Go standard library.

Features

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!

Quick Start

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)
    }
}

❓ Questions and Support

Any questions can be submitted via GitHub Issues. Feel free to start contributing or asking any questions required!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoResponse = errors.New("no stored response")
)

Functions

func WithAllowedStatusCodes

func WithAllowedStatusCodes(allowedStatusCodes []int) func(*CachedRoundTripper)

func WithCacheStore

func WithCacheStore(store ResponseStorer) func(*CachedRoundTripper)

func WithDeniedStatusCodes

func WithDeniedStatusCodes(deniedStatusCodes []int) func(*CachedRoundTripper)

func WithExpiryTime

func WithExpiryTime(expiryTime time.Duration) func(*CachedRoundTripper)

func WithName

func WithName(name string) func(*CachedRoundTripper)

Types

type CachedRoundTripper

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

func NewCachedRoundTripper

func NewCachedRoundTripper(options ...func(*CachedRoundTripper)) *CachedRoundTripper

func (*CachedRoundTripper) RoundTrip

func (h *CachedRoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

type ResponseStorer

type ResponseStorer interface {
	Save(response *http.Response) error
	Read(request *http.Request) (*http.Response, error)
	SaveContext(ctx context.Context, response *http.Response) error
	ReadContext(ctx context.Context, request *http.Request) (*http.Response, error)
}

Jump to

Keyboard shortcuts

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