middlewares

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package middlewares provides reusable middleware components for the requests HTTP client, including caching, cookie management, and header injection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheMiddleware

func CacheMiddleware(cache Cacher, ttl time.Duration, logger requests.Logger) requests.Middleware

CacheMiddleware creates a middleware that caches GET responses.

func CookieMiddleware

func CookieMiddleware(cookies []*http.Cookie) requests.Middleware

CookieMiddleware creates a middleware that adds the specified cookies to each request.

func HeaderMiddleware

func HeaderMiddleware(headers http.Header) requests.Middleware

HeaderMiddleware creates a middleware that adds the specified headers to each request.

Types

type CachedResponse

type CachedResponse struct {
	Status     string
	StatusCode int
	Headers    http.Header
	Body       []byte
}

CachedResponse represents a serializable HTTP response stored in the cache.

type Cacher

type Cacher interface {
	Get(key string) ([]byte, bool)
	Set(key string, value []byte, ttl time.Duration)
	Delete(key string)
}

Cacher is the interface for the cache.

type Duration

type Duration int64

type MemoryCache

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

MemoryCache is an in-memory cache implementation with TTL-based expiration.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new MemoryCache and starts a background goroutine to clean expired items.

func (*MemoryCache) Close added in v0.3.5

func (c *MemoryCache) Close()

Close stops the background cleanup goroutine.

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(key string)

Delete removes a cache item by key.

func (*MemoryCache) Get

func (c *MemoryCache) Get(key string) ([]byte, bool)

Get retrieves a cache item by key, returning the value and whether it was found.

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, value []byte, ttl time.Duration)

Set stores a value in the cache with the specified TTL.

Jump to

Keyboard shortcuts

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