smartcache

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 9 Imported by: 0

README

smartcache middleware

Caches bounded GET responses with conditional-request support and pluggable kv.Store storage.

app.Use(smartcache.New(smartcache.Config{
    MaxSize: 10_000,
    DefaultTTL: 5 * time.Minute,
    MaxBodySize: 1 << 20,
    VaryHeaders: []string{"Accept-Encoding", "Accept-Language"},
}))

Requests with Authorization or Cookie are excluded by default, as are responses with Set-Cookie or private/no-store directives. Keep AllowRequestCookies=false unless the cache key safely partitions identity.

Documentation

Overview

Package smartcache provides RFC 7234 compliant HTTP caching middleware. It handles Cache-Control, ETag, If-None-Match, If-Modified-Since, If-None-Match, and conditional request semantics automatically.

The cache supports both in-memory storage and pluggable backends. Responses are stored with their headers and body, and served with proper 304 Not Modified responses when applicable.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	Methods:              []string{"GET", "HEAD"},
	CacheableStatusCodes: []int{200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501},
	MaxSize:              10000,
	DefaultTTL:           5 * time.Minute,
	MaxBodySize:          1 << 20,
}

DefaultConfig returns the default configuration.

Functions

func Delete

func Delete(store kv.Store, key string)

Delete removes key from store.

func Len

func Len(store kv.Store) int

Len reports the number of live entries in store.

func New

func New(config ...Config) fh.HandlerFunc

New creates a smartcache middleware.

func Set

func Set(store kv.Store, key string, resp *Response, ttl time.Duration)

Set JSON-encodes resp and stores it in store under key with the given TTL. Store-level errors are swallowed because caches are best-effort: callers observe a later miss rather than request failure.

Types

type CacheControl

type CacheControl struct {
	MaxAge          time.Duration
	SMaxAge         time.Duration
	NoCache         bool
	NoStore         bool
	Public          bool
	Private         bool
	Immutable       bool
	MustRevalidate  bool
	ProxyRevalidate bool
	NoTransform     bool
	StaleIfError    time.Duration
}

CacheControl directives parsed from Cache-Control header.

type Config

type Config struct {
	// Store is the cache backend. Default: kv.NewMemoryStore sized to
	// MaxSize. Pass any kv.Store implementation when constructing the
	// middleware; file-backed stores should be constructed with
	// kv.WithMaxEntrySize(maxCacheEntrySize) to bound persisted entry size.
	Store kv.Store

	// Methods is the list of HTTP methods to cache. Default: ["GET"].
	Methods []string

	// CacheableStatusCodes is the list of status codes to cache. Default: [200].
	CacheableStatusCodes []int

	// MaxSize is the maximum number of cached responses. Default: 10000.
	MaxSize int

	// DefaultTTL is the default TTL when Cache-Control is not set. Default: 5m.
	DefaultTTL time.Duration

	// MaxBodySize is the maximum response body size to cache. Default: 1MB.
	MaxBodySize int

	// Next is an optional skip function.
	Next func(ctx fh.Ctx) bool

	// KeyFunc generates a cache key from the request. Default: method + path + query.
	KeyFunc func(ctx fh.Ctx) string

	// VaryHeaders lists additional request headers folded into the cache
	// key (e.g. "Accept-Encoding", "Accept-Language") so a response cached
	// for one variant (compressed, localized, ...) is never served to a
	// request that asked for a different one.
	VaryHeaders []string

	// AllowRequestCookies permits caching/serving requests that carry a
	// Cookie header. Default false: requests with an Authorization or
	// Cookie header are never read from or written to the shared cache,
	// since a personalized/authenticated response could otherwise be
	// cached and served to a different caller on the same path.
	AllowRequestCookies bool
}

Config holds configuration for the smartcache middleware.

type Response

type Response struct {
	StatusCode int
	Headers    map[string][]string
	Body       []byte
	ETag       string
	LastMod    time.Time
	Expires    time.Time
	CC         CacheControl
	Stored     time.Time
}

Response is a cached HTTP response.

func Get

func Get(store kv.Store, key string) (*Response, bool)

Get looks up key in store and decodes it back into a *Response.

Jump to

Keyboard shortcuts

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