cachecontrol

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: MIT Imports: 4 Imported by: 0

README

Cache-Control middleware for Gin

This Gin middleware generates cache control headers.

Build Status Go Report Card PkgGoDev

Usage

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),
	}))

	// Alternatively, you can choose a preset:
	// router.Use(cacheControl.New(cacheControl.NoCachePreset))

	router.GET("/", func(ginCtx *gin.Context) {
		ginCtx.String(http.StatusOK, "Hello, Gopher!")
	})

	router.Run()
}

Documentation

See GoDoc.

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 Duration

func Duration(duration time.Duration) *time.Duration

Duration is a helper function which returns a time.Duration pointer.

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

Jump to

Keyboard shortcuts

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