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

# README

gin-cache

Release doc goreportcard for gin-cache codecov

English | 🇨🇳中文

A high performance gin middleware to cache http response. Compared to gin-contrib/cache. It has a huge performance improvement.

Feature

  • Has a huge performance improvement compared to gin-contrib/cache.
  • Cache http response in local memory or Redis.
  • Offer a way to custom the cache strategy by per request.
  • Use singleflight to avoid cache breakdown problem.
  • Only Cache 2xx HTTP Response.

How To Use

Install

go get -u github.com/chenyahui/gin-cache

Example

Cache In Local Memory

package main

import (
	"time"

	"github.com/chenyahui/gin-cache"
	"github.com/chenyahui/gin-cache/persist"
	"github.com/gin-gonic/gin"
)

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

	memoryStore := persist.NewMemoryStore(1 * time.Minute)

	app.GET("/hello",
		cache.CacheByRequestURI(memoryStore, 2*time.Second),
		func(c *gin.Context) {
			c.String(200, "hello world")
		},
	)

	if err := app.Run(":8080"); err != nil {
		panic(err)
	}
}

Cache In Redis

package main

import (
	"time"

	"github.com/chenyahui/gin-cache"
	"github.com/chenyahui/gin-cache/persist"
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
)

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

	redisStore := persist.NewRedisStore(redis.NewClient(&redis.Options{
		Network: "tcp",
		Addr:    "127.0.0.1:6379",
	}))

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

Benchmark

wrk -c 500 -d 1m -t 5 http://127.0.0.1:8080/hello

MemoryStore

MemoryStore QPS

RedisStore

RedisStore QPS

# Packages

No description provided by the author
No description provided by the author

# Functions

Cache user must pass getCacheKey to describe the way to generate cache key.
CacheByRequestPath a shortcut function for caching response by url path, means will discard the query params.
CacheByRequestURI a shortcut function for caching response by uri.
No description provided by the author
IgnoreQueryOrder will ignore the queries order in url when generate cache key .
WithBeforeReplyWithCache will be called before replying with cache.
WithCacheStrategyByRequest set up the custom strategy by per request.
No description provided by the author
WithLogger set the custom logger.
WithOnHitCache will be called when cache hit.
WithOnMissCache will be called when cache miss.
WithOnShareSingleFlight will be called when share the singleflight result.
No description provided by the author
WithPrefixKey will prefix the key.
WithSingleFlightForgetTimeout to reduce the impact of long tail requests.

# Structs

Config contains all options.
Discard the default logger that will discard all logs of gin-cache.
ResponseCache record the http response cache.
Strategy the cache strategy.

# Interfaces

Logger define the logger interface.

# Type aliases

No description provided by the author
GetCacheStrategyByRequest User can this function to design custom cache strategy by request.
OnHitCacheCallback define the callback when use cache.
OnMissCacheCallback define the callback when use cache.
OnShareSingleFlightCallback define the callback when share the singleflight result.
Option represents the optional function.