Documentation ¶
Index ¶
Examples ¶
Constants ¶
const CacheControlHeader = "Cache-Control"
Variables ¶
var CacheAssetsForeverPreset = Config{ Public: true, MaxAge: Duration(8760 * time.Hour), Immutable: true, }
CacheAssetsForeverPreset is a cache-control configuration preset which advices the HTTP client and all caches in between to cache the object forever without revalidation. Technically, "forever" means 1 year, in order to comply with common CDN limits.
var NoCachePreset = Config{ MustRevalidate: true, NoCache: true, NoStore: true, }
NoCachePreset is a cache-control configuration preset which advices the HTTP client not to cache at all.
Functions ¶
func New ¶
func New(config Config) gin.HandlerFunc
New creates a new Gin middleware which generates a cache-control header. Existing cache-control headers are removed. Other caching-related headers, such as `Expires` and `Pragma`, remain unchanged.
Example ¶
package main import ( "github.com/gin-gonic/gin" cachecontrol "go.eigsys.de/gin-cachecontrol/v2" "net/http" "time" ) func main() { router := gin.Default() router.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), })) router.GET("/", func(ginCtx *gin.Context) { ginCtx.String(http.StatusOK, "Hello, Gopher!") }) _ = router.Run() }
Output:
Types ¶
type Config ¶
type Config struct { MustRevalidate bool NoCache bool NoStore bool NoTransform bool Public bool Private bool ProxyRevalidate bool MaxAge *time.Duration SMaxAge *time.Duration Immutable bool StaleWhileRevalidate *time.Duration StaleIfError *time.Duration }
Config defines a cache-control configuration.
References: https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.2 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control