Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
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.
Example ¶
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
cacheControl "github.com/joeig/gin-cachecontrol"
)
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()
}
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
Click to show internal directories.
Click to hide internal directories.