httpcache

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package httpcache provides HTTP response caching with persistent storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	ExpiresAt time.Time `json:"expires_at"`
	ETag      string    `json:"etag,omitempty"`
	Data      []byte    `json:"data"`
}

CacheEntry represents a cached HTTP response with expiration and ETag support.

type CachedHTTPClient

type CachedHTTPClient struct {
	// contains filtered or unexported fields
}

CachedHTTPClient wraps an HTTP client with caching support.

func NewCachedHTTPClient

func NewCachedHTTPClient(cache *OtterCache, httpClient HTTPClient, logger *slog.Logger) *CachedHTTPClient

NewCachedHTTPClient creates a new cached HTTP client.

func (*CachedHTTPClient) Do

Do performs an HTTP request with caching support.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient interface for making HTTP requests.

type OtterCache

type OtterCache struct {
	// contains filtered or unexported fields
}

OtterCache implements an HTTP cache using the Otter library for efficient in-memory caching.

func NewMemoryOnlyCache

func NewMemoryOnlyCache(ttl time.Duration, logger *slog.Logger) (*OtterCache, error)

NewMemoryOnlyCache creates an in-memory only cache with no disk persistence. This is ideal for web servers where response caching is handled separately.

func NewOtterCache

func NewOtterCache(ctx context.Context, dir string, ttl time.Duration, logger *slog.Logger) (*OtterCache, error)

NewOtterCache creates a new OtterCache instance with the specified directory, TTL, and logger.

func (*OtterCache) APICall

func (c *OtterCache) APICall(url string, requestBody []byte) ([]byte, bool)

APICall retrieves cached API call data for the given URL and request body.

func (*OtterCache) Close

func (c *OtterCache) Close() error

Close shuts down the cache and saves all pending data.

func (*OtterCache) Get

func (c *OtterCache) Get(url string) (data []byte, etag string, found bool)

Get retrieves cached data for the given URL, returning data, etag, and found status.

func (*OtterCache) Set

func (c *OtterCache) Set(url string, data []byte, etag string) error

Set stores data in the cache with the given URL and etag.

func (*OtterCache) SetAPICall

func (c *OtterCache) SetAPICall(url string, requestBody []byte, data []byte) error

SetAPICall stores API call data in the cache with URL and request body as key.

func (*OtterCache) Stats

func (c *OtterCache) Stats() map[string]any

Stats returns cache statistics.

Jump to

Keyboard shortcuts

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