Categorygithub.com/joeig/gin-cachecontrol
modulepackage
1.1.1
Repository: https://github.com/joeig/gin-cachecontrol.git
Documentation: pkg.go.dev

# 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"
	"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.

# Functions

Duration is a helper function which returns a time.Duration pointer.
New creates a new Gin middleware which generates a cache-control header.

# Variables

CacheAssetsForeverPreset is a cache-control configuration preset which advices the HTTP client and all caches in between to cache the object forever without revalidation.
NoCachePreset is a cache-control configuration preset which advices the HTTP client not to cache at all.

# Structs

Config defines a cache-control configuration.