httpcache

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 17 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 *Cache, 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 *Cache,
	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 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 NewWithPath

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

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

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