Documentation
¶
Overview ¶
Package httpcache provides HTTP response caching with thundering herd prevention.
Index ¶
- func FetchURL(ctx context.Context, cache Cacher, client *http.Client, req *http.Request, ...) ([]byte, error)
- func FetchURLWithValidator(ctx context.Context, cache Cacher, client *http.Client, req *http.Request, ...) ([]byte, error)
- func ResolveRedirects(ctx context.Context, rawURL string, logger *slog.Logger) string
- func URLToKey(rawURL string) string
- type Cache
- type Cacher
- type HTTPError
- type ResponseValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FetchURL ¶
func FetchURL(ctx context.Context, cache Cacher, client *http.Client, req *http.Request, logger *slog.Logger) ([]byte, error)
FetchURL fetches a URL with caching and thundering herd prevention. If cache is non-nil, uses GetSet to ensure only one request is made for concurrent calls.
func FetchURLWithValidator ¶
func FetchURLWithValidator( ctx context.Context, cache Cacher, client *http.Client, req *http.Request, logger *slog.Logger, validator ResponseValidator, ) ([]byte, error)
FetchURLWithValidator fetches a URL with caching and optional response validation. If validator returns false, the response is returned but NOT cached.
func ResolveRedirects ¶ added in v0.7.9
ResolveRedirects follows HTTP, HTML meta refresh, and JavaScript redirects. Returns the final URL after following all redirects (up to maxRedirects). If no redirects are found, returns the original URL unchanged.
Types ¶
type Cache ¶
type Cache struct {
*sfcache.TieredCache[string, []byte]
// contains filtered or unexported fields
}
Cache wraps sfcache for HTTP response caching.
func NewNull ¶ added in v0.7.2
func NewNull() *Cache
NewNull creates a Cache with no persistence (all gets miss, all sets discard).
func NewWithPath ¶
NewWithPath creates a new Cache with disk persistence at the specified path.
type Cacher ¶ added in v0.7.1
type Cacher interface {
GetSet(ctx context.Context, key string, fetch func(context.Context) ([]byte, error), ttl ...time.Duration) ([]byte, error)
TTL() time.Duration
}
Cacher allows external cache implementations for sharing across packages.
type ResponseValidator ¶
ResponseValidator validates a response body. Returns true if cacheable.