{"modulePath":"github.com/bartventer/httpcache","version":"v0.14.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpcache\n\nPackage httpcache provides an implementation of http.RoundTripper that adds transparent HTTP response caching according to RFC 9111 (HTTP Caching).\n\nThe main entry point is [NewTransport](#NewTransport), which returns an [http.RoundTripper](/net/http#RoundTripper) for use with [http.Client](/net/http#Client). httpcache supports the required standard HTTP caching directives, as well as extension directives such as stale-while-revalidate, stale-if-error and immutable.\n\nExample usage:\n\n\tpackage main\n\n\timport (\n\t\t\"log/slog\"\n\t\t\"net/http\"\n\t\t\"time\"\n\n\t\t\"github.com/bartventer/httpcache\"\n\n\t\t// Register a cache backend by importing the package\n\t\t_ \"github.com/bartventer/httpcache/store/fscache\"\n\t)\n\n\tfunc main() {\n\t\tdsn := \"fscache://?appname=myapp\" // Example DSN for the file system cache backend\n\t\tclient := \u0026http.Client{\n\t\t\tTransport: httpcache.NewTransport(\n\t\t\t\tdsn,\n\t\t\t\thttpcache.WithSWRTimeout(10*time.Second),\n\t\t\t\thttpcache.WithLogger(slog.Default()),\n\t\t\t),\n\t\t}\n\t}\n\n## Constants\n\n```go\nconst (\n\tDefaultSWRTimeout = 5 * time.Second\n\n\tCacheStatusHeader = internal.CacheStatusHeader\n)\n```\n\n## Variables\n\n```go\nvar ErrOpenCache = errors.New(\"httpcache: failed to open cache\")\n```\nErrOpenCache is used as the panic value when the cache cannot be opened. You may recover from this panic if you wish to handle the situation gracefully.\n\nExample usage:\n\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\tif err, ok := r.(error); ok \u0026\u0026 errors.Is(err, ErrOpenCache) {\n\t\t\t\t// Handle the error gracefully, e.g., log it or return a default transport\n\t\t\t\tlog.Println(\"Failed to open cache:\", err)\n\t\t\t\tclient := \u0026http.Client{\n\t\t\t\t\tTransport: http.DefaultTransport, // Fallback to default transport\n\t\t\t\t}\n\t\t\t\t// Use the fallback client as needed\n\t\t\t\t_ = client\n\t\t\t} else {\n\t\t\t\t// Re-panic if it's not the expected error\n\t\t\t\tpanic(r)\n\t\t\t}\n\t\t}\n\t}()\n\n## Functions\n\n```go\nfunc ContextWithTraceID(ctx context.Context, traceID string) context.Context\n```\nContextWithTraceID adds a trace ID to the context, which can be used for logging or tracing purposes. The trace ID can be retrieved later using [TraceIDFromContext](#TraceIDFromContext).\n\n```go\nfunc NewClient(dsn string, options ...Option) *http.Client\n```\nNewClient returns a new [http.Client](/net/http#Client), configured with a transport that caches HTTP responses, using the specified cache backend DSN.\n\nSee [NewTransport](#NewTransport) for details on the DSN and available options.\n\n```go\nfunc NewTransport(dsn string, options ...Option) http.RoundTripper\n```\nNewTransport returns an [http.RoundTripper](/net/http#RoundTripper) that caches HTTP responses using the specified cache backend.\n\nThe dsn parameter follows the format documented in [store.Open](/github.com/bartventer/httpcache/store#Open). Configuration is done via functional options like [WithUpstream](#WithUpstream) and [WithSWRTimeout](#WithSWRTimeout).\n\nPanics with [ErrOpenCache](#ErrOpenCache) if the cache backend cannot be opened.\n\n```go\nfunc TraceIDFromContext(ctx context.Context) (string, bool)\n```\nTraceIDFromContext retrieves the trace ID from the context, if it exists.\n\n## Types\n\n```go\ntype Option interface {\n\t// contains filtered or unexported methods\n}\n```\n\n```go\nfunc WithLogger(logger *slog.Logger) Option\n```\nWithLogger sets the logger for debug output; default: [slog.New](/log/slog#New)([slog.DiscardHandler](/log/slog#DiscardHandler)).\n\n```go\nfunc WithSWRTimeout(timeout time.Duration) Option\n```\nWithSWRTimeout sets the timeout for Stale-While-Revalidate requests; default: [DefaultSWRTimeout](#DefaultSWRTimeout).\n\n```go\nfunc WithUpstream(upstream http.RoundTripper) Option\n```\nWithUpstream sets the underlying [http.RoundTripper](/net/http#RoundTripper) used for upstream/origin requests. Default: [http.DefaultTransport](/net/http#DefaultTransport).\n\nNote: Headers added by the upstream roundtripper (e.g., authentication headers) do not affect cache key calculation or Vary header matching (RFC 9111 §4.1). The cache operates on the original client request, not the mutated request seen by the upstream roundtripper.\n\n","path":"github.com/bartventer/httpcache","name":"httpcache","synopsis":"Package httpcache provides an implementation of http.RoundTripper that adds transparent HTTP response caching according to RFC 9111 (HTTP Caching).","isRedistributable":true}
