httpcache

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

httpcache

A simple HTTP Client cache for Go that uses go-redis/cache as the storage layer.

It can respect cache headers to act like a private client (e.g like a browser or API client) or cache all responses if desired.

Why this exists

Two reasons:

  1. You need a http client that respects cache headers and doesn't request cached assets multiple times.
  2. You want to cache API responses at a low level so that your application code doesn't abuse the upstream API.

Both these use cases are not supported out of the box with http.Client in the Go standard library.

Usage

// Create a new cache using Redis Cache
myCache := cache.New(&cache.Options{
Redis: redisClient,
})

// Setup transport (using tripware)
transport := tripware.New(http.DefaultTransport)
transport.Use(httpcache.WithCache(myCache, 1*time.Minute))
client := &http.Client{Transport: transport}

// Alternative setup transport using http.RoundTripper
transport := httpcache.NewCacheTransport(http.DefaultTransport, myCache, 1*time.Minute)
client := &http.Client{Transport: transport}

See simple GET example for a runnable example.

Configuration

Check
func(req *http.Request) bool

A function that checks if the current request is cacheable.

Default: All GET and HEAD requests that DO NOT specify a range header.

CacheKeyFn
func(req *http.Request) string

Specifies the function to generate cache keys if the current solution doesn't meet your requirements.

Default: httpcache:METHOD:URLENCODED(URL)

Documentation

Index

Constants

View Source
const HTTPCacheHeader = "X-Http-Cache"

Variables

This section is empty.

Functions

func CacheKey

func CacheKey(req *http.Request) string

func DefaultRequestChecker

func DefaultRequestChecker(req *http.Request) bool

func IsCachedResponse

func IsCachedResponse(resp *http.Response) bool

func WithCache

func WithCache(c *cache.Cache, opts ...Option) tripware.Tripperware

WithCache returns a http.Client tripware that will cache responses. Use with tripware pkg.

Types

type Cache

type Cache interface {
	Set(key string, data []byte)
	Get(key string) ([]byte, bool)
	Del(key string)
}

type Option

type Option func(*Transport)

func WithCacheKeyFn

func WithCacheKeyFn(fn func(req *http.Request) string) Option

WithCacheKeyFn returns a new RoundTripper that will use a custom function to generate cache keys

func WithRequestChecker

func WithRequestChecker(check func(req *http.Request) bool) Option

WithRequestChecker returns a new RoundTripper that will check if a request should be cached

func WithTTL

func WithTTL(ttl time.Duration) Option

WithCache returns a new RoundTripper that will cache responses

type RedisCache

type RedisCache struct {
	TTL time.Duration
}

func (*RedisCache) Set

func (c *RedisCache) Set(key string, data []byte)

type Transport

type Transport struct {
	// The RoundTripper interface actually used to make requests
	// If nil, http.DefaultTransport is used
	Cache *cache.Cache
	// contains filtered or unexported fields
}

Transport is an implementation of http.RoundTripper that will return values from a cache where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since) to repeated requests allowing servers to return 304 / Not Modified

func NewCacheTransport

func NewCacheTransport(next http.RoundTripper, c *cache.Cache, options ...Option) *Transport

func (*Transport) RoundTrip

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

RoundTrip implements the RoundTripper interface

Directories

Path Synopsis
examples
simple-get command

Jump to

Keyboard shortcuts

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