Categorygithub.com/things-go/gin-cache
modulepackage
0.1.0
Repository: https://github.com/things-go/gin-cache.git
Documentation: pkg.go.dev

# README

gin-cache gin's middleware

Go.Dev reference codecov Go Report Card Licence Tag Sourcegraph

Gin middleware/handler to enable Cache.

Usage

Start using it

Download and install it:

go get github.com/things-go/gin-cache

Import it in your code:

import cache "github.com/things-go/gin-cache"

Example

1. memory store

See the memory store

package main

import (
	"time"

	"github.com/gin-gonic/gin"
	inmemory "github.com/patrickmn/go-cache"

	cache "github.com/things-go/gin-cache"
	"github.com/things-go/gin-cache/persist/memory"
)

func main() {
	app := gin.New()

	app.GET("/hello",
		cache.CacheWithRequestURI(
			memory.NewStore(inmemory.New(time.Minute, time.Minute*10)),
			5*time.Second,
			func(c *gin.Context) {
				c.String(200, "hello world")
			},
		),
	)
	if err := app.Run(":8080"); err != nil {
		panic(err)
	}
}

2. redis store

See the redis store

package main

import (
	"time"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"

	cache "github.com/things-go/gin-cache"
	redisStore "github.com/things-go/gin-cache/persist/redis"
)

func main() {
	app := gin.New()

	store := redisStore.NewStore(redis.NewClient(&redis.Options{
		Network: "tcp",
		Addr:    "localhost:6379",
	}))

	app.GET("/hello",
		cache.CacheWithRequestPath(
			store,
			5*time.Second,
			func(c *gin.Context) {
				c.String(200, "hello world")
			},
		),
	)
	if err := app.Run(":8080"); err != nil {
		panic(err)
	}
}

3. custom key

See the custom key

package main

import (
	"time"

	"github.com/gin-gonic/gin"
	inmemory "github.com/patrickmn/go-cache"

	cache "github.com/things-go/gin-cache"
	"github.com/things-go/gin-cache/persist/memory"
)

func main() {
	app := gin.New()

	app.GET("/hello/:a/:b", custom())
	if err := app.Run(":8080"); err != nil {
		panic(err)
	}
}

func custom() gin.HandlerFunc {
	f := cache.CacheWithRequestURI(
		memory.NewStore(inmemory.New(time.Minute, time.Minute*10)),
		5*time.Second,
		func(c *gin.Context) {
			c.String(200, "hello world")
		},
		cache.WithGenerateKey(func(c *gin.Context) (string, bool) {
			return c.GetString("custom_key"), true
		}),
	)
	return func(c *gin.Context) {
		a := c.Param("a")
		b := c.Param("b")
		c.Set("custom_key", cache.GenerateKeyWithPrefix(cache.PageCachePrefix, a+":"+b))
		f(c)
	}
}

# Packages

No description provided by the author

# Functions

Cache user must pass store and store expiration time to cache and with custom option.
CacheWithRequestPath a shortcut function for caching response with url path, which discard the query params.
CacheWithRequestURI a shortcut function for caching response with uri.
GenerateKeyWithPrefix generate key with GenerateKeyWithPrefix and u, if key is larger than 200,it will use sha1.Sum key like: prefix+u or prefix+sha1(u).
GenerateRequestPathKey generate key with PageCachePrefix and request Path.
GenerateRequestURIKey generate key with PageCachePrefix and request uri.
NewDiscard a discard logger on which always succeed without doing anything.
NewPool new pool for BodyCache.
WithBodyCachePool custom body cache pool, default is private cache pool.
WithEncoding custom Encoding, default is JSONEncoding.
WithGenerateKey custom generate key ,default is GenerateRequestURIKey.
WithLogger custom logger, default is Discard.
WithRandDuration custom rand duration for expire, default return zero expiration time always expire + rand().
WithSingleflight custom single flight group, default is private single flight group.

# Variables

PageCachePrefix default page cache key prefix.

# Structs

BodyCache body cache store.
BodyWriter dup response writer body.
Config for cache.
Discard is an logger on which all Write calls succeed without doing anything.
No description provided by the author
No description provided by the author

# Interfaces

Encoding interface.
Logger logger interface.
Pool BodyCache pool.

# Type aliases

Option custom option.