httpcache

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPClient

func NewHTTPClient(cache *HTTPCache) *http.Client

NewHTTPClient returns a fully initialized http.Client that uses the provided cache to store responses. It uses the default http.Client as a base, and replaces the transport with a new, caching RoundTripper.

Types

type Adapter

type Adapter interface {
	Get(key string) (string, bool)
	Set(key string, value string, ttl time.Duration) bool
	Delete(key string)
	Close()
}

type HTTPCache

type HTTPCache struct {
	Adapter
	// contains filtered or unexported fields
}

func NewOtterCache

func NewOtterCache(cache OtterCache, options ...Option) HTTPCache

func (*HTTPCache) With

func (cache *HTTPCache) With(options ...Option)

type Option

type Option func(*HTTPCache)

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets the default TTL for this cache, which is used if the response does not contain a Cache-Control header.

type OtterCache

type OtterCache interface {
	Get(key string) (string, bool)
	Set(key string, value string, ttl time.Duration) bool
	Delete(key string)
	Close()
}

OtterCache mimics the public API of the Otter cache so that we don't have to import it into this library. https://github.com/maypok86/otter

type RoundTripper

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

RoundTripper implements the http.RoundTripper interface, that is used by the http.Client to cache outbound HTTP requests. https://echorand.me/posts/go-http-client-middleware/ https://lanre.wtf/blog/2017/07/24/roundtripper-go https://pkgo.dev/net/http#RoundTripper

func NewRoundTripper

func NewRoundTripper(cache *HTTPCache) RoundTripper

NewRoundTripper returns a fully initialized RoundTripper

func (RoundTripper) RoundTrip

func (roundTripper RoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface, which replaces the http.Client's default behavior with a caching mechanism.

func (*RoundTripper) SetCache

func (roundTripper *RoundTripper) SetCache(cache *HTTPCache)

SetCache replaces the existing cache with a new one

func (*RoundTripper) SetClient

func (roundTripper *RoundTripper) SetClient(client *http.Client)

SetClient replaces the default http.Client with a custom client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳