httpcache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2020 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheExpirationTime

func CacheExpirationTime(response *http.Response) int64

Parse "Cache-Control" header. Return unix seconds time at which item expires. Return 0 for not-cacheable.

func NewCacheTransport

func NewCacheTransport(cachePath string) (rt http.RoundTripper, err error)

Return a net/http RoundTripper transport which caches GETs to local storage. If cachePath is "", returns http.DefaultTransport and no caching is done.

func NewClient

func NewClient(cachePath string) (*http.Client, error)

Types

type HttpCache

type HttpCache struct {
	// UnderlyingTransport defaults to http.DefaultTransport
	UnderlyingTransport http.RoundTripper

	// Maximum number of body bytes that will be saved to cache.
	// Defaults to 1000000.
	// Body gets read into RAM in its entirety whatever the size.
	// Larger things pass through but are not written to cache.
	MaxBodyBytes int
	// contains filtered or unexported fields
}

Implement net/http RoundTripper; store GET results to local storage.

There's no maximum size of total cached stuff or LRU replacement. Everything is cached until it expires. This is simple and mostly useful for static-ish API objects? (Originally developed to hold some keys in an oauth process where the keys changed on a days-to-weeks rate.)

TODO: max total size; LRU replacement (OR, closest-to-expiration replacement)

func (*HttpCache) RoundTrip

func (hc *HttpCache) RoundTrip(request *http.Request) (*http.Response, error)

implement net/http RoundTripper

type StoredResponse

type StoredResponse struct {
	Status     string `json:"S"` // "200 OK"
	StatusCode int    `json:"s"` // 200
	Proto      string `json:"p"` // "HTTP/1.0"
	ProtoMajor int    `json:"P"` // 1
	ProtoMinor int    `json:"r"` // 0

	Headers          map[string][]string `json:"h,omitempty"`
	Body             string              `json:"b"`
	TransferEncoding []string            `json:"t,omitempty"`
	Trailers         map[string][]string `json:"z,omitempty"`
}

database values are [varint expiration unix time seconds][json StoredResponse]

Jump to

Keyboard shortcuts

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