https://github.com/joeig/gin-cachecontrol
Cache-Control middleware for Gin
https://github.com/joeig/gin-cachecontrol
cache-control gin go http
Last synced: 3 months ago
JSON representation
Cache-Control middleware for Gin
- Host: GitHub
- URL: https://github.com/joeig/gin-cachecontrol
- Owner: joeig
- License: mit
- Created: 2022-01-08T19:31:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-14T06:35:47.000Z (3 months ago)
- Last Synced: 2025-04-14T07:37:21.813Z (3 months ago)
- Topics: cache-control, gin, go, http
- Language: Go
- Homepage: https://pkg.go.dev/go.eigsys.de/gin-cachecontrol/v2
- Size: 71.3 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache-Control middleware for Gin
This Gin middleware generates cache-control headers.
[](https://github.com/joeig/gin-cachecontrol/tree/master/.github/testcoverage.yml)
[](https://goreportcard.com/report/go.eigsys.de/gin-cachecontrol/v2)
[](https://pkg.go.dev/go.eigsys.de/gin-cachecontrol/v2)## Setup
```shell
go get -u go.eigsys.de/gin-cachecontrol/v2
``````go
import "go.eigsys.de/gin-cachecontrol/v2"
```## Usage
### With a preset
```go
// 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](https://pkg.go.dev/go.eigsys.de/gin-cachecontrol/v2#pkg-variables)):
* `cachecontrol.NoCachePreset`
* `cachecontrol.CacheAssetsForeverPreset` (you may only want this for carefully selected routes)### With a custom configuration
```go
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),
}
)
)
```## Documentation
See [Go reference](https://pkg.go.dev/go.eigsys.de/gin-cachecontrol/v2).