Documentation
¶
Overview ¶
Package httpcache provides an implementation of http.RoundTripper that adds transparent HTTP response caching according to RFC 9111 (HTTP Caching).
The main entry point is NewTransport, which returns an http.RoundTripper for use with 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.
Example usage:
package main
import (
"log/slog"
"net/http"
"time"
"github.com/bartventer/httpcache"
// Register a cache backend by importing the package
_ "github.com/bartventer/httpcache/store/fscache"
)
func main() {
dsn := "fscache://?appname=myapp" // Example DSN for the file system cache backend
client := &http.Client{
Transport: httpcache.NewTransport(
dsn,
httpcache.WithSWRTimeout(10*time.Second),
httpcache.WithLogger(slog.Default()),
),
}
}
Index ¶
Constants ¶
const ( DefaultSWRTimeout = 5 * time.Second CacheStatusHeader = internal.CacheStatusHeader )
Variables ¶
var ErrOpenCache = errors.New("httpcache: failed to open cache")
ErrOpenCache 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.
Example usage:
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok && errors.Is(err, ErrOpenCache) {
// Handle the error gracefully, e.g., log it or return a default transport
log.Println("Failed to open cache:", err)
client := &http.Client{
Transport: http.DefaultTransport, // Fallback to default transport
}
// Use the fallback client as needed
_ = client
} else {
// Re-panic if it's not the expected error
panic(r)
}
}
}()
Functions ¶
func ContextWithTraceID ¶ added in v0.10.0
ContextWithTraceID 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.
func NewClient ¶ added in v0.9.0
NewClient returns a new http.Client, configured with a transport that caches HTTP responses, using the specified cache backend DSN.
See NewTransport for details on the DSN and available options.
func NewTransport ¶
func NewTransport(dsn string, options ...Option) http.RoundTripper
NewTransport returns an http.RoundTripper that caches HTTP responses using the specified cache backend.
The dsn parameter follows the format documented in store.Open. Configuration is done via functional options like WithUpstream and WithSWRTimeout.
Panics with ErrOpenCache if the cache backend cannot be opened.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithLogger ¶
WithLogger sets the logger for debug output; default: slog.New(slog.DiscardHandler).
func WithSWRTimeout ¶
WithSWRTimeout sets the timeout for Stale-While-Revalidate requests; default: DefaultSWRTimeout.
func WithUpstream ¶ added in v0.7.0
func WithUpstream(upstream http.RoundTripper) Option
WithUpstream sets the underlying http.RoundTripper used for upstream/origin requests. Default: http.DefaultTransport.
Note: 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.
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
app
command
|
|
|
Package internal provides the core unexported functionality for the httpcache package.
|
Package internal provides the core unexported functionality for the httpcache package. |
|
testutil
Package testutil provides utility functions for testing in Go.
|
Package testutil provides utility functions for testing in Go. |
|
Package store provides a registry and entry point for cache backends.
|
Package store provides a registry and entry point for cache backends. |
|
acceptance
Package acceptance provides a suite of acceptance tests for Cache implementations.
|
Package acceptance provides a suite of acceptance tests for Cache implementations. |
|
driver
Package driver defines interfaces to be implemented by cache backends as used by the github.com/bartventer/httpcache/store package.
|
Package driver defines interfaces to be implemented by cache backends as used by the github.com/bartventer/httpcache/store package. |
|
expapi
Package expapi provides HTTP handlers for managing and interacting with cache backends.
|
Package expapi provides HTTP handlers for managing and interacting with cache backends. |
|
fscache
Package fscache implements a file system-based cache backend.
|
Package fscache implements a file system-based cache backend. |
|
internal/registry
Package registry provides a registry for cache drivers.
|
Package registry provides a registry for cache drivers. |
|
memcache
Package memcache implements an in-memory cache backend.
|
Package memcache implements an in-memory cache backend. |
