Categorygithub.com/kenshaw/diskcache
modulepackage
0.9.0
Repository: https://github.com/kenshaw/diskcache.git
Documentation: pkg.go.dev

# README

diskcache Go Package

Package diskcache provides a standard Go HTTP transport (http.RoundTripper) implementation designed to cache, minify, compress, and transform HTTP responses on disk. Allows definition of caching policies on a per-method, per-host, or per-path basis.

Additionally provides header, body and content transformers that alter cached HTTP response headers and bodies prior to storage on disk. Includes ability to rewrite headers, white/black-list headers, strip XSS prefixes, Base64 encode/decode content, minify content, and marshal/unmarshal data stored on disk using Go's GLib/ZLib compression.

Package diskcache does not act as an on-disk HTTP proxy. Please see github.com/gregjones/httpcache for a HTTP transport implementation that provides a RFC 7234 compliant cache.

Example

A basic Go example:

// _example/example.go
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"os"
	"time"

	// "github.com/spf13/afero"
	"github.com/kenshaw/diskcache"
)

func main() {
	d, err := diskcache.New(
		// diskcache.WithFs(afero.New*(...)),
		// diskcache.WithBasePathFs(path),
		diskcache.WithAppCacheDir("diskcache-example"),
		diskcache.WithTTL(365*24*time.Hour),
		diskcache.WithHeaderWhitelist("Date", "Set-Cookie", "Content-Type"),
		diskcache.WithHeaderTransform(
			`Date:\s+(.+?)`, `Date: Not "$1"`,
		),
		diskcache.WithMinifier(),
		diskcache.WithErrorTruncator(),
		diskcache.WithGzipCompression(),
	)
	if err != nil {
		log.Fatal(err)
	}
	// create client using diskcache as the transport
	cl := &http.Client{
		Transport: d,
	}
	for i, urlstr := range []string{
		"https://github.com/kenshaw/diskcache",      // a path that exists
		"https://github.com/kenshaw/does-not-exist", // a path that doesn't
		// repeat
		"https://github.com/kenshaw/diskcache",
		"https://github.com/kenshaw/does-not-exist",
	} {
		if err := grab(cl, "GET", urlstr, i); err != nil {
			log.Fatal(err)
		}
	}
}

func grab(cl *http.Client, method, urlstr string, id int) error {
	req, err := http.NewRequest(method, urlstr, nil)
	if err != nil {
		return err
	}
	// execute request
	res, err := cl.Do(req)
	if err != nil {
		return err
	}
	fmt.Fprintf(os.Stdout, "------------------- %s %s (%d) -------------------\n", method, urlstr, id)
	buf, err := httputil.DumpResponse(res, true)
	if err != nil {
		return err
	}
	if _, err = os.Stdout.Write(buf); err != nil {
		return err
	}
	fmt.Fprintf(os.Stdout, "\n------------------- END %s %s (%d) -------------------\n\n", method, urlstr, id)
	return nil
}

See the Go package documentation for more examples.

afero support

The afero filesystem package can be used in conjunction with diskcache to satisfy advanced use-cases such as using an in-memory cache, or storing on a remote filesystem.

Notes

Prior to writing diskcache, a number of HTTP transport packages were investigated to see if they could meet the specific needs that diskcache was designed for. There are in fact a few other transport packages that provide similar functionality as diskcache (notably httpcache), however after extensive evaluation, it was decided that existing package implementations did not meet all requirements.

# Functions

Match creates a simple matcher for the provided method, host and path regular expressions, and substitution key string.
New creates a new disk cache.
NewHeaderTransformer creates a new header transformer from the passed matching regexp and replacement pairs.
NewSimpleMatcher creates a simple matcher for the provided method, host and path regular expressions, substitution key string, and other options.
NewSimpleValidator creates a simple validator.
TTL returns the ttl from the context.
UserCacheDir returns the user's system cache dir, adding paths to the end.
WithAppCacheDir is a disk cache option to set the afero fs used locked to the user's cache directory joined with the app name and any passed paths.
WithBase64Decoder is a disk cache option to add a body transformer that does base64 decoding of responses for specific content types.
WithBasePathFs is a disk cache option to set the afero fs used locked to a base directory.
WithBodyTransformers is a disk cache option to set the body transformers.
WithContentTypeTTL is a disk cache option to set the cache policy TTL for matching content types.
WithContextTTL adds the ttl to the context.
WithDefaultMatcher is a disk cache option to set the default matcher.
WithErrorTruncator is a disk cache option to add a body transformer that truncates responses when the HTTP status code != OK (200).
WithFlatChain is a disk cache option that marshals/unmarshals responses, removing headers from responses, and chaining marshaling/unmarshaling to a provided marshaler/unmarshaler.
WithFlatGzipCompression is a disk cache option that marshals/unmarshals responses, with headers removed from responses, and with gzip compression.
WithFlatStorage is a disk cache option to set a flat marshaler/unmarshaler removing headers from responses.
WithFlatZlibCompression is a disk cache option that marshals/unmarshals responses, with headers removed from responses, and with zlib compression.
WithFs is a disk cache option to set the afero fs used.
WithGzipCompression is a disk cache option to set a gzip marshaler/unmarshaler.
WithHeaderBlacklist is a disk cache option to add a header transformer that removes any header in the blacklist.
WithHeaderTransform is a disk cache option to add a header transformer that transforms headers matching the provided regexp pairs and replacements.
WithHeaderTransformers is a disk cache option to set the header transformers.
WithHeaderWhitelist is a disk cache option to add a header transformer that removes any header not in the whitelist.
WithIndexPath is a disk cache option to set the index path name.
WithLongPathHandler is a disk cache option to set a long path handler.
WithMarshalUnmarshaler is a disk cache option to set a marshaler/unmarshaler.
WithMatchers is a disk cache option to set matchers.
WithMethod is a disk cache option to set matching request method(s).
WithMinifier is a disk cache option to add a body transformer that does content minification of HTML, XML, SVG, JavaScript, JSON, and CSS data.
WithMode is a disk cache option to set the file mode used when creating files and directories on disk.
WithNoDefault is a disk cache option to disable the default matcher.
WithPrefixStripper is a disk cache option to add a body transformer that strips a specific XSS prefix for a specified content type.
WithQueryEncoder is a disk cache option to set the query encoder.
WithQueryPrefix is a disk cache option that sets a query encoder, that adds the supplied prefix to non-empty and canonical encoding The query string encoder can be limited to only the passed fields.
WithRetryStatusCode is a disk cache option to add a validator to the cache policy that retries when the response status is not the expected status.
WithStatusCodeTrunactor is a disk cache option to add a body transformer that truncates responses when the status code is not in the provided list.
WithTransport is a disk cache option to set the underlying HTTP transport.
WithTruncator is a disk cache option to add a body transformer that truncates responses based on match criteria.
WithTTL is a disk cache option to set the cache policy TTL.
WithValidator is a disk cache option to set the cache policy validator.
WithValidatorFunc is a disk cache option to set the cache policy validator.
WithZlibCompression is a disk cache option to set a zlib marshaler/unmarshaler.

# Constants

Validity states.
Validity states.
Transform priorities.
Transform priorities.
Transform priorities.
Transform priorities.
Transform priorities.
Validity states.

# Structs

Base64Decoder is a body transformer that base64 decodes the body.
Cache is a http.RoundTripper compatible disk cache.
FlatMarshalUnmarshaler is a flat file marshaler/unmarshaler, dropping original response header when marshaling.
GzipMarshalUnmarshaler is a gzip mashaler/unmarshaler.
Minifier is a body transformer that minifies HTML, XML, SVG, JavaScript, JSON, and CSS content.
Policy is a disk cache policy.
PrefixStripper is a body transformer that strips a prefix.
RegexpHeaderTransformer transforms headers matching regexps and replacements.
SimpleMatcher handles matching caching policies to requests.
SimpleValidator is a simple response validator.
Truncator is a body transformer that truncates responses based on match criteria.
ZlibMarshalUnmarshaler is a zlib mashaler/unmarshaler.

# Interfaces

BodyTransformer is the shared interface for mangling body content prior to storage in the fs.
HeaderTransformer is the shared interface for modifying/altering headers prior to storage on disk.
MarshalUnmarshaler is the shared interface for marshaling/unmarshaling.
Matcher is the shared interface for retrivieving a disk cache policy for requests.
Option is a disk cache option.
Validator is the shared interface for validating responses.

# Type aliases

HeaderTransformerFunc is a header rewriter func.
TransformPriority is the body transform priority.
ValidatorFunc is a response validator func.
Validity indicates response validity.