httpcache

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package httpcache provides HTTP response caching with thundering herd prevention.

Index

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

func ResolveRedirects(ctx context.Context, rawURL string, logger *slog.Logger) string

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.

func URLToKey

func URLToKey(rawURL string) string

URLToKey converts a URL to a cache key using SHA256 hash.

Types

type Cache

type Cache struct {
	*sfcache.TieredCache[string, []byte]
	// contains filtered or unexported fields
}

Cache wraps sfcache for HTTP response caching.

func New

func New(ttl time.Duration) (*Cache, error)

New creates a new Cache with disk persistence at ~/.cache/sociopath.

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

func NewWithPath(ttl time.Duration, cachePath string) (*Cache, error)

NewWithPath creates a new Cache with disk persistence at the specified path.

func (*Cache) TTL added in v0.7.1

func (c *Cache) TTL() time.Duration

TTL returns the default TTL for cache entries.

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 HTTPError

type HTTPError struct {
	URL        string
	StatusCode int
}

HTTPError represents an HTTP error response.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type ResponseValidator

type ResponseValidator func(body []byte) bool

ResponseValidator validates a response body. Returns true if cacheable.

Jump to

Keyboard shortcuts

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