httpcache

package module
v0.0.0-...-509f1ac Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

httpcache

Go coverage

An HTTP transport for Go standard library's HTTP Client that caches HTTP Responses in a database to speed up subsequent requests. Currently supported:

  1. sqlite
  2. postgres (coming soon)

Motivation

I was working on an unrelated tool that ran expensive queries over HTTP multiple times. Since the HTTP response didn't change between runs, I wrote httpcache to cap the maximum latency of making multiple HTTP requests to the latency of the initial cold request.

Implementation

When an HTTP request is made, its signature is first queried against the cache (SQLite). If found, the corresponding HTTP response is returned unmodified.

If not found (i.e. cold request), the underlying http.RoundTripper is first called to execute the HTTP request and the underlying http.Response.Body is replaced with a custom io.ReadCloser that buffers the response while it is being read and caches the response when it is closed.

The implementation currently uses a very basic hashing mechanism to create a signature of an outbound HTTP request. While most parts of an HTTP request are easy to serialize as string, the one component that is not trivial to serialize are the HTTP Headers. Since HTTP headers are expressed as an unordered map, the keys are currently sorted before concatenating each header using a pre-defined delimiter (|), and then its sha256 hash computed to generate the unique signature. This also has the benefit of not leaking Authentication headers as plaintext in the cache. The signature of an HTTP request has the following form when stored in the cache -

{HTTP Method}:{URL}:{HTTP Header Hash}

Todo

  • Basic Cache Invalidation support
  • Add postgres as an alternative source
  • Improve test coverage
  • Add pgxpool support for postgres source
  • Custom caching strategy based on HTTP response status codes
  • Explore parsing Cache Control headers for request-level control
  • Bring your own HTTP request hasher
  • Configurable Cache Invalidation support
  • Add build/contribution guide to readme
  • Support returning HTTP headers in cached response
  • Improve documentation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTransport

func NewTransport(ctx context.Context, cache CacheInitializer, rt http.RoundTripper) (*cachedTransport, error)

Types

type CacheInitializer

type CacheInitializer interface {
	Init(ctx context.Context) (ResponseCacher, error)
}

type Params

type Params struct {
	ReqHash    string
	Body       string
	Headers    string
	StatusCode int
}

type RequestHashFn

type RequestHashFn func(req *http.Request) string

type Response

type Response struct {
	ReqHash    string
	Body       string
	Headers    string
	StatusCode int
	UpdatedAt  string
}

type ResponseCacher

type ResponseCacher interface {
	GetResponse(ctx context.Context, reqHash string) (Response, error)
	CacheResponse(ctx context.Context, arg Params) (Response, error)
	DeleteAllResponses(ctx context.Context) error
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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