This Gin middleware generates cache-control headers.
go get -u go.eigsys.de/gin-cachecontrol/v2
import "go.eigsys.de/gin-cachecontrol/v2"
// Apply globally:
r.Use(cachecontrol.New(cachecontrol.NoCachePreset))
// Apply to specific routes:
cacheForever := cachecontrol.New(cachecontrol.CacheAssetsForeverPreset)
r.GET("/favicon.ico", cacheForever, faviconHandler)
Supported presets (documentation):
cachecontrol.NoCachePreset
cachecontrol.CacheAssetsForeverPreset
(you may only want this for carefully selected routes)
r.Use(
cachecontrol.New(
cachecontrol.Config{
MustRevalidate: true,
NoCache: false,
NoStore: false,
NoTransform: false,
Public: true,
Private: false,
ProxyRevalidate: true,
MaxAge: cachecontrol.Duration(30 * time.Minute),
SMaxAge: nil,
Immutable: false,
StaleWhileRevalidate: cachecontrol.Duration(2 * time.Hour),
StaleIfError: cachecontrol.Duration(2 * time.Hour),
}
)
)
See Go reference.