Documentation
¶
Overview ¶
Package cache provides HTTP caching interfaces and utilities.
Index ¶
- func FetchURL(ctx context.Context, cache HTTPCache, client *http.Client, req *http.Request, ...) ([]byte, error)
- func FetchURLWithValidator(ctx context.Context, cache HTTPCache, client *http.Client, req *http.Request, ...) ([]byte, error)
- type BDCache
- func (c *BDCache) Close() error
- func (c *BDCache) Get(ctx context.Context, url string) (data []byte, etag string, headers map[string]string, found bool)
- func (c *BDCache) RecordHit()
- func (c *BDCache) RecordMiss()
- func (c *BDCache) SetAsync(ctx context.Context, url string, data []byte, etag string, ...) error
- func (c *BDCache) SetAsyncWithTTL(ctx context.Context, url string, data []byte, etag string, ...) error
- func (c *BDCache) Stats() Stats
- type CachedResponse
- type DomainRateLimiter
- type HTTPCache
- type HTTPError
- type ResponseValidator
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FetchURL ¶
func FetchURL(ctx context.Context, cache HTTPCache, client *http.Client, req *http.Request, logger *slog.Logger) ([]byte, error)
FetchURL fetches a URL with caching support. If cache is non-nil and contains the URL, returns cached data. Otherwise, executes the HTTP request, caches successful responses (HTTP 200), and returns the body. Returns an error if the HTTP status is not 200 OK. The caller must set all necessary headers on the request before calling this function.
func FetchURLWithValidator ¶
func FetchURLWithValidator( ctx context.Context, cache HTTPCache, client *http.Client, req *http.Request, logger *slog.Logger, validator ResponseValidator, ) ([]byte, error)
FetchURLWithValidator fetches a URL with caching support and optional response validation. If validator is provided and returns false, the response is NOT cached but still returned. This is useful for avoiding caching of incomplete/shell responses.
Types ¶
type BDCache ¶
type BDCache struct {
// contains filtered or unexported fields
}
BDCache wraps bdcache to implement the HTTPCache interface.
func New ¶
New creates a new BDCache with disk persistence. The cache directory defaults to ~/.cache/sociopath. ttl is the default time-to-live for cached entries. Use NewWithPath to specify a custom cache directory.
func NewWithPath ¶
NewWithPath creates a new BDCache with disk persistence at the specified path.
func (*BDCache) Get ¶
func (c *BDCache) Get(ctx context.Context, url string) (data []byte, etag string, headers map[string]string, found bool)
Get retrieves a cached response by URL.
func (*BDCache) RecordHit ¶
func (c *BDCache) RecordHit()
RecordHit increments the cache hit counter.
func (*BDCache) RecordMiss ¶
func (c *BDCache) RecordMiss()
RecordMiss increments the cache miss counter.
func (*BDCache) SetAsync ¶
func (c *BDCache) SetAsync(ctx context.Context, url string, data []byte, etag string, headers map[string]string) error
SetAsync stores a response in the cache asynchronously.
type CachedResponse ¶
CachedResponse holds HTTP response data.
type DomainRateLimiter ¶
type DomainRateLimiter struct {
// contains filtered or unexported fields
}
DomainRateLimiter enforces a minimum delay between requests to the same domain. It is safe for concurrent use from multiple goroutines.
func NewDomainRateLimiter ¶
func NewDomainRateLimiter(minDelay time.Duration) *DomainRateLimiter
NewDomainRateLimiter creates a rate limiter that enforces minDelay between requests to the same domain. Domain-specific overrides can be set with SetDomainDelay.
func (*DomainRateLimiter) SetDomainDelay ¶ added in v0.4.2
func (r *DomainRateLimiter) SetDomainDelay(domain string, delay time.Duration)
SetDomainDelay sets a custom minimum delay for a specific domain. This overrides the default minDelay for requests to this domain.
func (*DomainRateLimiter) Wait ¶
func (r *DomainRateLimiter) Wait(rawURL string)
Wait blocks until it's safe to make a request to the given URL's domain. It ensures at least minDelay has passed since the last request to that domain.
type HTTPCache ¶
type HTTPCache interface {
Get(ctx context.Context, url string) (data []byte, etag string, headers map[string]string, found bool)
SetAsync(ctx context.Context, url string, data []byte, etag string, headers map[string]string) error
SetAsyncWithTTL(ctx context.Context, url string, data []byte, etag string, headers map[string]string, ttl time.Duration) error
RecordHit()
RecordMiss()
Stats() Stats
}
HTTPCache defines the interface for caching HTTP responses.
type ResponseValidator ¶
ResponseValidator is a function that validates a response body. Returns true if the response should be cached, false otherwise.