Skip to content

gin-contrib/cache

Folders and files

NameName
Last commit message
Last commit date
Apr 3, 2025
Oct 9, 2023
Apr 3, 2025
Dec 29, 2024
Nov 14, 2016
May 7, 2024
Nov 14, 2016
Dec 29, 2024
Jul 7, 2022
Dec 29, 2024
Apr 3, 2025
Apr 3, 2025

Repository files navigation

Cache gin's middleware

Build Status codecov Go Report Card GoDoc

Gin middleware/handler to enable Cache.

Usage

Start using it

Download and install it:

go get github.com/gin-contrib/cache

Import it in your code:

import "github.com/gin-contrib/cache"

Canonical example:

See the example

package main

import (
  "fmt"
  "time"

  "github.com/gin-contrib/cache"
  "github.com/gin-contrib/cache/persistence"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()

  store := persistence.NewInMemoryStore(time.Second)

  r.GET("/ping", func(c *gin.Context) {
    c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
  })
  // Cached Page
  r.GET("/cache_ping", cache.CachePage(store, time.Minute, func(c *gin.Context) {
    c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
  }))

  // Listen and Server in 0.0.0.0:8080
  r.Run(":8080")
}