cache

command
v14.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Caching on two layers: client caching headers and server-side response caching.

middleware/cache sets the HTTP caching header family (Cache-Control, Expires, ETag, Last-Modified, Vary, Pragma) and answers conditional GET/HEAD requests with 304 Not Modified. It stores nothing; it teaches browsers and proxies to. The clean way to apply it is one handler on one group, so the policy for a cluster of rarely-changing "constants" endpoints lives in a single place.

middleware/cache/httpcache is the other layer: it stores whole rendered responses in DragonflyDB/Redis and replays them without running your handlers, and Pair(r) couples the caching handler with one that flushes the group's entries after a successful mutation.

Learn: set client caching headers for a whole group with middleware/cache, and store rendered responses with httpcache, dropped after a mutation. Before: ../accesslog

Without a store the response cache is a working no-op. To use one, set REDIS_ADDR to the address of a DragonflyDB or Redis server.

Run:

go run main.go
REDIS_ADDR=localhost:6379 go run main.go

Try:

curl -i http://localhost:8080/meta/countries   # 200, Cache-Control, ETag, Expires, Vary
curl -i http://localhost:8080/reports/summary  # 200; with a store the next call is replayed
curl -i -X POST http://localhost:8080/reports  # 201, invalidates the entries above

Next: ../i18n

Jump to

Keyboard shortcuts

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